4 enter values

icon picker
B enter number

enter numbers to set up a sleep timer and a volume level

program requirement

Some user preferences on a smart speaker use number values. For example the timer countdown to sleep mode or low power mode and also the default volume level preferred by the user.
This program should ask the owner to enter and store number values for the sleep timer and the default volume level for the smart speaker.

learning point

we remember from the previous task that values entered by a user at the keyboard are read as text or String values.
to read keyboard input and store as a number the keyboard’s text value has to be converted into a proper integer value.
for example typing the keys 1 2 3 would be read as “123” which is a string
java must convert the String value “123” into the integer 123 before storing into an int variable

user interface

Here is an an example of a simple dialog where the user is asked to enter preferences using numbers.
Let's set a timer for sleep mode
Please the number of minutes: 5
Sleep timer minutes is now 5
Let's set a volume level (1-10)
Please enter the volume level: 6
The volume level is now 6

introducing the code

1 int timer = 0;
2 System.out.print("Please the number of minutes ");
3 timer = Integer.parseInt(keyboard.nextLine());
understanding the code
line 1 declares and initialises the integer variable which will be used to store the number entered by the user at the keyboard
line 2 displays a prompt to advise the user to enter a value and describes the value
line 3 reads the text typed typed by the user, converts it into an integer and finally stores the number into the variable called timer.
notice that keyboard.nextLine is nested within the Integer.parseInt() code

developer task

Here is the full dialog that the program should create with the user. Read, copy and re-use the code you already have for entering the timer value.
Let's set a timer for sleep mode
Please the number of minutes: 5
Sleep timer minutes is now 5
Let's set a volume level (1-10)
Please enter the volume level: 6
The volume level is now 6
Follow the PRIMM stages below to develop your code.

program code

Loading…

PRIMM

predict
Read the code and assume that the user wants to set a timer to 5.
Can you anticipate what would appear on the console when the program is run?
only the 5 should be entered, not 5 minutes or 5 mins

run
go ahead and run the program, enter a timer value of your own in response to the prompt, check that the program replies to you with your timer value
run the program again and enter a different timer

investigate
what happens if you do type 5 minutes
you will see an error message NumberFormatException
the unit of measure for a number is never input with the number itself, only the actual digits making up the number

modify
Here is the full dialog that the program should create with the user. Read, copy and re-use the code you already have for entering the timer value.
Let's set a timer for sleep mode
Please the number of minutes: 5
Sleep timer minutes is now 5
Let's set a volume level (1-10)
Please enter the volume level: 6
The volume level is now 6

make

Can you create more dialog to set another numeric preference?
you can decide how to continue with a different prompt
you will need to create another int variable to store the user’s response
remember to use the code for prompting and reading the user’s response
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.