Hi guys.. I have no idea whats going on with my system. I've never worked on it properly before but I keep having problems with getting a user input...
I took this code off another website and tried to solve the problem but nothing... Ok so whats happening is this:
I compile and run fine but instead of getting "Username: " first, I get the first input line which is where I have to type username... So basically everything is one line up... I have to type the password when the program outputs "Username: " and so on..
When i get to the 2 + 2 bit, I just press enter...
When you post code, it is more readable if you enclose it in code tags, and therefore more likely to get an answer.
It is good that you mentioned you hadn't checked it out on Windows yet, though you don't say where you are running it. I have nothing but Windows to check it out on, so I'm going to have to guess at your problem.
It is possible that the 'print' statements do not flush their characters to the output (on your system) until the input is entered. Some text i/o is like that; they wait around for a line feed or something before they go ahead with the line. To test this out, replace "System.out.print" with "System.out.println"; you won't have your prompt and your input on the same line, but it may work closer to what you expected.
You may be able to execute flush() or some similar method on System.out to overcome the problem, also. But before you try that, try the other to see if this IS your problem, because I think the behavior of flush is less consistent across systems than println. At least, I don't know for sure that will fix it; I'm more confident of println.
On Windows, this does work for me, although the cursor is positioned at the beginning of the line for input and jumps to the end when I start typing. I've seen that behavior in other command-line programs.
Joined: Nov 05, 2011
I really don't know whats up with my system, im using linux.. I don't know if that helps.
I checked it on another unix based system and it works fine... without changing anything.. Thanks for the help. I'll try changing to println again and let you know as soon as I can.
Welcome again I have added code tags as previously suggested, and you can see how much better it looks.
I think the problem is that nextLine() doesn’t do what you think it does. Look at this old post, where somebody else had a similar problem, and see whether it helps you. If not, tell us and we’ll try again.
Joined: Nov 05, 2011
I attached a screenshot to show you exactly what i mean... I can type anything in the line after "...2+2 " and it will accept it. And even if I initialize Scanner in a different place, it still seems to give me an input line right where I do so..
I did try the println and what Campbell Ritchie suggested in the other post but neither of these worked.. I will find out more about the flush() that rc mentioned when I get the chance..
Thanks Campbell Ritchie for putting in the code tags. Really cool of you..
What is 2 + 2:
Joined: Oct 13, 2005
Please use copy-and-paste, which is much easier to read than screenshots.
It still looks like a problem with nextLine(), but when I ran it on a bash shell, I got
campbell@xxxxx:~/java> java ScannerDemo
What is 2 + 2: 99
Invalid username or password, access denied!
No changes to your original code. No sign of any errors. Which shell are you using?