This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes I need help with scanner Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "I need help with scanner" Watch "I need help with scanner" New topic
Author

I need help with scanner

Michaela Harrison
Greenhorn

Joined: Nov 05, 2011
Posts: 4
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...

Can someone please help?

I haven't checked this on windows yet.

Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479
Hi. Welcome to the Ranch.

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.

rc
Michaela Harrison
Greenhorn

Joined: Nov 05, 2011
Posts: 4
Hi Rc

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.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38067
    
  22
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.
Michaela Harrison
Greenhorn

Joined: Nov 05, 2011
Posts: 4
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..

admin
Username: secret
Password: 4
What is 2 + 2:
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38067
    
  22
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
Username: Campbell
Password: password
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?
Michaela Harrison
Greenhorn

Joined: Nov 05, 2011
Posts: 4
I am also using bash shell...
 
Consider Paul's rocket mass heater.
 
subject: I need help with scanner
 
Similar Threads
Reading from console
Problem with keyboard input using a Scanner object
how to split a text file
Please help me with this scanner object problem
Weird multiple output issue with if statements