← Home

Accepting user input

In order to accept user input from the console, one must first import java.util.Scanner. Then have some statement such as System.out.println("Enter number:"); in order to prompt the user to enter a number. Otherwise they won't know! Then instantiate a new Scanner object by following this rule:

1Scanner myScanner = new Scanner(System.in);

This will store the user's input inside a variable. Let's take a string for example. In this case, we would first, declare a new String:

1String myString;

and secondly, store information inside the variable using:

1myString = myScanner.nextLine();

By letting myString equal the Scanner object .nextLine(), we're telling the scanner input object to give us the entire line that was just typed in by the user and assign/store it in myString.

When placing the dot after myScanner, you should notice Eclipse provides you a bunch of options other than nextLine(). Depending on what kind of input you’d like to accept, you can choose nextInt() to accept an integer input or nextDouble() to accept a double etc.

You can also react to what the user inputs. One way to do this is to use the method hasNextInt(). This is also possible for doubles and floats and booleans etc. Add this to an if statement and you can react to what type of input the user inputted. So, you can create the following:

1if(myScan.hasNextInt()) {
2  System.out.println("Why did you enter an integer????? We wanted a double");
3}

Ex 1 (Computer Science)

Create a method that will continually prompt the user for an integer until a valid integer is entered. That valid integer is a multiple of 13.

Ex 2 (Maths)

Write a program to calculate a person's Body Mass Index (or BMI).

BMI = weight / height²

Ex 3 (Maths)

Following the method created on right-angled triangles, extend this by creating another method which calculates the angle of a right-angled triangle given the lengths of two of its sides and returns this value in degrees.

Things to think about:

Hints

Ex 4 (Games): Guess that number

Use what you have been taught to create the game, Guess that number. The computer will generate a random number and the player will have to guess it till he/she gets it right. After each attempt the computer says whether it's higher or lower until he/she gets it right.

To generate a random number:

1  Random rand = new Random();
2  int randomNum = rand.nextInt(max);
3  // will generate a random number between 0 (inclusive) and max (exclusive)

You will need to import java.util.Random.

Extension

Add a lives element to the game so that the user only has ten or so lives to get it right.

Ex 5 (Games): Hangman

N.B. This exercise is designed to stretch your computational thinking.

Use what you have been taught to create a version of the game Hangman in the console. For the sake of simplicity display the lives as a number, rather than drawing out a pretty diagram, unless you really want to.

Hints

Other Resources

And finally, more on classes →