-4

So basically, I had to write a little java programm that assigns student scores.

It works fine, I can enter H (Higher) grades fine, but when I choose S(standart) it throws me back to the editor and I just don't udnerstand whats wrong.

boolean isHigher = getInput().charAt(0) == 'H' || getInput().charAt(0) == 'h';
Pshemo
  • 113,402
  • 22
  • 170
  • 242
  • 1
    Which exception? Maybe charAt return StringIndexOutOfBounds? Maybe getInput() return null? – Marco Acierno Aug 01 '14 at 10:46
  • Does get input also change the input? So when it evaluates the second half of the ||, you get a StringIndexOutOfBounds? Or even a NullPointerException? – blitzen Aug 01 '14 at 10:48
  • How does your `getInput` method working? Is it perhaps asking user for input? In that case you when there will be no `H` at start of returned string you expect from user to rewrite its choice again. Anyway to help you with your code we need to be able to reproduce your problem. Otherwise it would be just guessing what could went wrong. – Pshemo Aug 01 '14 at 10:49
  • what is the error that you are getting? – Thusitha Thilina Dayaratne Aug 01 '14 at 10:50
  • The error I get is a StringIndexOutOfBoundException – user3899055 Aug 01 '14 at 10:54

1 Answers1

0

The only reason I can think of is that your getInput() removes the input, and doesn't actually ask it again.

In that case the following scenario occurs: the input isn't equal to "H" so you go to the or, you have no input left so you return an empty String of which you ask the char at position 0 => StringIndexOutOfBoundException.

You should use:

Character enteredCharacter = getInput().charAt(0);
boolean isHigher = enteredCharacter == 'H' || enteredCharacter == 'h';
Simon Verhoeven
  • 1,259
  • 11
  • 23