Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 13th, 2006, 7:43 PM   #1
Jakeyman_I_Am
Newbie
 
Join Date: Mar 2005
Posts: 14
Rep Power: 0 Jakeyman_I_Am is on a distinguished road
Question Skipping input.nextLine();

Hello! I have a issue here, and to be honest I have seen this beofre, but I cannot remember how I corrected it. The problem is in this line:

Names[custNum] = input.nextLine();

the problem is that it skips that line.. completely. That is the second input line in the constructor, and is followed by a system.out.println(); line. if you need more ask! Thanks!
__________________
The elder newb.
Jakeyman_I_Am is offline   Reply With Quote
Old Jun 13th, 2006, 7:55 PM   #2
Booooze
Expert Programmer
 
Booooze's Avatar
 
Join Date: Mar 2006
Location: Igloo
Posts: 710
Rep Power: 3 Booooze is on a distinguished road
Send a message via MSN to Booooze
the problem is that it skips that line

Obviously, that line isn't excuting to skip itself. Post more of the code.
Booooze is offline   Reply With Quote
Old Jun 13th, 2006, 8:02 PM   #3
Jakeyman_I_Am
Newbie
 
Join Date: Mar 2005
Posts: 14
Rep Power: 0 Jakeyman_I_Am is on a distinguished road
Quote:
Originally Posted by Booooze
the problem is that it skips that line

Obviously, that line isn't excuting to skip itself. Post more of the code.

public static void saveBalance()
{
System.out.println("-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");

System.out.print("Please tell me, what is the ID of the customer?");
custNum = input.nextInt();

System.out.println("Thank you!");
System.out.print("Now please tell me this customer's name?(" + custNum + "):");
Names[custNum] = input.nextLine();

System.out.println(" ");

System.out.println("Thank You!");
System.out.print("Now please tell me " + Names[custNum] + "'s balance: ");
Balance[custNum] = input.nextDouble();

System.out.println("-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");

control();
}
__________________
The elder newb.
Jakeyman_I_Am is offline   Reply With Quote
Old Jun 13th, 2006, 8:03 PM   #4
Jakeyman_I_Am
Newbie
 
Join Date: Mar 2005
Posts: 14
Rep Power: 0 Jakeyman_I_Am is on a distinguished road
Also, further down in the program, the code skips the very same type line. (A input.nextLine) And I do remeber this is class being a topic my teacher covered. I just don't remember what he said..
__________________
The elder newb.
Jakeyman_I_Am is offline   Reply With Quote
Old Jun 13th, 2006, 8:09 PM   #5
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Read the forum's rules/FAQ. You can learn about code tags there, and discover other things regarding the community which you wish to use as a resource. It's only the neighborly thing to do.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jun 13th, 2006, 8:11 PM   #6
Booooze
Expert Programmer
 
Booooze's Avatar
 
Join Date: Mar 2006
Location: Igloo
Posts: 710
Rep Power: 3 Booooze is on a distinguished road
Send a message via MSN to Booooze
Ok, first off a couple things:

#1 - put your code in code tags if you want anyone to help. (honestly)
#2- I'm looking at this page: http://www.cs.wisc.edu/~cs302/io/JavaIO_Scanner.html
Are you sure your defining custNum properly? I think when you get the user input, its going to be a string. You will need to convert it. Also where is it defined? Im not big on Java, but hope this helped. Last thing, are you getting any errors when you execute the code?
Booooze is offline   Reply With Quote
Old Jun 13th, 2006, 8:19 PM   #7
Jakeyman_I_Am
Newbie
 
Join Date: Mar 2005
Posts: 14
Rep Power: 0 Jakeyman_I_Am is on a distinguished road
I am sorry. I come from a forum with no such rules. Anyways here is the code again, and I put a comment in where it skipps. Also, if I output the custNum var (declared and initialized above this) just after where it is inputted, it does come out correctly. And no, I get no errors! Thanks again, sorry about before!


public static void saveBalance()
{
System.out.println("-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");

System.out.print("Please tell me, what is the ID of the customer?");
custNum = input.nextInt();

System.out.println("Thank you!");
System.out.print("Now please tell me this customer's name?(" + custNum + "):");
Names[custNum] = input.nextLine();  //<----- Skips This!!

System.out.println(" ");

System.out.println("Thank You!");
System.out.print("Now please tell me " + Names[custNum] + "'s balance: ");
Balance[custNum] = input.nextDouble();

System.out.println("-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");

control();
}
__________________
The elder newb.
Jakeyman_I_Am is offline   Reply With Quote
Old Jun 13th, 2006, 8:43 PM   #8
titaniumdecoy
Programming Guru
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 1,007
Rep Power: 5 titaniumdecoy will become famous soon enough
Send a message via AIM to titaniumdecoy
What class is the 'input' variable an instance of?
titaniumdecoy is online now   Reply With Quote
Old Jun 13th, 2006, 10:11 PM   #9
Jakeyman_I_Am
Newbie
 
Join Date: Mar 2005
Posts: 14
Rep Power: 0 Jakeyman_I_Am is on a distinguished road
Quote:
Originally Posted by titaniumdecoy
What class is the 'input' variable an instance of?

public static Scanner input = new Scanner(System.in);
__________________
The elder newb.
Jakeyman_I_Am is offline   Reply With Quote
Old Jun 13th, 2006, 10:27 PM   #10
titaniumdecoy
Programming Guru
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 1,007
Rep Power: 5 titaniumdecoy will become famous soon enough
Send a message via AIM to titaniumdecoy
Check out the documentation for the Scanner class. According to the docs, the nextLine() function:

Quote:
Advances this scanner past the current line and returns the input that was skipped. This method returns the rest of the current line, excluding any line separator at the end. The position is set to the beginning of the next line.
See if replacing nextLine() with next() helps.
titaniumdecoy is online now   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:35 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC