Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Java (http://www.programmingforums.org/forum17.html)
-   -   Skipping input.nextLine(); (http://www.programmingforums.org/showthread.php?t=10344)

Jakeyman_I_Am Jun 13th, 2006 7:43 PM

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!

Booooze Jun 13th, 2006 7:55 PM

:

the problem is that it skips that line

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

Jakeyman_I_Am Jun 13th, 2006 8:02 PM

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();
}

Jakeyman_I_Am Jun 13th, 2006 8:03 PM

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.. ;)

DaWei Jun 13th, 2006 8:09 PM

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.

Booooze Jun 13th, 2006 8:11 PM

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?

Jakeyman_I_Am Jun 13th, 2006 8:19 PM

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();
}


titaniumdecoy Jun 13th, 2006 8:43 PM

What class is the 'input' variable an instance of?

Jakeyman_I_Am Jun 13th, 2006 10:11 PM

Quote:

Originally Posted by titaniumdecoy
What class is the 'input' variable an instance of?


:

public static Scanner input = new Scanner(System.in);

titaniumdecoy Jun 13th, 2006 10:27 PM

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.


All times are GMT -5. The time now is 7:59 AM.

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