+ 4

How Can I Use multiple Scanners in my java program?

I want to use two Scanners in my java program. Say, I want to enter two user-Desired numbers and find their sum? I am a beginner, so plzzz anyone help me and make me understand?

25th Feb 2017, 5:16 AM
XxTUBOLTxX
XxTUBOLTxX - avatar
6 Réponses
+ 5
one scanner does it all. import java.util.Scanner; ... Scanner mScanner = new Scanner(System.in); int first = mScanner.nextInt(); int second = mScanner.nextInt(); System.out.printf( "sum of %d and %d is %d", first, second, (first + second)); mScanner.close(); hope this helps!!!
25th Feb 2017, 6:01 AM
Michael Szczepanski
Michael Szczepanski - avatar
+ 2
fair enough... if you have used c++ its the cin. the scanner is an "object" the import at the top allows us to use the "class" to create this object. in order to use it, we have to create it. Scanner scan = new Scanner(System.in); this is how we create objects in java. scan is now a Scanner object. template for any object declaration: dataType objName = new dataType(param); this might seem like much but this is one of the most important concepts of object orientated programming. when I declared the "first" integer. you can see its similar to int x = 5; but with different values. so the dataType is int. the objectName is x and its value is 5. so for the others one. its dataType is int its name is first and its value is going to be whatever the user inputs into the console. that scan.nextInt(); will wait for the user to enter a number. then when they press enter it will save that number into the first variable. ***if they type in something that is Not an integer, the program Will break*** that printf is a formatted print function. thats a little more advanced than the others but you should definitely know how to use them at the end I have scan.close(); it closes the scanner.... you dont Need to have this, but its good practice to include it. I hope that this explained it better. if you want me to explain one thing in more depth please dont hesitate to ask!! im always avaiable and full of knowledge.
25th Feb 2017, 4:45 PM
Michael Szczepanski
Michael Szczepanski - avatar
+ 2
I went through and commented on your codes. little tid bits
3rd Mar 2017, 3:25 PM
Michael Szczepanski
Michael Szczepanski - avatar
+ 1
wow man! I think You can help me , Sir @Michael Szczepanski, or you Young @XxTUBOLTxX... Does JAVA support 2 or more Scanners instantiated in a program? I mean... in main() body I have a [ Scanner sc1 = new Scanner(System.in) ] for users type in some information, then... a kind of menu is executed, and program executes a method to interact with user, colecting another kind of data using another Scanner, so, inside of this method I have [ Scanner scReg = new Scanner(System.in) ]. In short, main() has one Scanner sc1 , myMethod() has one other Scanner scReg . Is it possible ? If yes... How ? Please, I need this help, nobady could help up to now. I aways have an error: "NoSuchElementException" inside of "Scanner.class" .
8th Aug 2019, 8:48 PM
Ronaldo Marques
Ronaldo Marques - avatar
0
Thanks a lot for ur kind support...but this goes beyond my understanding...I told u that I am a beginner...Sire, I m stoned...
25th Feb 2017, 9:21 AM
XxTUBOLTxX
XxTUBOLTxX - avatar
0
Thanks a lot Sir Michael Szczepanski, I really appreciate your kind support and help. This time I got its root, and you made me completely known to it. Thanks a lot, once again, now I think that I can also make my friends familiar with it. I have created a few programs based on your teaching and my ability to grasp. You can take a look at them and tell me if have I created any mistake.
3rd Mar 2017, 10:53 AM
XxTUBOLTxX
XxTUBOLTxX - avatar