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/nextLine read different pieces.
  • Print a prompt before you read so the user knows what to type.
  • After nextInt(), call nextLine() 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");
  }
}

FAQ: Java User Input

Common questions about this page.

What is the StudyGrid Java tutorial?

The StudyGrid Java tutorial is a full beginner-to-advanced track: syntax, types, input, loops, methods, classes, collections, generics, maps, and lambdas. Each chapter has copy-and-run examples.

Should I run java scanner examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn java scanner in this Java Java lesson (Java User Input).

Is the Java editor the same as Try Python or Try C++?

No. Try Java compiles with javac at /java/try and shows stdout plus compiler messages. Try Python stays at /try. Try C++ stays at /cpp/try. Java lessons never open those editors.

Do I need to install a JDK to learn Java?

No. Open a chapter, click Try it in Java, and compile in the browser. You can also download a .java file and compile locally with javac.

Where should I start the Java tutorial?

Start at Java Intro, then Get Started and Syntax. After the first program, continue to output, variables, and if-else. After classes, open Java Examples, then generics, map, and lambdas. Use Next at the bottom of each chapter.

Is the Java tutorial free?

Yes. The Java workshop on StudyGrid (studygrid.in) is free: dashboard, chapters, and the compile-and-run editor.