Java Programming for Mobile Developers Part 6 !
The use of several of the Java I/O classes may be required to successfully receive input that is typed by the user. The java.io package contains most, if not all, of the classes you will need to use. The java.util package also contains some classes that are useful for input and output. Don't worry, you won't need to use all 50+ classes. But, you will need to learn about and use at least a couple of them. Either use the fully qualified name shown or import the specified package to use either of these classes.
java.io.InputStream - stores information about the connection between an input device and the computer or program.
java.util.Scanner - used to read the input available from an InputStream object.
The Scanner class has a method called nextLine that returns a line of text as typed by the user. There are two available constructors for creating a Scanner object. For console input, we will use the one that requires only one argument, an instance of an InputStream object.
We will use the System.in object as our InputStream object and then use that object to create an instance of the Scanner class.
Steps for console based user input:
Use the System.in object to create a Scanner object.
Display a prompt to the user for the desired data.
Use the Scanner object to read a line of text from the user.
Do something interesting with the input received from the user.
Would you like to see some code?
I thought so.
Here it is:
1. Create a Scanner using the InputStream available.
Scanner scanner = new Scanner( System.in );
2. Don't forget to prompt the user
System.out.print( "Type some data for the program: " );
3. Use the Scanner to read a line of text from the user.
String input = scanner.nextLine();
4. Now, you can do anything with the input string that you need to.
Like, output it to the user.
System.out.println( "input = " + input );
Disclaimer : This video does not represent "Java Programming for Mobile Developers" officially. This video tutorial is solely for educational purpose only.
Credits to Udemy
Related searches :java programming for mobile developers ,java mobile programming tutorial, java mobile programming, java programming,java jdk, intelli j idea, intelli idea overview, the main class demo, output demo, input demo, preview of classes, finding information of class, classes and objects demo, data types demo, modifiers demo, javamobile84845