I have a bit of programming experience, mostly through stuff like Godot's GDscript which was very intuitive for me, and Java is a bit scary, with its syntax and all. I've only started yesterday through a programming course I'm following and we did some simple input output things. What I'm confused about is that apparently I have to put an "input.nextLine();" after using input.nextInt() otherwise my program crashes. What? Why?? I'm not even sure how to word my confusion, I'm usually decent at explaining what problem I'm having with stuff, but I just can't wrap my head around why this happens, why it makes sense for this to ever happen, I just don't get what's going on at all. Is this just a problem on Java's side that I have to remember of and work around?
#Using Java for the first time and having trouble understanding how input.nextInt() works and why
1 messages · Page 1 of 1 (latest)
<@&987246399047479336> please have a look, thanks.
Here's an example of something that causes problems:
import java.util.Scanner;
public class DumbFuckingProblem
{
public static void main(String[]args) {
Scanner input = new Scanner(System.in);
int number = input.nextInt();
input.nextLine();
String word = input.nextLine();
int number_from_word = Integer.parseInt(word);
}
}
Detected code, here are some useful tools:
import java.util.Scanner;
public class DumbFuckingProblem {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int number = input.nextInt();
input.nextLine();
String word = input.nextLine();
int number_from_word = Integer.parseInt(word);
}
}
As in
That doesn't cause problems
But if I remove that "input.nextLine();", it does
Here's what happens
God I hate BlueJ, though I'll be stuck with it for a little while longer
I have no clue why that's the line it gives me an error on. What, why
here read this
Mixing any nextXXX method with nextLine from the Scanner class for user input, will not ask you for input again but instead result in an empty line read by nextLine.
To prevent this, when reading user input, always only use nextLine. If you need an int, do
int value = Integer.parseInt(scanner.nextLine());
instead of using nextInt.
Assume the following:
Scanner scanner = new Scanner(System.in);
System.out.println("Enter your age:");
int age = scanner.nextInt();
System.out.println("Enter your name:");
String name = scanner.nextLine();
System.out.println("Hello " + name + ", you are " + age + " years old");
When executing this code, you will be asked to enter an age, suppose you enter 20.
However, the code will not ask you to actually input a name and the output will be:
Hello , you are 20 years old.
The reason why is that when you hit the enter button, your actual input is
20\n
and not just 20. A call to nextInt will now consume the 20 and leave the newline symbol \n in the internal input buffer of System.in. The call to nextLine will now not lead to a new input, since there is still unread input left in System.in. So it will read the \n, leading to an empty input.
So every user input is not only a number, but a full line. As such, it makes much more sense to also use nextLine(), even if reading just an age. The corrected code which works as intended is:
Scanner scanner = new Scanner(System.in);
System.out.println("Enter your age:");
// Now nextLine, not nextInt anymore
int age = Integer.parseInt(scanner.nextLine());
System.out.println("Enter your name:");
String name = scanner.nextLine();
System.out.println("Hello " + name + ", you are " + age + " years old");
The nextXXX methods, such as nextInt can be useful when reading multi-input from a single line. For example when you enter 20 John in a single line.
Oh so the answer is that nextInt sucks lmao
Wait, why does it still crash if I type "10 10"? Based on the last few lines in that paragraph, it should successfully read the first 10 into nextInt and the second 10 into the word that gets used into parseInt
Seems like word isn't equal to "10" but instead it's " 10" instead and I don't know where that space came from. I mean, I'm guessing it's the one I input in between the two tens, but if that gets counted, that's kinda weird
This is what I mean
Man, nextInt fucking sucks, can't say my first approach to Java has been a pleasure
Whatever, this is secondary
Thank you very much for the help
