Java Arena
Step 5 of 6

Reading doubles and booleans

Decimal numbers and true-or-false answers are read the same way: read a line, then convert it.

  • Double.valueOf(text) turns text like 2.5 or -0.75 into a double.
  • Boolean.valueOf(text) gives true when the text is true in any mix of capitals (TRUE, True), and false for anything else.
Scanner scanner = new Scanner(System.in);
double height = Double.valueOf(scanner.nextLine());
boolean ready = Boolean.valueOf(scanner.nextLine());
System.out.println("Height: " + height);
System.out.println("Ready: " + ready);
Input
2
yes
Output
Height: 2.0
Ready: false

Two things in that output are worth a second look:

  • The input was 2, but the program prints 2.0. A double is always printed with its decimal part.
  • The input was yes, which isn't true, so the boolean is false.

Your task

Challenge 1 of 3

Ask for a decimal number, read it as a double and print it. For the input 3.75 the program prints:

Give a decimal number:
You gave 3.75

A hidden test types a whole number: as a double, 4 is printed as 4.0.

Challenge 2

Ask whether it's raining, read the answer as a boolean with Boolean.valueOf, and print it. For the input true the program prints:

Is it raining?
Raining: true

Challenge 3

Read a price (a double) and whether the customer is a member (a boolean), in that order, and print both with labels. For the input 12.5 and false the program prints:

Price:
Member?
Price: 12.5
Member: false