0
public static void main(String[] args){
    char OperatorCode;
    int answer;
    int firstOperand;
    int secondOperand;
    String inputString;
    boolean quit = false;
    Scanner inLine = new Scanner(System.in);
    do{
        System.out.print("Enter the Operator Code (A,S,M or D): ");
        inputString = inLine.nextLine();
        OperatorCode = inputString.charAt(0);
        if (OperatorCode != 'Q'){
            inputString = inputString.substring(1, inputString.length());
            Scanner string = new Scanner(inputString);
            System.out.print("Enter the First Operand: ");
            firstOperand = string.nextInt();
            System.out.print("Enter the Second Operand: ");
            secondOperand = string.nextInt();
            switch (OperatorCode){
                case 'A' : answer = (firstOperand + secondOperand);
                           System.out.println(firstOperand + " + " + secondOperand + " is " + answer);
                case 'S' : answer = (firstOperand - secondOperand);
                           System.out.println(firstOperand + " - " + secondOperand + " is " + answer);
                case 'M' : answer = (firstOperand * secondOperand);
                           System.out.println(firstOperand + " * " + secondOperand + " is " + answer);
                case 'D' : answer = (firstOperand / secondOperand);
                           System.out.println(firstOperand + " / " + secondOperand + " is " + answer);
                           break;
            }               
        } else 
              quit = true;
    }while (!quit);
} 
}

What seems to be the problem on my code? It outputs the first statement(?) fine and I can input my desired character there but when it goes to the second statement(?) ("Enter the first Operand.") I get an error which says"

"Enter the First Operand: Exception in thread "main" java.util.NoSuchElementException"

Can somebody please point out my fault?

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
Rod Galangco
  • 37
  • 1
  • 9

2 Answers2

0

This may be a problem of remaining '\n'. Maybe this question may help you : Scanner issue when using nextLine after nextXXX

Community
  • 1
  • 1
chipou
  • 45
  • 1
  • 4
0

I suggest you to use string.nextLine(); instead of .nextInt(); method as it doesnt takes care of EOL while nextLine parses it

And why do you pass the string as the parameter to the scanner class here ,

Scanner string = new Scanner(inputString);

i guess it could be , (System.in)

Also add break; to your case A,S and M as John suggested

Santhosh
  • 8,181
  • 4
  • 29
  • 56