-4

I have to make a simple calculator that asks for a valid operator and stops when the operator is an 'S' character. It then asks for 2 numbers to calculate. The first time calculating it works without any errors, but once it tries to ask for the next operator I get the following error:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0 at java.lang.String.charAt(String.java:658) at Main.main(Main.java:16)

import java.util.Scanner;

public class Main {

  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    boolean geldigeOperator;
    double getal1;
    double getal2;
    char operator;

    do {
        System.out.print("Operator (S = stoppen): ");
        String s = input.nextLine();
        //pakt het eerste character van de string die is ingevoerd door de gebruiker
        operator = s.charAt(0);
        geldigeOperator = isGeldigeOperator(operator);
        if (!geldigeOperator) {
            System.out.println("Operator is ongeldig");
            System.out.println("");
        } else {
            System.out.print("Eerste getal: ");
            getal1 = input.nextDouble();
            System.out.print("Tweede getal: ");
            getal2 = input.nextDouble();
            printBerekening(operator, getal1, getal2);
            System.out.println();
        }
    } while (!(operator == 'S'));
}

public static boolean isGeldigeOperator(char karakter) {
    if (karakter == '+' || karakter == '-' || karakter == '*' || karakter == '/' || karakter == '%') {
        return true;
    } else {
        return false;
    }
}

public static void printBerekening(char operator, double getal1, double getal2) {
    double result = 0;
    switch(operator) {
        case '+':
            result = getal1 + getal2;
            System.out.println(getal1 + " + " + getal2 + " = " + result);
            break;
        case '-':
            result = getal1 - getal2;
            System.out.println(getal1 + " - " + getal2 + " = " + result);
            break;
        case '*':
            result = getal1 * getal2;
            System.out.println(getal1 + " * " + getal2 + " = " + result);
            break;
        case '/':
            result = getal1 / getal2;
            System.out.println(getal1 + " / " + getal2 + " = " + result);
            break;
        case '%':
            result = getal1 % getal2;
            System.out.println(getal1 + " % " + getal2 + " = " + result);
            break;
    }
  }
}

The working code should give the following results:

Picture of expected results

Draken
  • 3,049
  • 13
  • 32
  • 49
Per Slegt
  • 1
  • 2
  • 1
    Possible duplicate of [Scanner is not taking input of another string and skipping it](https://stackoverflow.com/questions/50675707/scanner-is-not-taking-input-of-another-string-and-skipping-it) – Poorna Senani Gamage Oct 04 '18 at 08:32

2 Answers2

1

Add input.nextLine(); at the end in your else block.

The problem here is that when you call input.nextLine() it consumes whole line along with the newline at last but when you use input.nextDouble() it only takes the number not the newLline character at the end hence when you call nextLine() after nextDouble() ,nextLine() reads only newLine character and you get the error.

Nawnit Sen
  • 905
  • 2
  • 6
  • 13
0

Move the creation of the Scanner to the do..while loop. This way any redundant input you have at the end of the operation gets destroyed and you get a shiny new Scanner each time you start a new iteration.

do {
        Scanner input = new Scanner(System.in);
        System.out.print("Operator (S = stoppen): ");
        String s = input.nextLine();
        ....
} while (!(operator == 'S'));
Itamar Kerbel
  • 2,063
  • 1
  • 14
  • 26