Can we use Scanner(System.in).nextInt() in java?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Can we use Scanner(System.in).nextInt() in java?



Can this statement be used in Java:


int a = new Scanner(System.in).nextInt();



If it can, can anyone explain it to me?





You’ll have to use new to create a Scanner object, but yes. Can you add some more code and context?
– Logan
Aug 6 at 3:14



new


Scanner





Have you, I dunno, tried running it?
– Mad Physicist
Aug 6 at 4:07





6 Answers
6



You can do it this way also, but you are not assigning the Scanner object to any reference variable. So if you want to read any input again you would need to create another Scanner object.


Scanner


Scanner


int a = new Scanner(System.in).nextInt();



The best approach is to create a Scanner object and store it in a reference variable.


Scanner


Scanner sc = new Scanner(System.in);



Scanner is a class and nextInt() isn't static method
so,you must use new keyword to make Scanner object, and then you can use that method


Scanner


nextInt()


new


Scanner



Yes, the following code would work:


System.out.println("Enter a number: ");
int k = new Scanner(System.in).nextInt();
System.out.println(k);



Why? You are declaring an int (k). You are setting it's value equal to a new instance of the Scanner object that we are creating which will store the int primitive that we choose to enter. So the type is compatible. IF you attempted to use the Scanner.next() method used for strings, or the Scanner.nextFloat() method, they would not be compatable. Since we will be storing an integer however, there is no compilation error with this statement.



BUT just because you can do something in Java doesn't mean that you should. It is a better practice to first create a Scanner object, and then create the int variable:


System.out.println("Enter a number: ");
Scanner reader = new Scanner(System.in);
int k = reader.nextInt();



This statement is not right. You have to create an object of class Scanner by using the new operator as shown below.


Scanner


new


Scanner sc = new Scanner(System.in);
int val = sc.nextInt();





But i seen someone on internet doing this
– Pratik kataria
Aug 6 at 3:16





@Pratikkataria With or without new? The code snippet would work with new, but the object would be garbage collected after reading one input.
– Logan
Aug 6 at 3:19



new


new





@Steephen a = new Scanner(System.in).nextInt(); then what is the object of this statement
– Pratik kataria
Aug 6 at 3:26






without new, it works in few languages like Kotlin
– Viswanath Kumar Sandu
Aug 6 at 3:27





@Pratikkataria new Scanner(System.in) does create a temporary object of Scanner with out any name.
– Steephen
Aug 6 at 3:34



new Scanner(System.in)



Your statement is not correct. It needs to be the following:


int value = new Scanner(System.in).nextInt();



Only do this if you are not planning to use the Scanner object again.


Scanner



Yes it can be used.



Assigning it to a Scanner object and calling a function is popular choice and also it increases readability.



It is important to assign it to a Scanner object when you want to use it more than once.



Example:


Scanner sc = new Scanner(System.in);

int sum = 0, count = 0;

while (sc.hasNextInt())

int num = sc.nextInt();
sum += num;
count++;



here you are using the same object more than once.



But if you are going to do something like this,


Scanner sc = new Scanner(System.in);

int num = sc.nextInt();

System.out.println("num " + num);



here you are using the object only once.



It is not really important to assign it to object and call the function. You can simply do this


System.out.println("num" + new Scanner(System.in).nextInt());






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

make 2 or more post in bootsrap

Store custom data using WC_Cart add_to_cart() method in Woocommerce 3

Firebase Auth - with Email and Password - Check user already registered