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).
Java Random.nextInt() behavior - Stack Overflow
java - Understanding Scanner's nextLine(), next(), and nextInt() methods - Stack Overflow
[Java] What is the use of nextInt() ?
scan.nextInt vs Integer.valueOf(scan.nextLine())
Videos
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).
you are testing 51 possibilities for a positive outcome and only 50 possibilities for a negative outcome.
- 100-50 = 51 possibilities
- 0-49 = 50 possibilities.
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:
The nextInt() call consumes the
2character and leaves the position at theNL.The next() call skips the
NL, consumesHandi, and leaves the cursor at the second NL.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.
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.
I've been doing mooc.fi java course for a couple of weeks now and I noticed that every time they want an user to input a number, they use
int number = Integer.valueOf(scan.nextLine());
but I was curious what other parameters I can use with the scanner and I found out I can just type
int number = scan.nextInt();
which seems to be doing exactly the same thing. Is there any reason why would I want to use the longer one?
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?