You call int result = random.nextInt(101) which creates uniformly distributed integers in [0,100], which can take 101 different values. If you check if (result > 49) then you have 51 possible values ([50,100]) and in the else case you have only 50 values ([0,49]). Thus the result is more likely to be in the upper part. To fix it you can do int result = random.nextInt(100).

Answer from Michael Kreutz on Stack Overflow
๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ ref_scanner_nextint.asp
Java Scanner nextInt() Method
Java Examples Java Videos Java ...j.nextInt()); } else { myObj.next(); } } The nextInt() method returns the int value of the number that the next token represents....
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ scanner-nextint-method-in-java-with-examples
Scanner nextInt() method in Java with Examples - GeeksforGeeks
July 11, 2025 - NoSuchElementException: throws if input is exhausted ยท IllegalStateException: throws if this scanner is closed Below programs illustrate the above function: Program 1: ... // Java program to illustrate the // nextInt() method of Scanner class in Java // without parameter import java.util.*; public class GFG1 { public static void main(String[] argv) throws Exception { String s = "Gfg 9 + 6 = 12.0"; // create a new scanner // with the specified String Object Scanner scanner = new Scanner(s); while (scanner.hasNext()) { // if the next is a Int, // print found and the Int if (scanner.hasNextInt()) { System.out.println("Found Int value :" + scanner.nextInt()); } // if no Int is found, // print "Not Found:" and the token else { System.out.println("Not found Int value :" + scanner.next()); } } scanner.close(); } }
Discussions

Java Random.nextInt() behavior - Stack Overflow
Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... I wrote this simple code just out of curiosity and encountered some behavior of the nextInt() method from the Java Random class that I don't quite understand. More on stackoverflow.com
๐ŸŒ stackoverflow.com
java - Understanding Scanner's nextLine(), next(), and nextInt() methods - Stack Overflow
And it may, or may not consume an end-of line, depending on where the end-of-line is. The precise behavior is described by the javadoc, and you probably need to read it carefully for yourself in order that you can fully understand the nuances. ... The nextInt() call consumes the 2 character ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
[Java] What is the use of nextInt() ?
If you ask someone to borrow a knife, would you be satisfied if they instead handed you an entire forge? Sure, you could use the forge to make your own knife, but the forge is not a knife. You can't cut your sandwich with a forge. Same thing here- Random is not the same thing as an int. It is a structure that can be used to create an int, but it is not an int. If you're asking why you have to create a new Random object instead of just calling a single makeInt() function, it's because the pseudo-random number generator needs to be seeded with some initial data to work, and then it needs to keep track of what it's generated to use that to make the next number. More on reddit.com
๐ŸŒ r/learnprogramming
9
0
January 13, 2017
scan.nextInt vs Integer.valueOf(scan.nextLine())
scan.nextInt has a problem that u cannot take string input after athat (idk the cause), its better to use Integer.valueOf(scan.nextLine()) More on reddit.com
๐ŸŒ r/learnjava
23
49
August 22, 2020
๐ŸŒ
CodeGym
codegym.cc โ€บ java blog โ€บ java io & nio โ€บ scanner nextint() method in java
Scanner NextInt() Method in Java
December 25, 2024 - 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 in Java.
๐ŸŒ
CodeGym
codegym.cc โ€บ java blog โ€บ methods in java โ€บ java random nextint() method
Java Random nextInt() Method
October 11, 2023 - int nextInt(int n) โ€” returns the next random value of type int in the range from 0 to n.
๐ŸŒ
Microsoft Learn
learn.microsoft.com โ€บ en-us โ€บ dotnet โ€บ api โ€บ java.util.scanner.nextint
Scanner.NextInt Method (Java.Util) | Microsoft Learn
Scans the next token of the input as an int. [Android.Runtime.Register("nextInt", "(I)I", "")] public int NextInt(int radix);
Find elsewhere
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ java-util-random-nextint-java
Java.util.Random.nextInt() in Java - GeeksforGeeks
March 21, 2025 - The nextInt() method is used to get the random integer values in the range of int.
Top answer
1 of 2
6

nextLine() reads the remainder of the current line even if it is empty.

Correct.

nextInt() reads an integer but does not read the escape sequence "\n".

Correct1.

next() reads the current line but does not read the "\n".

Incorrect. In fact, the next() method reads the next complete token. That may or may not be the rest of the current line. And it may, or may not consume an end-of line, depending on where the end-of-line is. The precise behavior is described by the javadoc, and you probably need to read it carefully for yourself in order that you can fully understand the nuances.

So, in your example:

  1. The nextInt() call consumes the 2 character and leaves the position at the NL.

  2. The next() call skips the NL, consumes H and i, and leaves the cursor at the second NL.

  3. The nextLine() call consumes the rest of the 2nd line; i.e. the NL.


Note that the above assumes that you are using the default delimiter. (As your example does.) If you change the scanner's delimiter by calling one of the useDelimiter(...) methods, you can cause (for example) next() to return newline characters as part of a token.


