First Steps in Java — Part 3
Input
1: Setup | 2: Variables, Data Types and Sizes | 3: Input | 4: Operators | 5: Strings | 6: Conditionals | 7: Loops | 8: Methods | 9: Arrays
Yesterday, we talked about variables and the various data types. I hope you had fun doing the exercise. Today, we are going the discover a way how to get the user input into a file. This way, we dynamically set the values of our variables.
Output
In the first lesson, we learned how to output data with the SOP statement. Just to repeat: System.out.println()
prints the argument in the parenthesis onto the screen.
Input
Now, we need to get some data into the program. For that, we need to import the library java.util, which contains the Scanner class with the import keyword.
There are many Java libraries. They provide additional functionality for all different purposes, e.g. networking, databases, Html parsing, e-mail, date and time, messaging, PDF, unit testing, etc.
Most packages and libraries need to be imported. When working with a Java IDE, the feature IntelliSense does this for you.
We will import our library manually. At the top of the file, before the class declaration, we place the import statement.
There are many Java libraries that provide additional functionality for all different purposes, e.g. networking, databases, Html parsing, e-mail, date and time, messaging, PDF, unit testing, etc.
Most packages and libraries need to be imported. When working with a Java IDE, the feature IntelliSense does this for you.
We will add our library manually, at the top of the file, before the class declaration, we place the import statement.
Import Library
import java.util.Scanner;
public class Output {
Create a new Scanner Object
Create a new Scanner object with the new keyword and pass the System class’ static InputStream field in as an argument. This new object, you assign to a variable of the type Scanner.Scanner input = new Scanner(System.in);
Scan and Store the User Input
Take the variable value from the user.
- Import the class definition
java.util.Scanner;
- Create an object of this class
Scanner input = new Scanner(System.in);
- Store the user input with the scanner method next() into a variable.
The methodnext()
finds and returns the next token from this scanner.
A token is the smallest element of a program that is meaningful to the compiler, i.e. keywords, variables, constants, special characters, operations, etc.
Let’s create a complete procedure that asks for a user name.
- Create a Scanner object.
Scanner inputName = new Scanner(System.in);
- Create an output that asks for the name.
System.out.println(“What is your first name?”);
- Store the user input into a new variable.
String userName = inputName.next();
- Output the stored variable.
System.out.println(“Hi “ + userName + “, how are you?”);
In this lesson, we learned how to store some user input in a variable and return it to the screen. Create your own file Input.java and create a user input on your own.
Get the code for this lesson at GitHub.
The next lesson is about operators, I hope I see you tomorrow.