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?

Answer: C. Scanner sc = new Scanner(System.in); int num = sc.nextInt();With a Scanner object, nextInt() is the Java method used to read an integer value from the user.

  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 223 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: Rssb Senior Computer Instructor

Loading lesson…