User Brian
Brian
Level 41

Scanner nextInt() method in Java

Published in the Java Developer group

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!Scanner nextInt() method in Java - 1
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION