how to receive only int values from the user? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

how to receive only int values from the user?

want to recieve only int type data from user https://code.sololearn.com/ctw45s71Mg3Q/?ref=app

24th Oct 2017, 5:39 PM
Nitin Nair
3 Answers
+ 31
import java.util.Scanner; class Output { public static void main(String[] args) { String name="papa panther"; int x=3,y=2; int sum=x+y; ++sum; String lo="black",ho="big"; Scanner pap=new Scanner(System.in); System.out.println(x=pap.nextInt()); if(x==10){ System.out.println("too short"); } System.out.print(name+lo+ho); System.out.println(sum); } } //try this ☺ //int x=pap.nextInt (); //to get user input as a integer
24th Oct 2017, 6:47 PM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 8
The part of taking input is wrong. If pap is the Scanner object, then input should be taken like this: int num = pap.nextInt();
24th Oct 2017, 5:45 PM
Shamima Yasmin
Shamima Yasmin - avatar
+ 2
You can use hasNextInt() method. What you are going to do with this method is: https://code.sololearn.com/c2nH9xaq3b23/#java Apparently for some reason I don't know, the code doesn't work on SoloLearn. It works on NetBeans, try it on IDEs.
24th Oct 2017, 5:53 PM
Mert Açıkportalı
Mert Açıkportalı - avatar