Java Arena
Step 6 of 6

Mixing input types

A program often reads several kinds of values. The recipe is the same every time: read one line with scanner.nextLine(), then convert it to the type you need.

To get Write
a String scanner.nextLine()
an int Integer.valueOf(scanner.nextLine())
a double Double.valueOf(scanner.nextLine())
a boolean Boolean.valueOf(scanner.nextLine())

Each read takes the next line of input, so the order of the reads has to match the order of the input.

Scanner scanner = new Scanner(System.in);
String item = scanner.nextLine();
int count = Integer.valueOf(scanner.nextLine());
double weight = Double.valueOf(scanner.nextLine());
System.out.println(count + " x " + item + ", " + weight + " kg each");
Input
melon
3
1.25
Output
3 x melon, 1.25 kg each

Your task

Challenge 1 of 3

Read a name and then an age (an int), and print one sentence. For the input Ada and 36 the program prints:

Name:
Age:
Ada is 36 years old.

Challenge 2

Read four values in this order: a word, a whole number, a decimal number and true or false. Print each one with its label. For the input pencil, 12, 0.8 and true the program prints:

Text: pencil
Integer: 12
Double: 0.8
Boolean: true

Challenge 3

This time the input comes in a different order: a decimal number, true or false, a word, then a whole number. Print them in yet another order: the word, the whole number, the decimal number and the boolean. For the input 2.5, false, spoon and 4 the program prints:

spoon
4
2.5
false