Lesson 3 of 34

Reading Input with Scanner

8 min 20 XP

We read values from the user with a Scanner. First write import java.util.Scanner; at the top. Type the values in the Input box before you press Run.

Example: edit me and press Run

Output Loading Java (first time takes a few seconds)
Press Run to see the result here.

nextInt() reads a whole number, nextDouble() a decimal, next() one word (up to a space) and nextLine() a whole line. Values separated by spaces or new lines are read one at a time.

Example: edit me and press Run

Output Loading Java (first time takes a few seconds)
Press Run to see the result here.

Writing "Sum = " + a + b prints 1230! Put (a + b) in brackets so the numbers are added first.

Challenge

+20 XP

Read two whole numbers and print their product. Input: 6 7 → Output: 42

Output Loading Java (first time takes a few seconds)
Press Run to see the result here.