New course! Every coder should learn Generative AI!
Try a free lesson+ 13
Use nextLine to read in the int and then parse it. Do the same with the double.
Or add an extra nextLine after the nextInt as nextInt doesn't read the last newline character.
Google>>scanner nextLine skips :D
Source: http://stackoverflow.com/a/13102066
+ 13
Tashi and luka thanks for this thread.
+ 11
@luka Your welcome :)
a = Integer.parseInt(sc.nextLine());
sc.nextLine(); //But why do you have the sc.nextLine() here?
+ 9
Weird! I tested your code, didn't find bugs. Everything fine.
I commented out the first two inputs (int + double). Input a String. String was printed.
Then input int + String - only int was printed. Same with double + String, only double printed.
I think code playground input is buggy. Maybe the input is saved in a typified array. Could explain the behaviour...
+ 9
Found a solution, not good, but working. Input String first ^.^
And it is no typified array, after inputting String, int, double you can e.g. sum int+double.
+ 8
@Jafca: Another 👍
Because it is a simple working solution and I was so slow on the uptake.
+ 6
Or use ide?
+ 6
o.o
Sorry, I never noticed that problem with Scanner and thought it was just a playground bug.
Do you have to use Scanner? BufferedReader instead?
+ 5
You should always use a different Scanner Object Instance for each different value type. In this case one for String, one for int, and really maybe even another for double.
+ 3
Actually, nextLine() consumes the last carriage return (enter key) which was pressed after inputting a floating point number. You can verify this if carefully examine the console output. So if you want to get a 3rd input from user, simply insert sc.next() before sc.nextLine()
+ 2
I like Java.. But I don't know why I cannot go into it. The coding structure looks complicated.
+ 1
important question
0
lol XD