0

for my code I'm writing to take some input from scanner (an int first, then a string, and finally a double), I can only get the code to compile when I take out the lines for int and the double. Otherwise I get this error: enter image description here

and even when I take out the int and the double, what gets printed from my string is the integer value I typed in first, not the text. I really need help with this. its driving me nuts. as someone coming from c++ this would've taken me 2 minutes to do in c++ no problem, right now its ruining my whole day.

Here's my code: enter image description here

JG98
  • 43
  • 5

1 Answers1

0

The problem in your case is with Scanner methods:

  • next() (and its companion methods nextInt(); nextDouble()) - doesn't allow Scanner to consume new line separator
  • nextLine() - advances scanner past the current line and returns the input that was skipped, cosuming new line separator

When you first type int and press ENTER, your int token is returned, but new line separator is not consumed - nextLine() scanner method is consuming empty string and remaining line separator, so your text token is being taken by nextDouble().

Basically there are a few solutions in this post, decide which one will best suit you.

spyrka
  • 11
  • 1
  • thanks, i figured it out. but i didnt really understand why it worked. your explanation is the best one. – JG98 Sep 18 '21 at 06:30