Sololearn: Learn to Code
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
23rd Feb 2017, 9:20 PM
Jafca
Jafca - avatar
+ 13
Tashi and luka thanks for this thread.
23rd Feb 2017, 9:31 PM
‎‏‎‏‎Joe
‎‏‎‏‎Joe - avatar
+ 11
@luka Your welcome :) a = Integer.parseInt(sc.nextLine()); sc.nextLine(); //But why do you have the sc.nextLine() here?
23rd Feb 2017, 10:07 PM
Jafca
Jafca - avatar
+ 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...
23rd Feb 2017, 8:48 PM
Tashi N
Tashi N - avatar
+ 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.
23rd Feb 2017, 8:51 PM
Tashi N
Tashi N - avatar
+ 8
@Jafca: Another 👍 Because it is a simple working solution and I was so slow on the uptake.
23rd Feb 2017, 9:33 PM
Tashi N
Tashi N - avatar
+ 6
Or use ide?
23rd Feb 2017, 9:15 PM
Tashi N
Tashi N - avatar
+ 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?
23rd Feb 2017, 9:23 PM
Tashi N
Tashi N - avatar
+ 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.
23rd Feb 2017, 9:31 PM
ChaoticDawg
ChaoticDawg - avatar
+ 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()
24th Feb 2017, 4:59 PM
Sarmad
Sarmad - avatar
+ 2
I like Java.. But I don't know why I cannot go into it. The coding structure looks complicated.
26th Feb 2017, 12:42 PM
Erick Mwenda Njagi
Erick Mwenda Njagi - avatar
+ 1
important question
27th Feb 2017, 9:22 AM
Anusionwu Philip
Anusionwu Philip - avatar
0
lol XD
25th Feb 2017, 8:43 PM
Charles T