Reputation: 108
I have made a console calculator that the user input the numbers and the operation in one line like this;
5 + 5
after pressing enter it will show the result like this;
Result: 5 + 5 = 10
I am trying to store every complete result to an array so I can print or list them when the user pressed (L) but I could not succeed as my code always stores the last calculation. I want the calculations printed when the user press L like this:
Operation: L
Previous Calculations:
5 + 5 = 100
4 * 5 = 20
Here is my code and I thank you in advance.
public static void main(String[] args) {
do {
System.out.print("**********************************************\n"
+ "Enter L to list previous calculations.\nEnter Q to exit.\n"
+ "**********************************************\n"
+ "Operation: ");
Scanner scan = new Scanner(System.in);
String input = scan.nextLine();
String[] operation = input.split(" ");
if (input.length() < 5) {
if (input.equals("q")) {
System.out.print("\n\n**********************************************\n"
+ "ByeBye\n"
+ "**********************************************\n");
System.exit(0);
}
if (input.equals("l")) {
System.out.println("\n\nPrevious Calculations:\n");
continue;
} else {
System.out.println("\n\nWrong Operation!!!");
continue;
}
}
int first_number = Integer.parseInt(operation[0]);
int second_number = Integer.parseInt(operation[2]);
int outcome = 0;
boolean wrong_operation = false;
String operator = operation[1];
switch (operator) {
case "+":
outcome = first_number + second_number;
break;
case "-":
outcome = first_number - second_number;
break;
case "*":
outcome = first_number * second_number;
break;
case "/":
outcome = first_number / second_number;
break;
default:
wrong_operation = true;
}
String answer;
String array_item;
if (wrong_operation) {
System.out.println("\n\nWrong Operation!!! " + "(" + operator + ")");
} else {
answer = String.valueOf(outcome);
array_item = input + " = " + answer;
System.out.println("\n\nResult: " + array_item);
char[] myArray = array_item.toCharArray();
System.out.println(myArray);
}
} while (true);
}
Upvotes: 1
Views: 55
Reputation: 331
You can declare a list to store the previous calculations before de do
declaration, like this:
public static void main(String[] args) {
List<String> previousCalculations = new ArrayList<>(); // Declare list
do {
System.out.print("**********************************************\n"
+ "Enter L to list previous calculations.\nEnter Q to exit.\n"
+ "**********************************************\n"
+ "Operation: ");
...
...
Then store the results after you show the answer
answer = String.valueOf(outcome);
array_item = input + " = " + answer;
System.out.println("\n\nResult: " + array_item);
previousCalculations.add(array_item); // Store results
And finally print the previous calculations when the option is selected:
if (input.equals("l")) {
System.out.println("\n\nPrevious Calculations:");
// Print the previous calculations
for(String calc : previousCalculations) {
System.out.println(calc);
}
continue;
} else {
System.out.println("\n\nWrong Operation!!!");
continue;
}
Upvotes: 2