1 ... except that your terminology is wrong. When the data is being read, it is not an escape sequence. It is an end-of-line sequence that could consist of a CR, a NL or a CR NL depending on the platform. The escape sequences you are talking about are in Java source code, in string and character literals. They may >>represent<< a CR or NL or ... other characters.

2 of 2
4

If your input is 2 hello hi

nextInt() - just read the next token as a int (Here - it is 2) otherwise it give error

next() - Just read the next token, (here - it is hello)

nextline() - It read a line until it gets newline (here - after read previous 2 hello input by nextInt, next; nextline read hi only because after that it finds a newline )

if input is

2 

Hi 

Hello 

nextInt, next is same for above discussion .

In nextline(), it finds newline after completing the input Hi read by next(). So, nextline() stops to read input for getting the newline character.

๐ŸŒ
Reddit
reddit.com โ€บ r/learnprogramming โ€บ [java] what is the use of nextint() ?
[Java] What is the use of nextInt() ? : r/learnprogramming
January 13, 2017 - You're meant to instantiate it once and then take as many numbers from the stream as necessary to do whatever it is you're trying to do. nextInt() is the name of the method for taking the next int from that sequence, but that's just one of a number of methods available.
๐ŸŒ
Oracle
docs.oracle.com โ€บ javase โ€บ 8 โ€บ docs โ€บ api โ€บ java โ€บ util โ€บ Random.html
Random (Java Platform SE 8 )
April 21, 2026 - Returns the next pseudorandom, uniformly distributed int value from this random number generator's sequence. The general contract of nextInt is that one int value is pseudorandomly generated and returned.
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ scanner nextint() java
Scanner nextInt() Java - Scaler Topics
May 4, 2023 - The nextInt() in Java is used to read, scan, or parse the next token of an input as int. Scaler Topics explains the syntax, working of each method along with parameters, return value, and examples.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ java โ€บ util โ€บ random_nextint_inc_exc.htm
java.util.Random.nextInt() Method
The nextInt(int n) method is used to get a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence.
๐ŸŒ
Oracle
docs.oracle.com โ€บ javase โ€บ 8 โ€บ docs โ€บ api โ€บ java โ€บ util โ€บ Scanner.html
Scanner (Java Platform SE 8 )
April 21, 2026 - Returns true if the next token in this scanner's input can be interpreted as an int value in the specified radix using the nextInt() method.
๐ŸŒ
Sololearn
sololearn.com โ€บ en โ€บ Discuss โ€บ 2807018 โ€บ java-scanner-nextint-and-read-nextint
Java scanner.nextInt() and read.nextInt() | Sololearn: Learn to code for FREE!
As far as I learned, Scanner scanner = new Scanner(System.in); is a method to ask the user to give input. And to use this input, I should use int number = scanner.nextInt(); But I also saw a method below from practice. int number = read.nextInt(); What's 'scanner.nextInt' and 'read.nextInt', and what do they do? javascannerread ยท
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 69548968 โ€บ what-does-nextint-do-with-the-scanner-class-in-this-code
java - What does nextInt do with the Scanner class in this code? - Stack Overflow
Copy public static void integerCompute() { Scanner numberScanner = new Scanner(System.in); int x = 0; // Variable to keep track of user input int sum = 0; // Variable to keep track of sum int before; // Variable to check previously entered number do { before = x; System.out.println("Enter a number: "); x = numberScanner.nextInt(); // What does this do?
๐ŸŒ
Reddit
reddit.com โ€บ r/learnprogramming โ€บ [java] how do i force a particular data type when using scanner.nextint()?
[Java] How do I force a particular data type when using ...
September 14, 2023 -

Doing a project for school. This isn't required for the assignment, but it bugs me and I'd like to be able to fix this.

numDonations[i] = input.nextInt();

while (numDonations[i] < 1) {
			System.out.print("There must be at least 1 donation. Please try again: ");
			numDonations[i] = input.nextInt();
			input.nextLine();

This while loop works well enough to ensure the user enters a number greater than 1. But it doesn't prevent the user from entering a character instead, which crashes the program. Is there a way to change the condition to something like

while (numDonations[i] < 1 || numDonations[i] != an integer)

or would this need to be handled in a different way?

As I'm writing this, I think I'm seeing that I wouldn't be able to fix the conditional, because the program crashes before it even gets that far. I'd have to change the input method. Maybe capture a string and somehow parse out the integer? Is that how it's handled in the real world?

๐ŸŒ
Reddit
reddit.com โ€บ r/learnjava โ€บ how is scanner.hasnextint() supposed to work?
How is scanner.hasNextInt() supposed to work? : r/learnjava
January 25, 2021 - The issue is Scanner.hasNextInt() checks to see if the next token in the scanner is an integer, and then Scanner.nextInt() is used to get that value, and removes it from the Scanner's buffer, thus allowing the next int to be read. Inside your final else statement, where you print false, place ...
๐ŸŒ
IncludeHelp
includehelp.com โ€บ java โ€บ scanner-nextint-method-with-example.aspx
Java Scanner nextInt() Method with Example
nextInt() method is used to read the next token of the input as an int value at the implicit radix (rad) of this Scanner.