Hi I'm a big problem:
Write a program that computes the sum and difference of n numbers entered from the terminal ;
the numbers and operators are separated from each other by one or more spaces .
eg : 1 + 3-4 + 12 +2 - 10-1
and must be printed : 3
Now my program is:
Code:
import java.util.StringTokenizer;
import java.util.Scanner;
class Calcolatore{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
String numeri=sc.nextLine();
StringTokenizer st = new StringTokenizer(numeri," +");
StringTokenizer sk = new StringTokenizer(numeri," -");
int add=0;
while(st.hasMoreTokens()){
String f=st.nextToken();
String g=sk.nextToken();
int n=Integer.parseInt(f);
int z=Integer.parseInt(f);
if(f.equals(" +")){
add=add+n;
}else if(g.equals(" -")){
add=add-n;
}
}
System.out.println(add);
}
}
where am I wrong?
thank you every one