0

Error In Code

This code does not execute the name[i] = in.nextLine() part of the code. Instead, it skips directly to the income[i] = in.nextLong line. Can anyone please tell me why? Here's the Playground link: https://code.sololearn.com/cmsNy1M1L4go/#java

23rd Jan 2019, 2:52 PM
Arpan Sircar
Arpan Sircar - avatar
5 Réponses
+ 2
Add an in.nextLine(); after reading an int or long to read the newline character, otherwise you have to type your inputs like so: 2 Alice 300000 Bob 200000 Also, when calculating tax, do the division last since it's the integer division (20/100 is 0). Otherwise, the code works. Edit: fixed
23rd Jan 2019, 3:10 PM
Zen
Zen - avatar
+ 1
Not sure what error you were getting, but it is probably due to how your entries were entered. Also, you not reading the newline character after reading a long, as I said.
23rd Jan 2019, 6:45 PM
Zen
Zen - avatar
0
Fam, not important. What I don't understand is why the code skips the part where it has to take String inputs into the array. There are no compilation errors in the array, the compiler made sure of that. And I'm not getting the runtime error that is occurring.
23rd Jan 2019, 3:29 PM
Arpan Sircar
Arpan Sircar - avatar
0
I have fixed my answer.
23rd Jan 2019, 4:44 PM
Zen
Zen - avatar
0
Okay, so the code is working fine, but the thing is why does it occur at all? I tried the same code using BufferedReader class from the io package and it doesn't show any such errors. Does this happen only in Scanner?
23rd Jan 2019, 6:33 PM
Arpan Sircar
Arpan Sircar - avatar