4 enter values

icon picker
A enter text

enter text values to customise user and device

program requirement

A device like a smart speaker will usually be set up the owner, perhaps with the details like the owner’s name and the device used to control the smart speaker, for example a smartphone or table.
This program should ask the owner to enter and store text values for the owner’s name and personal device to set up a new smart speaker.

learning point

a user can enter data for the program to store by using a keyboard.
a java program must include code to enable keyboard input.
data entry can then happen as a dialog where the user is prompted for input by a message displayed on the console.
when the user types characters as a response they are stored in a variable as a text value
the default for entering and storing a value is text, (numbers and other data types need additional code and will be introduced later in the tutorials).

user interface

Here is an an example of a simple dialog where the user is asked to enter a name.
Please enter your name: Fred
OK Fred, identify your device
understanding the input-output dialog
the text displayed by the computer is in black the user’s response in red
the user types a response after reading a prompt which is a message the program outputs to request input
a good prompt will give the user a helpful description of what to enter

introducing the code

1 Scanner keyboard = new Scanner(System.in);
..
2 String name = "";
3 System.out.print("Please enter your name: ");
4 name = keyboard.nextLine();
5 System.out.println("OK " + name + ", identify your device");
understanding the code
on line 1 the code sets up the program to be able to read what the user types, this setup need only happen once at the start of the program
on line 2 the String variable called name is declared, it will be used to store whatever is entered, text is the default data type for entering values
on line 3 a prompt message is displayed on the console to request the user to enter data
on line 4 the program waits for the user to type in some characters and press the enter key, whatever is typed is then stored into the variable called name as a text value
later, on line 5, the program can use the stored value, for example to continue a dialogue with the user

developer task

Here is the full dialog that the program should create with the user.
Let's set up your profile
Please enter your name: Fred
OK Fred, identify your device
Please enter the type of device: ipad
OK, Fred, repl works just fine on ipad

program code

Loading…

PRIMM


predict
Read the code and assume that the user's name is Duke. Can you anticipate what would appear on the console when the program is run?

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

investigate
edit the prompt to Hi, can your type name in please?
edit the reply from the program to read:
Thanks Duke, now please enter the kind of device you are using:
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 user's name.
Let's set up your profile Please enter your name: Fred
OK Fred, identify your device
Please enter the type of device: ipad
OK, Fred, repl works just fine on ipad

make

can you continue the dialog, perhaps the user could configure the smart speaker with their favourite kind of music?
You can decide how to continue with a different prompt
You will need to create another String variable to store the user’s response
remember to use the code for prompting and reading the user’s response
Hey Duke, please enter your favourite music: electronic
OK Duke, adding eletronic music to your playlist
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.