import java.util.Scanner;
public class a2q1 {
public static void main(String[] args) {
double reg = 0;
double heap[];
heap = new double[5];
boolean end = false;
Scanner in = new Scanner(System.in);
while (!end) {
System.out.println("Enter the Command: ");
String cmd = in.next();
double v = in.nextInt();
if (cmd.equalsIgnoreCase("SET")){
reg = v;
}
if (cmd.equalsIgnoreCase("TELL")) {
System.out.print(reg);
}
if (cmd.equalsIgnoreCase("ADD")){
reg = reg + v;
}
if (cmd.equalsIgnoreCase("SUB")) {
reg = reg - v;
}
if (cmd.equalsIgnoreCase("MUL")) {
reg = reg * v;
}
if (cmd.equalsIgnoreCase("DIV")) {
reg = reg / v;
}
if (cmd.equalsIgnoreCase("HALT")) {
end = true;
System.exit(0);
}
}
}
}
In this code, I am trying to read some commands from the user and do the arithmetic accordingly. commands will look like, 'Add 1' which should add 1 to the register, 'Mul 2' will multiply, 'SET 10' will set the value of the register to 10, 'HALT" will terminate the program and 'TELL" will print the current register value. Although my loop seems to work fine,I think I might be having some problems with storing those string and the number (double). Any suggestions to correct this mistake? Any help is appreciated. (Ignore the heap array)
Following is the exception it is throwing
> Exception in thread "main" java.util.InputMismatchException at
> java.base/java.util.Scanner.throwFor(Scanner.java:943) at
> java.base/java.util.Scanner.next(Scanner.java:1598) at
> java.base/java.util.Scanner.nextInt(Scanner.java:2263) at
> java.base/java.util.Scanner.nextInt(Scanner.java:2217) at
> a2q1.main(a2q1.java:15)