+ 1

Why can't the Switch method be called?

The program can't use the user's input as a parameter for the switch method. What am I doing wrong? https://code.sololearn.com/cuNfAyG2BXXc

14th Nov 2017, 12:19 AM
Yukinori Shinohara
Yukinori Shinohara - avatar
2 Answers
+ 8
Your nextLine call is your problem. Change it to next and it runs. if you enter 2.7 1.3 Add on one line, oPerator gets ' Add'. if you enter on separate lines, nextLine gets the empty string after 1.3 to eol.
14th Nov 2017, 1:04 AM
John Wells
John Wells - avatar
+ 9
One of the best tricks for testing code is put debugging output in the program temporarily. I put a System.out.println("'"+oPerator+"'"); before the switch and there was the answer. I myself am still learning Java (though I've decades of programming experience) so I wasn't able to figure it out by looking. Now, I will know for next time.
14th Nov 2017, 1:51 AM
John Wells
John Wells - avatar