0
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)
Abra
  • 19,142
  • 7
  • 29
  • 41
yashrex12
  • 5
  • 3
  • You need to call method `nextLine` after you call method `nextInt`. Refer to [Scanner is skipping nextLine() after using next() or nextFoo()?](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo) Alternatively, you could just call `nextLine` (rather than `next` followed by `nextInt`) to read the entire line and parse it yourself. – Abra May 28 '22 at 03:59

1 Answers1

0

After you read a word using in.next() , you also need to get rid of the line entered by the user after the word (you might not write any line as a user, still there will be a line since you press enter). To get rid of this line, do this

String cmd = in.next();
in.nextLine();
double v = in.nextInt();

Now it will work fine.

Utkarsh Sahu
  • 409
  • 3
  • 16