0

My main method for this program, which runs the readInput and writeOutput methods, is in a different java file. My question is as follows.

Here is the pastebin of it

Why is Java giving me an out of bounds exception at lines 31 and 32 before I can even enter a string? I'm trying to store the first character of the string as a char to use it in the following "if, else if, else" statement.

Any hints as to what might be the problem? I'm willing to bet it's something simple that I accidentally glossed right over while typing this up.

For those who cannot get the link to work, the code is as follows.

    System.out.println("Is the nature increase, decrease, or neutral?");
char nature;
nature = keys.nextLine().toLowerCase().charAt(0);

if(nature=='i')
  natureMult=1.1;
else if(nature=='n')
  natureMult=1.0;
else if(nature=='d')
  natureMult=0.9;
else
{
  System.out.println("Error! You entered an invalid multiplier!");
  System.exit(0);
}
Jon C
  • 1

1 Answers1

1

Scanner#nextInt does not consume newline characters

Instead they get pased through to the next Scanner method as is the case here. nextInt is not consuming the newline character before the nextLine() statement in (the) question so the Scanner instance keys is not blocking. Add a nextLine() statement before the statement to consume the character

keys.nextLine(); // add this 
nature = keys.nextLine().toLowerCase().charAt(0);
Reimeus
  • 158,255
  • 15
  • 216
  • 276