User input into if statement? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

User input into if statement?

Does anyone know how to use the result of the user input in an if (user input > 16) { statement? Here is my code: https://code.sololearn.com/czXMyLYhM3XC/?ref=app It says that the ‘>’ symbol is not suitable for the binary operation. Please help. Thanks.

15th Jul 2018, 2:11 PM
M10Coding
M10Coding - avatar
7 Answers
0
Integer.parseInt() turns strings (text) into a number value e.g. “56” -> 56 .nextLine() always returns a string so in order to treat input like a number you need to turn it to such
15th Jul 2018, 4:23 PM
TurtleShell
TurtleShell - avatar
+ 2
✔ You can write yourself if you complete this seriously ✔ Nicely explained with simple example ➡https://www.sololearn.com/learn/Java/2205/
15th Jul 2018, 2:22 PM
Sudarshan Rai
Sudarshan Rai - avatar
0
You have to store the input inside a variable... String input = weight.nextLine(); Then to compare it to 16 you need to turn that string into an int (number)... if (Integer.parseInt(input) > 16) { // ... }
15th Jul 2018, 2:21 PM
TurtleShell
TurtleShell - avatar
0
TurtleShell what does the (Integer.parseInt) method here mean? my code doesnt seem to agree with it.
15th Jul 2018, 2:34 PM
M10Coding
M10Coding - avatar
0
TurtleShell sorry to keep asking questions but the code is still returning an error. if you wouldnt mind reviewing it that would be great. thanks. https://code.sololearn.com/czXMyLYhM3XC/?ref=app
15th Jul 2018, 4:36 PM
M10Coding
M10Coding - avatar
0
Printing input instead of weight.nextLine() will do the job :)
15th Jul 2018, 4:37 PM
TurtleShell
TurtleShell - avatar
0
TurtleShell thanks very much. fixed the issue!
15th Jul 2018, 4:39 PM
M10Coding
M10Coding - avatar