Java Tutorial
Java User Input
Scanner reads from the keyboard. Pair it with print so the user knows what to type.
Scanner reads tokens
new Scanner(System.in) reads from stdin. nextInt reads an integer. nextDouble reads a decimal. next reads a word. nextLine reads the rest of the line.
Example
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
System.out.println("you typed " + n);
}
}Two values
Call next twice. On StudyGrid, type the numbers in the stdin box, one per line or on one line separated by a space.
Example
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int a = in.nextInt();
int b = in.nextInt();
System.out.println(a + b);
}
}The editor fills sample stdin when it sees Scanner. You can edit the stdin box before Compile & run.
Prompt first
Print a question before you read, so a person at the keyboard knows what to type. println goes to stdout; Scanner reads stdin. They are two different streams.
Example
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Your age: ");
int age = in.nextInt();
System.out.println("Next year you are " + (age + 1));
}
}The nextInt then nextLine trap
Mixing number reads with nextLine catches almost everyone. nextInt() reads the number but leaves the newline behind, so the very next nextLine() returns an empty string instead of waiting for input.
After nextInt() (or nextDouble()), add a bare in.nextLine(); to swallow the leftover newline before you read a full line of text. This is one of the most-searched Java beginner bugs.
Try It Yourself
Exercise: Read two integers and print their product. (In the stdin box, put each number on its own line.)
Show solution
Scanner in = new Scanner(System.in);
int a = in.nextInt();
int b = in.nextInt();
System.out.println(a * b);nextInt skips the whitespace between the two numbers, so a space or a newline between them both work.
Key Takeaways
new Scanner(System.in)reads the keyboard;nextInt/nextDouble/next/nextLineread different pieces.- Print a prompt before you read so the user knows what to type.
- After
nextInt(), callnextLine()once to clear the leftover newline before reading a line.
Worked examples
The short listings above are there so you can see the grammar. The programs here use the same statements on quantities that already have units: a speed, a pH, a count of bases. They are classroom numbers. Air resistance is ignored. g is 9.81 m/s² unless a line says otherwise.
Open them in the Java editor at /java/try. Change one measurement and check whether the result still has the right unit.
Physics
Read two measurements
Scanner reads the next token. Pair a prompt with nextDouble so a lab partner knows what to type.
Example
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
double v = in.nextDouble();
double i = in.nextDouble();
System.out.println("P = " + (v * i) + " W");
}
}