Back to Top
 
 

Retrieving data from the user

We can keep on writing methods and displaying their results but it's getting rather boring. It would be much more interesting if the computer "talked" to us and reacted accordingly.

The next code snippet is going to let us read the data entered using the keyboard. We are going to ask the user about their name and then greet them.

We are going to use the Scanner class to do it: it is a standard Java class that has methods dedicated just for this.

To not muddle up how this works we are not going to discuss it in detail. Let's see the main ideas of such "communication":

See how the Scanner class works:

What new do you see in the code?

  • earlier there was not import instruction: thanks to we we will be able to use the method to read keyboard input:

import java.util.Scanner;
  • also the instruction below is new:
Scanner scan = new Scanner(System.in);

In general, we create a Scanner class object and use it to call a method reading keyboard input. Notice that declaring an object shares similarities with declaring variables. In this case the type of the object is Scanner; the name of the object: scan. There is, however, a keyword 'new' which is characteristic for structures creating new objects.

  • scan.nextLine() - here the text entered by the user is acquired.

The remaining parts of the code are already familiar. You can also check back in the previous lesson if you don't remember something.

Note: that the value we are taking has the type of String so if you want to assign it to a variable it also needs to be a String type.

We can create something resembling a dialogue with a user, taking turns asking questions and reading their answers.

See an example of the Scanner class:

If instead of String we wanted to get the numerical information to be able to perform calculations on it later, one of the ways to achieve it is by using the scan.nextInt instruction instead of scan.nextLine().

But while nextLine takes any values written by the user, nextInt only takes int type values. In case of a different type the program will throw an error.

 

Tasks

 

Task 1

Ask the user about the current temperature outside. Then print it to the screen. Add a comment based on the temperature:

  • temp <= -15 - print “Stay home :)”
  • temp < 0 and temp > -15 - print “Wear a hat.”
  • temp >= 0 and temp < 15 - print “Nippy but not too bad.”
  • temp >= 15 and temp < 25 - print “Quite nice and warm.”
  • temp >= 25 - print “Grab a cold drink and head for the beach!!!

Go to the first task:

Task 2

Ask the user about the temperature again. Let's try to convert the temperature given by the user from Celsius to Kelvin. To get Kelvin from Celsius degrees you need to add 273 to the value given by the user (it's rounded down, the exact value is 273,15, but let's stick to integers). Print the temperature in Kelvin for the user.

Go to the second task:

Task 3

Ask the user about a number three times; assign the answers to the 'a', 'b' and 'c' variables. Pick the highest number and assign it to the variable 'max'. Print the message: "The highest of the given number is" + max.

Go to the third task: