I am currently creating a maths quiz game which outputs random arithmetic questions to the user. I have a function in which it creates the questions and stores them in a string array. The questions can vary in the amount of integers present in the question; e.g. a question can be 3+4-9/3= or 4*2/9= but the minimum amount of integers that must be present is 2. e.g. 3+4= etc.
What I am trying to do is evaluate the question from reading it left to right ignoring order of operations. E.g. 4+2*5= should equal 30 instead of 14.
My thinking to this problem is this:
Get a question, loop through the string and extract individual characters and store them in an arraylist. Convert those characters into an int and first evaluate the first three elemtns in the arraylist, store the answer in a temporary int value. Get the next 2 elements and check if it does not contain an equal sign, if it doesn't then evaluate the the temporary int value with these two elements. Continue to do this whilst it doesn't find an equals sign. If it find the equals sign then stop the iteration and produce the result.
What my problem is that I cant seem to convert the operations +,-,*,/ to int values. I thought I could as every char could be converted into an int.
public void initAnswers(){
for(int i =0; i < questions.length; i++){
ArrayList<Integer> indivialIntegers = new ArrayList<>();
String questionToGetIndividual = questions[0];
//extracting individual chars
for(int j = 0; j < questionToGetIndividual.length(); j++){
indivialIntegers.add(Character.getNumericValue(questionToGetIndividual.charAt(j)));
}
}
EDIT = This is my updated code. I am getting a number format exception error. Caused by: java.lang.NumberFormatException: Invalid int
Can anybody see why? By the way, I wrote a method to generate the questions (the amount of integes in the questions are randomly generated). They are stored in an array. Im just simply iterating through each question in that array and evaluating the answer: My starting function is initAnswers().
public int computeAnswerThreeTerms(int numberOne, int numberTwo, String operation){
if(operation.equals("+")){
Log.d("onc","in addition");
return numberOne + numberTwo;
}else if(operation.equals("-")){
Log.d("onc","in subtraction");
return numberOne - numberTwo;
}else if(operation.equals("/")){
Log.d("onc","in division");
return numberOne / numberTwo;
}else if(operation.equals("*")){
Log.d("onc","in multiplication");
return numberOne * numberTwo;
}else{
return 0;
}
}
public int computeAnswer(int numberOne, String operation, int runningTotal){
if(operation.equals("+")){
Log.d("onc","in addition");
return numberOne + runningTotal;
}else if(operation.equals("-")){
Log.d("onc","in subtraction");
return numberOne - runningTotal;
}else if(operation.equals("/")){
Log.d("onc","in division");
return numberOne / runningTotal;
}else if(operation.equals("*")){
Log.d("onc","in multiplication");
return numberOne * runningTotal;
}else{
return 0;
}
}
public void getAnswer(ArrayList<String> toBeExamined){
//get numbers and put them in an array
//get operations and put them in an array
for(int i =0; i < toBeExamined.size(); i++){
String extractingIntegers = toBeExamined.get(i);
// /d matches digits /D matches non digits
String[] numbersInStringformat = extractingIntegers.split("/d");
ArrayList<Integer> numbersInNumberformat = new ArrayList<>();
for(int counter = 0; counter < numbersInStringformat.length; counter++){
numbersInNumberformat.add(Integer.parseInt(numbersInStringformat[counter]));
}
String[] operationsInStringformat = extractingIntegers.split("/D");
int tempAnswer;
int tempIntCounter =0;
int stringCounter =0;
tempAnswer = computeAnswerThreeTerms(numbersInNumberformat.get(tempIntCounter), numbersInNumberformat.get(tempIntCounter+1), operationsInStringformat[stringCounter]);
tempIntCounter = 1;
while(!operationsInStringformat[1].equals("=")){
if(operationsInStringformat[stringCounter].equals("=")){
break;
}
tempAnswer = computeAnswer(numbersInNumberformat.get(tempIntCounter++),operationsInStringformat[stringCounter],tempAnswer);
}
answers[i] = tempAnswer;
}
}
public void initAnswers(){
ArrayList<String> individualCharacters = new ArrayList<>();
for(int i =0; i < questions.length; i++){
//change i to 0 for testing purposes
String questionToGetIndividual = questions[i];
//extracting individual chars
individualCharacters.add(questionToGetIndividual);
}
getAnswer(individualCharacters);
}