What is the right syntax to read an integer from the user in Java?

2024

What is the right syntax to read an integer from the user in Java?

  1. A.

    Scanner sc = new Scanner(System.in); int num = sc.readInt();

  2. B.

    Scanner sc = new Scanner(System.in); int num = sc.inputInt();

  3. C.

    Scanner sc = new Scanner(System.in); int num = sc.nextInt();

  4. D.

    Scanner sc = new Scanner(System.in); int num = sc.getInt();

  5. E.

    Question not attempted

Attempted by 65 students.

Show answer & explanation

Correct answer: C

With a Scanner object, nextInt() is the Java method used to read an integer value from the user.

Explore the full course: Up Lt Grade Assistant Teacher 2025