Yo – trying to get back into java so decided to create a calculator.
It’s suppose to work like this:
1) Get input from the user in the form of a string
-> This input is stored as a string, for example:
12+89
2) Split the string according to operator position and ensure that whatever comes before this operation is converted to an integer and stored
3) Once string has been passed through, compute the value
Im pretty sure Im making this more complicated than what it actually is :p here is the code so far:
// Split String, count digits in string
public static void spitStrg() {
int numDigits = 0;
int numOperators = 0;
for (int i = 0; i < input.length(); i++) {
if (Character.isDigit(input.charAt(i))) {
numDigits++;
}
else if (input.charAt(i) == '+') {
System.out.println( "Position of operator: "+i);
numOperators++;
}
}
System.out.println("Number of Digits: "+numDigits);
System.out.println("Number of Operators: "+numOperators);
}
// Create a number from cut off point
public static void createNum(int startPoint, int operatorPosition) {
String tempNum = "";
for (int j = startPoint; j < operatorPosition; j++) {
tempNum = tempNum + input.charAt(startPoint);
}
}
Does anyone have any other suggestions on how to handle this? I.e. ask for one number at a time and then the operator or store everything in a vector to reflect a growing number of digits and operators?
not sure if this is ok for you. (or a cheat?)
you just let user input the whole expression as a string.
23+33-34*2^2then you could :
run above code, will give you: