Results 1 to 10 of 10
Thread: Simple user input not working
- 04-30-2012, 11:49 AM #1
Senior Member
- Join Date
- Aug 2011
- Posts
- 116
- Rep Power
- 0
Simple user input not working
A seemingly simple task has thrown up a strange error. Ive got a while loop which keeps adding assignments until the user decides they have finished. When i run it, it skips the module title section and just puts the module identifier on the same line, an example output below.
Enter new assignment details
****************************
Module Titile :> Module Identifier :> test
Assignment Titile :> test
Everything else prints out as it should, i dont understand why though as they are all the same . . .
Code is below,
Java Code:boolean add = true; while(add) { System.out.println("\nEnter new assignment details"); System.out.println("****************************"); System.out.print("Module Titile :> "); String mTitle = in.nextLine(); System.out.print("Module Identifier :> "); String mIdentifier = in.nextLine(); System.out.print("Assignment Titile :> "); String aTitle = in.nextLine(); System.out.print("Date Set :> "); String dateSet = in.nextLine(); System.out.print("Due Date :> "); String dueDate = in.nextLine(); System.out.print("Assignment Author :> "); String aAuthor = in.nextLine(); System.out.print("Weighting :> "); String weighting = in.nextLine(); System.out.println("\nEnter another assignment? (y/n)"); String another = in.next(); if(another.equals("n")) add = false; }
- 04-30-2012, 12:57 PM #2
AN21XX
- Join Date
- Mar 2012
- Location
- Munich
- Posts
- 297
- Rep Power
- 2
Re: Simple user input not working
Did you make sure "in" is empty at the beginning? Initialize or clear it and show us the declaration please.
- 04-30-2012, 01:56 PM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: Simple user input not working
Is there anything else happening before this?
For example, is there a "Please choose your action" followed by some integer input?Please do not ask for code as refusal often offends.
- 04-30-2012, 02:14 PM #4
Senior Member
- Join Date
- Aug 2011
- Posts
- 116
- Rep Power
- 0
- 04-30-2012, 02:18 PM #5
AN21XX
- Join Date
- Mar 2012
- Location
- Munich
- Posts
- 297
- Rep Power
- 2
Re: Simple user input not working
I don't like being ignored...
I like likes!.gif)
- 04-30-2012, 02:27 PM #6
Senior Member
- Join Date
- Aug 2011
- Posts
- 116
- Rep Power
- 0
Re: Simple user input not working
Sorry
-.gif)
The scanner is declared at the start of the class Scanner in = new Scanner(System.in);
It is then used in createMenu() to read an int as i said previously. I then use the scanner again in the above code to read a number of strings.
Ive never had to clear the scanner before using it again in similar programs, how would i do that?
- 04-30-2012, 02:36 PM #7
AN21XX
- Join Date
- Mar 2012
- Location
- Munich
- Posts
- 297
- Rep Power
- 2
Re: Simple user input not working
I would say
while (in.hasNext()) in.next();
But this is purely theoretical as I never worked with the scanner before, so I do not know if that is a blocking operation for the user if using a keyboard as input. :)I like likes!.gif)
- 04-30-2012, 06:31 PM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: Simple user input not working
Scanner.nextInt() will result in the newline character being left in the buffer.
This character is then read when you use nextLine(), resulting in an empty String for that read.
This is what is happening here.
Stick a call to nextLine() prior to running this bit of code here (probably just after you've finished with the previous menu would be a good spot). This will have the effect Sierra talks about in their first post of clearing the buffer.Please do not ask for code as refusal often offends.
- 04-30-2012, 06:55 PM #9
Senior Member
- Join Date
- Aug 2011
- Posts
- 116
- Rep Power
- 0
- 05-01-2012, 09:45 AM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Similar Threads
-
User Input???
By jonytek in forum New To JavaReplies: 8Last Post: 01-13-2013, 02:52 PM -
user input on gui
By JoePenguin in forum New To JavaReplies: 5Last Post: 01-26-2012, 07:27 PM -
Scanner user input not working in conjunction with while loop.
By TheNadz in forum New To JavaReplies: 5Last Post: 11-27-2010, 03:49 AM -
User input- Pop Up Box
By dedachi in forum AWT / SwingReplies: 3Last Post: 03-23-2009, 04:47 AM -
Scanner Issues (User Input: Very Simple)
By carlodelmundo in forum New To JavaReplies: 8Last Post: 10-31-2008, 02:44 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks