So I have a code that takes input that's an int but it keeps giving me "error: incomparable types scanner and int" how do I fix? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

So I have a code that takes input that's an int but it keeps giving me "error: incomparable types scanner and int" how do I fix?

Code error

19th Jul 2019, 10:56 PM
Enadiz
Enadiz - avatar
7 Answers
+ 3
Scanner scan = new Scanner(System.in); int x = scan.nextInt(); System.out.println(x + 2);
19th Jul 2019, 11:10 PM
Jake
Jake - avatar
+ 1
I think you have to put the .nextInt() on another variable, because the your "MyVar" is a instance of a Scanner. Scanner myVar = new Scanner(System.in); int numLine = myVar.nextInt(); if (...
19th Jul 2019, 11:58 PM
Knexx
Knexx - avatar
+ 1
Sorry I've forgot something. Use the method .nextLine and use a Integer.parseInt() to convert the value. import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int value = Integer.parseInt(sc.nextLine()); System.out.println(value); } } *But, I can run the code without that conversion
20th Jul 2019, 12:25 AM
Knexx
Knexx - avatar
+ 1
Sure. Consider voting for me :)
20th Jul 2019, 1:55 AM
Knexx
Knexx - avatar
0
See my code is import java.util.*; import java.lang.*; import java.util.Scanner; class Rextester {       public static void main(String args[])     {         int num=7;         Scanner myVar = new Scanner(System.in);         System.out.println(myVar.nextint());         if (myVar ==num ) {             System.out.println("AHHHHH BOOOIIIIII");         }else if (myVar!=num) { System.out.println("no wrong");                                              }         else { System.out.println("idek");} }     }     
19th Jul 2019, 11:24 PM
Enadiz
Enadiz - avatar
0
Sadly has not worked either
20th Jul 2019, 12:09 AM
Enadiz
Enadiz - avatar
0
Oh this worked. Thanks so much!
20th Jul 2019, 12:37 AM
Enadiz
Enadiz - avatar