title: Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0 at java.lang.String.charAt(String.java:658)
body: <p>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:</p>
  
  <blockquote>
    <p>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)</p>
  </blockquote>
  
  <pre><code>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;
      }
    }
  }
  </code></pre>
  
  <p>The working code should give the following results:</p>
  
  <p><a href="https://i.stack.imgur.com/MKmPy.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/MKmPy.png" alt="Picture of expected results"></a></p>