Sitemap
Ali Said Ark

Ali Said Ark

Java Reminder: Scanner

2 min readSep 11, 2022

Java Reminder article series has been created for users who have a basic command of Java and want to repeat/remember all of the information that they have.

Scanner Class in Java is a class that we use when we want to get value from the user. Since I think that this subject can be best understood through examples, I will explain it through examples. Creating Scanner Class is one of three steps we take to get value from the user. The first step we need to do when getting a value from the user is to create an object from the Scanner Class.

Press enter or click to view image in full size
Scanner scan=new Scanner(System.in);

We now have a scanner object. This object will have the properties of a Scanner from the Scanner Class. After the first step, the user can actually enter the value we want, but if we do not direct the user and display the value in the console or on the app, it will not be very useful in terms of usability. We can inform and direct the user.

System.out.println("Please enter your name");    
String name=scan.next();
System.out.println("Your Name:"+name);

In the first line, we ask the user what we want. This actually has nothing to do with Scanner, but it is very useful to guide users on what we want from them.

Get Ali Said Ark’s stories in your inbox

Join Medium for free to get updates from this writer.

In the second line, we will assign the value that the user will enter to a data and save it so that we can use it later.

In the last line, we see that we can use the value we receive from the user as we want.

Let’s add a few more things to our code and explain:


System.out.println("Please enter your last name");

String lastName=scan.next();

System.out.println("Your Name " + name + " " + "Your Last Name" + lastName);

Actually, we repeated the previous two lines, we asked for a value from the user then we saved it. Now in the last line, you can see that we used two values that we took from the user are used together.

--

--