1
import java.util.Scanner;
public class mathstrainer {

    public static void main(String[] args) {
    Scanner answerinput=new Scanner(System.in);
    System.out.println("Welcome to Maths Trainer!");
    boolean choice=true;
    int max=15;
    int min=1;
    while(choice=true){
    int number1=(int)(Math.random()*max+min);
    int number2=(int)(Math.random()*max+min);
    System.out.println("what is "+number1+"+"+number2+"? >");
    int answer=answerinput.nextInt();
    int solution=number1+number2;
if(answer==solution){
    System.out.println("Correct! Well done!");
    System.out.println("Would you like another question? (Y/N)");
    String choiceword=answerinput.nextLine();
    choiceword=choiceword.toUpperCase();
    char choiceletter=choiceword.charAt(0);
    if(choiceletter=='Y'){
        choice=true;
    }
    else{
        choice=false;
    }

}
else{
    System.out.println("Incorrect! The right answer is "+solution);
    System.out.println("Would you like another question? (Y/N)");
    String choiceword=answerinput.nextLine();
    choiceword=choiceword.toUpperCase();
    char choiceletter=choiceword.charAt(0);
    if(choiceletter=='Y'){
        choice=true;
        }
    else{
        choice=false;
    }
}
}}}

It comes with the problem:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
at java.lang.String.charAt(Unknown Source)
at mathstrainer.main(mathstrainer.java:36)

Where have I gone wrong, and how can I fix it?

I am trying to create a loop using a boolean variable to act as the question creator and repeator but the loop can be stopped by turning the boolean value to false.

Also, what does the problem even mean? I seem to get this same problem a lot.

Chris Martin
  • 28,558
  • 6
  • 66
  • 126
  • http://stackoverflow.com/questions/13102045/skipping-nextline-after-using-next-nextint-or-other-nextfoo-methods – kirbyquerby Mar 07 '16 at 00:44
  • how does that apply to my question? like how would i impliment the thread you sent me about the next line being consumed? – death symbiote Mar 07 '16 at 00:47
  • You are calling nextLine() after nextInt() and so are reading in the (empty) end of the line instead of the input you want which is on the next line, causing choiceword to be blank. You need to call nextLine() after your nextInt() call. Read the other question's answer to understand. – kirbyquerby Mar 07 '16 at 00:50
  • how do i call nextline after a nextint call? – death symbiote Mar 07 '16 at 00:53
  • Call it straight after your nextInt() call so that the end of the line is read (you can literally just make the method call, you don't need to assign a value) then your next nextLine() call will work properly. Alternatively, you can say Integer.value of(scanner.nextLine()) to get an integer from input without having to worry – kirbyquerby Mar 07 '16 at 00:55
  • what do you mean by a method call? i am at a very basic stage of java learning and i dont know what it means – death symbiote Mar 07 '16 at 00:56
  • When you reference something using parentheses, you are calling a method, so nextLine() and next() are methods. A method is a block of code written that can be executed when it is called rather than forcing you to rewrite code for a repeated task. – kirbyquerby Mar 07 '16 at 00:57
  • Since you didn't follow normal Java class-name capitalization conventions, I read the name of your class as "Math Strainer" – Mad Physicist Mar 07 '16 at 00:57
  • can you help fix the code? i have changed it and the error is gone but when i press "n" it doesnt stop the loop even though it makes the choice variable false. when the variable is false, the loop should stop. – death symbiote Mar 07 '16 at 01:01

1 Answers1

2

When you call answerinput.nextInt() the scanner reads the integer and continues in the same line, after, the answerinput.nextLine() will read the input until find a line break, returning a empty string in your case. Possible solutions:

  • add a nextLine() after nextInt() to read the line break character.
  • replace the nextLine() with next(). The Scanner.next() will find the next token in input.
Alex Karshin
  • 11,259
  • 12
  • 45
  • 57
Emerson Dallagnol
  • 1,279
  • 1
  • 13
  • 21