stringcalculator

Solutions on MaxInterview for stringcalculator by the best coders in the world

showing results for - "stringcalculator"
Sherman
06 Jun 2017
1/* 
2    Shilendra Kumar Mishra
3    shilendra308388@gmail.com
4  */
5package stringcalculator;
6
7import java.util.Scanner;
8
9public class StringCalculator {
10
11	public static int add(String numbers) throws Exception{
12		int n;
13		int sum=0;
14	
15		
16	
17		if(numbers.equals("")){
18			return 0;
19		}
20		else{
21		String []s= numbers.split(",");
22		
23		for(int i=0;i<s.length;i++){
24			n=Integer.parseInt(s[i]);
25			if(n<0){
26				throw new Exception("Negative is not allowed");
27			}
28			sum+=n;
29		}
30		
31		}
32		return sum;
33	}
34	
35	public static void main(String[] args) throws Exception {
36		String num="";
37		int sum=0;
38		Scanner sc=new Scanner(System.in);
39		System.out.println("enter two number with separtion by comma , ");
40		System.out.println("for exaple \"1,2\" ");
41		 num=sc.next();
42		 
43		sum= add(num);
44		
45		System.out.println("the resulting sum is = "+sum);
46		
47		
48		
49		
50
51	}
52
53}
54
Leah
03 Jan 2021
1package stringcalculator;
2
3import java.util.Scanner;
4
5public class StringCalculator {
6
7	public static int add(String numbers) throws Exception{
8		int n;
9		int sum=0;
10	
11		
12	
13		if(numbers.equals("")){
14			return 0;
15		}
16		else{
17		String []s= numbers.split(",");
18		
19		for(int i=0;i<s.length;i++){
20			n=Integer.parseInt(s[i]);
21			if(n<0){
22				throw new Exception("Negative is not allowed");
23			}
24			sum+=n;
25		}
26		
27		}
28		return sum;
29	}
30	
31	public static void main(String[] args) throws Exception {
32		String num="";
33		int sum=0;
34		Scanner sc=new Scanner(System.in);
35		System.out.println("enter two number with separtion by comma , ");
36		System.out.println("for exaple \"1,2\" ");
37		 num=sc.next();
38		 
39		sum= add(num);
40		
41		System.out.println("the resulting sum is = "+sum);
42		
43		
44		
45		
46
47	}
48
49}
Elyas
05 Jul 2018
1/* 
2   Shilendra Kumar Mishra
3   shilendra308388@gmail.com
4   */
5package stringcalculator;
6
7import java.util.Scanner;
8
9public class StringCalculator {
10
11	public static int add(String numbers) throws Exception{
12		int n;
13		int sum=0;
14	
15		
16	
17		if(numbers.equals("")){
18			return 0;
19		}
20		else{
21		String []s= numbers.split(",");
22		
23		for(int i=0;i<s.length;i++){
24			n=Integer.parseInt(s[i]);
25			if(n<0){
26				throw new Exception("Negative is not allowed");
27			}
28			sum+=n;
29		}
30		
31		}
32		return sum;
33	}
34	
35	public static void main(String[] args) throws Exception {
36		String num="";
37		int sum=0;
38		Scanner sc=new Scanner(System.in);
39		System.out.println("enter two number with separtion by comma , ");
40		System.out.println("for exaple \"1,2\" ");
41		 num=sc.next();
42		 
43		sum= add(num);
44		
45		System.out.println("the resulting sum is = "+sum);
46		
47		
48		
49		
50
51	}
52
53}
54
55
Fernando
05 May 2019
1package stringcalculator;
2
3import java.util.Scanner;
4
5public class StringCalculator {
6
7	public static int add(String numbers) throws Exception{
8		int n;
9		int sum=0;
10	
11		
12	
13		if(numbers.equals("")){
14			return 0;
15		}
16		else{
17		String []s= numbers.split(",");
18		
19		for(int i=0;i<s.length;i++){
20			n=Integer.parseInt(s[i]);
21			if(n<0){
22				throw new Exception("Negative is not allowed");
23			}
24			sum+=n;
25		}
26		
27		}
28		return sum;
29	}
30	
31	public static void main(String[] args) throws Exception {
32		String num="";
33		int sum=0;
34		Scanner sc=new Scanner(System.in);
35		System.out.println("enter two number with separtion by comma , ");
36		System.out.println("for exaple \"1,2\" ");
37		 num=sc.next();
38		 
39		sum= add(num);
40		
41		System.out.println("the resulting sum is = "+sum);
42		
43		
44		
45		
46
47	}
48
49}
50
51
queries leading to this page
stringcalculatorstringcalculator