Java Notes
Java programs show information on the screen with System.out and can read text or numbers typed by a user with Scanner.
A program needs two ways to communicate with a person:
- Output means the program shows information, such as a result or instruction, on the screen.
- Input means the person types information and the program reads it.
In a simple console program, Java uses System.out to show output and Scanner to read what the user types.
Show text and values on the screen
You already used System.out.println. Java gives three common output methods. They all show something on the screen; the difference is what happens after the text is printed.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Read this program line by line:
print("Java ")displays the word but keeps the cursor on the same line.println("Basics")immediately addsBasics, then moves to a new line. Together the first two lines showJava Basicson one line.printf(...)formats a value before showing it.%.1fmeans “show a decimal number with one digit after the decimal point.”%nmoves to a new line.
| Method | Use it when | Example |
|---|---|---|
print | More text should continue on the same line | A prompt such as Name: |
println | You want one clear line of output | A result or message |
printf | You need a controlled format | Money, percentage, aligned values |
For most early programs, use println. Use print before waiting for input, and use printf when decimal places matter.
Read what a user types with Scanner
When a program needs a name, age, score, or menu choice, it cannot know the value in advance. Scanner reads the text typed into the console after the user presses Enter.
Before using Scanner, add this line above your class:
import java.util.Scanner;This import tells Java that your program wants to use the ready-made Scanner class. Now run the complete program in a terminal. The live editor cannot ask for interactive input, so this particular example is designed for Command Prompt or PowerShell.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Here is what happens:
new Scanner(System.in)connectsscannerto the keyboard input in the terminal.print("Your name: ")asks the user a question without moving to the next line.scanner.nextLine()waits for the user to type a full line and press Enter. That text is saved inname.scanner.nextInt()waits for a whole number and saves it inlessons.- The final
printlnjoins the entered values into a sentence. scanner.close()closes the input reader because the program has finished using it.
Try entering your own name and a number. The values printed by the program should be the values you typed, not fixed values from the code.
Different Scanner methods read different kinds of input
Choose a Scanner method based on what the user is expected to type.
| User will type... | Method | Example input |
|---|---|---|
| A complete line of text | nextLine() | Asha Patel |
| One whole number | nextInt() | 18 |
| A decimal number | nextDouble() | 82.5 |
| One word without spaces | next() | Java |
| A yes/no value | nextBoolean() | true |
Use nextLine() for names and sentences because it keeps spaces. next() would read only Asha from Asha Patel.
Why nextInt() and nextLine() can confuse beginners
nextInt() reads the number, but it leaves the Enter key press waiting in the input. If nextLine() comes immediately after it, that method reads the leftover Enter instead of waiting for new text.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
The extra scanner.nextLine() after nextInt() removes that leftover Enter. Then the next nextLine() can wait for the feedback sentence. Whenever you read a number and then need a full line of text, use this pattern.
Format decimals with printf
printf is useful when a value should always have a fixed number of decimal places.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
%s is replaced by text. %.2f is replaced by a decimal number rounded for display to two digits after the decimal point. %% prints one percent sign. %n moves to a new line. This does not change the original average variable; it only controls how Java displays it.
Common input mistake
If a program uses nextInt() and the person types ten instead of 10, Java cannot turn that word into a number. The program stops with an input error.
For now, enter the kind of value the prompt asks for. In a later lesson, you will learn validation so a program can show a friendly message instead of stopping.
Try it yourself
Create a program named AverageMarks. Ask the user for their name and two marks. Calculate the average, then print the learner's name and average with two decimal places.
Hint
Use nextLine() for the name, nextDouble() for each mark, and (firstMark + secondMark) / 2 for the average. Use printf with %.2f to display it.
One working answer
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Run this in a terminal, enter different marks, and check whether the printed average changes correctly.
Continue the lesson path