i have a homework assignment where i need to create a calculator that accepts the user input in this form [1+2/3*5-4]. im not supposed to use an array to store the values from the string. instead im supposed to take 3 numbers and 2 operators at a time. my question is how do i store the numbers and operators and once i store them and calculate the value get q new number and a new operator from the original string. this is what i have so far not sure if im in the right direction.
public class ExpresionEvaluation {
private static String expresion;
static double o1;
static double o2;
private static double o3;
private static char operator1;
private static char operator2;
public static double getO1(String s){
s=s.trim();
String r ="";
while (s.length()>0 && s.charAt(0)>='0' && s.charAt(0)<='9'){
r = r + s.charAt(0);
s = s.substring(1);
}
o1 = Double.parseDouble(r);
return(o1);
}
public static char getOperator1(String s){
s=s.trim();
char r;
while (s.length()>0 && s.charAt(0)>='0' && s.charAt(0)<='9'){
r = s.charAt(1);
s = s.substring(2);
}
r = operator1;
return(r);
}
public static double getO2(String s){
s=s.trim();
String r ="";
while (s.length()>0 && s.charAt(0)>='0' && s.charAt(0)<='9'){
r = r + s.charAt(2);
s = s.substring(3);
}
o2 = Double.parseDouble(r);
return(o2);
}
public static char getOperator2(String s){
s=s.trim();
char r;
while (s.length()>0 && s.charAt(0)>='0' && s.charAt(0)<='9'){
r = s.charAt(3);
s = s.substring(4);
}
r = operator2;
return(operator2);
}
public static double getO3(String s){
s=s.trim();
String r ="";
while (s.length()>0 && s.charAt(0)>='0' && s.charAt(0)<='9'){
r = r + s.charAt(4);
s = s.substring(5);
}
o3 = Double.parseDouble(r);
return(o3);
}
}