+ 1

how to fix it?

import java.util.*; import java.lang.*; public class Main { public static void main(String[] args) throws NumberFormatException{ int points = 4; Scanner s = new Scanner (System.in); int myAnswer0 = s.nextInt(); if (myAnswer0 != "points"){ myAnswer0 = s.nextInt(); }else{ String myString0 = String.valueOf(myAnswer0); System.out.println(points); } } }

28th Jun 2020, 7:05 PM
Yahel
Yahel - avatar
7 Answers
+ 3
"points" is a string. You can't compare an integer with a string. But you want to compare the user input with the value of your variable points: if(myAnswer0 != points){ ... }
28th Jun 2020, 7:20 PM
Denise Roßberg
Denise Roßberg - avatar
+ 1
nextInt() is only for integer input. If the user can enter both (an integer or a string) you should ask for a string.
28th Jun 2020, 8:57 PM
Denise Roßberg
Denise Roßberg - avatar
+ 1
ok I see your full code now https://code.sololearn.com/crJoNyj46O2T so I can modify my last answer to String myAnswer0 = s.next(); int myAnswer0Int; if (! myAnswer0.equals("points") ) { myAnswer0Int = Integer.valueOf(myAnswer0); }else{ //String myString0 = myAnswer0; System.out.println(points); }
28th Jun 2020, 10:42 PM
zemiak
0
no... i want to do that when i write the word 'points' it will show me the amount of points. and when i write an integer it will accept the input as well...
28th Jun 2020, 7:31 PM
Yahel
Yahel - avatar
0
String myAnswer0 = s.next(); if (! myAnswer0.equals("points") ) points = Integer.valueOf(myAnswer0); System.out.println(points);
28th Jun 2020, 8:10 PM
zemiak
0
no... you dont get it. i need that when i write an integer the System will work just fine and print the number, but when i write the String 'points' it will tell me the amount of points.
28th Jun 2020, 8:18 PM
Yahel
Yahel - avatar
0
You don't need multiple Scanners; But I have 5 tips for you. 1. Use a String to accept input from Scanner. 2. Your current code only asks one time, after that it ends at once. To make it continuous, use a do-while loop and exit if user enters something like "exit". Another else if for "exit" where it breaks the loop. 3. To store points, use an integer variable to increment whenever got correct answer. 4. Add the "points" as another if-else to show the current point or score. 5. Simplify your switch statement, put the scanner and checking of answer outside the switch. Inside the switch should only be the computation. Here is my sample code, https://code.sololearn.com/ctL6lP807s9X/#java Hope you get my idea. You can try to execute it here, https://onlinegdb.com/rkkKqo80I
28th Jun 2020, 11:25 PM
Mike Rustia