aspose file tools*
The moose likes Beginning Java and the fly likes Scanner class question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Scanner class question" Watch "Scanner class question" New topic
Author

Scanner class question

Brandt Charles
Ranch Hand

Joined: Apr 17, 2006
Posts: 57

Just curious, I had written a simple program for class that was illustrating constructors, set and get methods. I used the Scanner class to get user input for employee first and last name, and monthly salary. There were 2 employee objects that came from the class, both of which had the previously mentioned attributes.

Using the Scanner class, the user would for employee 1 enter first name, last name, then monthly salary. These were 3 separate steps using an object of the Scanner class called sc. It worked fine for the employee 1 object, but for employee 2 the user would not be given the opportunity to enter the first name, it would display the println string prompting the user to enter the first name for employee 2, but would jump to entering last name for employee 2 and allowing them to enter the last name. The salary for employee 2 could be entered as intended as well. The necessary code for entering the first name for employee 2 was present. Everything looked the way it should have.

The fix I came up with was forming a second Scanner class object for use with the second employee object. It no longer skipped the step to enter employee 2's first name and all is well. Why is this?

Sorry this is so long winded, I can attach the code if it would help.


Green, green, green!
Stephen Foy
Ranch Hand

Joined: Oct 17, 2005
Posts: 143
Best attatching the code


Stephen Foy - Microsoft Application Development Consultant
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38818
    
  23
Please let us see the code.
I got it to work quite easily.
Brandt Charles
Ranch Hand

Joined: Apr 17, 2006
Posts: 57

You asked for it, you got it:


Please forgive the simplicity of the assignment. I was just curious as to why the problem had occured. You will see object sc being used for getting info for the second employee object. When I included a second Scanner class is what resolved it. Please be gentle with how I went about coding this. We were specifically told not to use a GUI. Also, please forgive the formatting in the code section. That's how it pasted over.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38818
    
  23
Use sc.next() for a word rather than nextLine(). Then you can write "Brandt Charles" and have Brandt for 1st name and Charles for 2nd name. You can get 1234.56 on the same line as the salary.

You should be able to get away with one Scanner object only. Try


You have sc2 in, which you don't need. Apart from that, your programming seems to work.

CR
Brandt Charles
Ranch Hand

Joined: Apr 17, 2006
Posts: 57

Lordy what a spaz I am. I looks over the API docs more than once and completely missed the next() method. That's what I get for trying to complete assignements at work.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38818
    
  23
Don't worry about it. We were all beginners once
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Scanner class question