CodeGym/Java Blog/Java IO & NIO/Scanner nextInt() method in Java
Author
Artem Divertitto
Senior Android Developer at United Tech

Scanner nextInt() method in Java

Published in the Java IO & NIO group
members

What is the nextInt() Method in Java?

The nextInt() method scans the next token of the input data as an “int”.
As the name of the class Scanner elaborates, nextInt() method of this class is used to scan or parse the input. The input can be stored either as String, read from a file, real-time data or any System input by the user. This completely depends on the nature and need of your program. Please note that you need to import java.util.Scanner; before using the scanner object.

Example 1

Let’s take our first dive to the basic example.
import java.util.Scanner;

public class TestIntInput {

	public static void checkInt(String testData) {

		System.out.println(testData);

		Scanner scanner = new Scanner(testData);

		while (scanner.hasNext()) {

			if (scanner.hasNextInt()) {
				// calling the nextInt() method
				System.out.println(scanner.nextInt() + "\t\t INT FOUND!");
			} else {
				System.out.println(scanner.next() + "\t\t");
			}
		}
		scanner.close();
		System.out.println();
	}

	public static void main(String[] args) {

		String testData1 = "My 3 years old cat Diana, just gave birth to 5 healthy babies.";
		String testData2 = "The number 1 place to learn Java is CodeGym!";
		String testData3 = "6; 5 4 3. 2 1 !";

		checkInt(testData1);
		checkInt(testData2);
		checkInt(testData3);

	}
}

Output

My 3 years old cat Diana, just gave birth to 5 healthy babies. My 3 INT FOUND! years old cat Diana, just gave birth to 5 INT FOUND! healthy babies. The number 1 place to learn Java is CodeGym! The number 1 INT FOUND! place to learn Java is CodeGym! 6; 5 4 3. 2 1 ! 6; 5 INT FOUND! 4 INT FOUND! 3. 2 INT FOUND! 1 INT FOUND! !

Explanation

One thing to note in the above example in testData3 is that a number needs to be space-separated to be scanned as an individual int. That’s the reason why 6 and 3 are not identified as integers because they are colon and comma-separated respectively.

Example 2

This example uses the System input to scan it as integers.
import java.util.Scanner;

public class TestSystemInput {

	public static void getFinalExamScore() {

		System.out.println("Get Your Final Exam Score!\n");

		int finalScore = 0;
		int totalCourses = 0;
		Scanner scanner = new Scanner(System.in);

		System.out.println("Enter total Courses: ");
		totalCourses = scanner.nextInt();

		for (int i = 0; i < totalCourses; i++) {
			System.out.println("Enter score in course " + (i + 1) + " : ");
			finalScore = finalScore + scanner.nextInt();
		}

		System.out.println("Your final Score = " + finalScore);
		scanner.close();
	}

	public static void main(String[] args) {

		getFinalExamScore();
	}
}

Output

Get Your Final Exam Score! Enter total Courses: 3 Enter score in course 1 : 10 Enter score in course 2 : 15 Enter score in course 3 : 15 Your final Score = 40

Conclusion

That’s a wrap for the nextInt() method by Scanner class in Java. It can be a little overwhelming at first but practise will keep you afloat. Feel free to hop on in case of any ambiguity. We encourage you to play around with different input methods for a better understanding. Happy learning!
Comments (1)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Jesú
Level 14 , Madrid, Spain
13 October 2023, 21:30
package codegym.javasyntaxzero.scannerclass;

import java.util.Scanner;

public class TestSystemInput {

	public static void getFinalExamScore() {

		System.out.println("Get Your Final Exam Score!\n");

		int finalScore = 0;
		int totalCourses = 0;
		Scanner scanner = new Scanner(System.in);

		System.out.println("Enter total Courses: ");
		if (scanner.hasNextInt()) {
			totalCourses = scanner.nextInt();
		} else {
			System.out.println("Your input was: " + scanner.next() + " .unfortunately, that's not a number");
		}

		for (int i = 0; i < totalCourses; i++) {
			System.out.println("Enter score in course " + (i + 1) + " : ");
			if (scanner.hasNextInt()) {
				finalScore += scanner.nextInt();
			} else {
				System.out.println("Dude, your input is " + scanner.next() +
						", that's not a number.");
				return;
			}
		}

		System.out.println("Your final Score = " + finalScore);
		scanner.close();
	}

	public static void main(String[] args) {

		getFinalExamScore();
	}
}