-1

I am making a temperature converter and I ran into the following issue:

Would you like to make another conversion ? (Y/N)

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:79)
exit status 1

You can check out the full code here https://repl.it/@KyrillHuet/Temperature-Converter

And it seems that the first part works just fine but the last parst is causing the issue.

do{
  do{//New coversion?
    System.out.println();
    System.out.println("Would you like to make another conversion ? (Y/N)");

    response = sc.nextLine().charAt(0);
  } while (response != 'Y' & response != 'N'); // Filtering other awnsers than Y or N.

} while (response == 'Y');

I just started to learn java and in when I looked and checked with my book but everything seems fine. Help would be greatly appreciated. =)

BugsForBreakfast
  • 712
  • 10
  • 30
Hukify
  • 161
  • 1
  • 2
  • 7
  • Possible duplicate of [What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?](https://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it) – Robert Jun 19 '19 at 15:46

3 Answers3

0

I'm pretty sure you need to nullcheck the sc or the sc.nextline before you use it. Those compound chains of commands can get you into trouble unless you are sure that they are not going to be null.

0

I have tried the code in the link that you have provided. I cannot say why it is throwing that error using response = sc.nextLine().charAt(0);. However, if I use response = sc.next().charAt(0);, it works okay. So my suggestion is you use response = sc.next().charAt(0);. If anyone can explain why this is the case then they are free to do so.

Vinyl
  • 333
  • 1
  • 7
-2

Small correction in your code will work. Next instead of Next line since cursor already in same line

do{
  do{//New coversion?
    System.out.println();
    System.out.println("Would you like to make another conversion ? (Y/N)");

    response = sc.next().charAt(0);
  } while (response != 'Y' & response != 'N'); // Filtering other awnsers than Y or N.

} whil

[updated ]

Original issue posted has nextLine in the response. On trying to access Char at position 0 the string index out of range is thrown.

The problem here is the difference between next() and nextLine()

next() reads the string before the space. it cannot read anything after it gets the first space and nextLine() reads the whole line

In the example, since nextLine() is used the pointer try to find Char at position zero in the next line which does not exist

so keeping this as next() will get the content in the same line.

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
BharathyKannan
  • 148
  • 2
  • 3
  • 19
  • @Robert it is just an excerpt from the example provided, i made the changes in the https://repl.it/repls/ShyDualSeptagon and posted my comment here – BharathyKannan Jun 19 '19 at 15:29
  • Why do you think it's helpful to split your answer over two unrelated sites? – Robert Jun 19 '19 at 15:32
  • @Robert The original example reference is https://repl.it/repls/ShyDualSeptagon so I checked the code and see what is the issue , corrected it and posted what really need (specific answer ). – BharathyKannan Jun 19 '19 at 15:32
  • I don't know how long repl.it keeps answers around. Clearly your answer is useless without that repl.it link. It would be better to make sure your answer here stands on its own. You could link to repl.it, but link-only answers are discouraged and your answer is likely to get deleted. – Robert Jun 19 '19 at 15:48
  • @Robert i understand your point, i updated the answer accordingly. it should be better – BharathyKannan Jun 19 '19 at 15:57