Results 1 to 17 of 17
  1. #1
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    161
    Rep Power
    2

    Question Program Skipping Input

    This method down below is part of a bigger word practicing game :) When you are asked to enter a word however, you don't get the opportunity to do that strangely enough. It just skips that line for some reason, even though it obviously is there. Can you help me with this? Check lines 21 to 25.

    Thanks in advance!

    Java Code:
    	static void createSaves(){
    		Scanner hej = new Scanner(System.in);
    		System.out.print("What do you want to call your glossary? ");
    		String s = hej.nextLine();
    		int wordCount = 0;
    		try{
    			PrintStream ps = new PrintStream(new FileOutputStream("hej.txt", true));
    			ps.println(s);
    			while(true){
    				char c = ' ';
    				s = wordCount == 0 ? "Do you want to add a word? (Y/N): " : "Total words added: " + wordCount + "\nDo you want to add another word? (Y/N): ";
    				while(c != 'Y' && c != 'N'){
    					System.out.print(s);
    					c = hej.next().charAt(0);
    					c = capitalizingLetters(c);
    					if(c != 'Y' && c != 'N')
    						System.out.println("You did not answer the question!");
    				}
    				if(c == 'N')
    					break;
    				System.out.print("Swedish word " + (wordCount+1) + ": ");
    				s = hej.nextLine();
    				ps.print(s + " ");
    				System.out.print("Corresponding English word " + (wordCount+1) + ": ");
    				s = hej.nextLine();
    				ps.print(s + " ");
    				wordCount++;
    			}
    			ps.close();
    		}catch(FileNotFoundException e){
    			System.out.println("Your file could not be found!");
    		}
    	}

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: Program Skipping Input

    The issues surrounding the use of Scanner methods (some read a new line and some don't) have been explained multiple times. Please search.

  3. #3
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    161
    Rep Power
    2

    Default Re: Program Skipping Input

    I did what you said and found out that I had to type "System.out.flush();" after the two "ps.print(s + " ");", but unfortunately it didn't make any difference.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default Re: Program Skipping Input

    Here's a hint: type 'Y' directly followed by a Swedsh word and then press enter (i.e. no enter after the 'Y').

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    161
    Rep Power
    2

    Default Re: Program Skipping Input

    Why the secrecy? Can't you just please tell me what's wrong? xD :)

    Do you mean like this for example "YHello"? Should that somehow enter the Swedish word into the file? Because it doesn't :(

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default Re: Program Skipping Input

    No, I meant 'Y Hello'.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    161
    Rep Power
    2

    Default Re: Program Skipping Input

    Ok! That worked, thanks. How do I allow the user to enter it where it actually is supposed to be entered? Because this is not very user-friendly.

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: Program Skipping Input

    Well if you had done a proper research and or experimentation you would have found that a call to next does NOT consume a carriage return which remains in the buffer. So the next time you call nextLine all it does read the carriage return but there is not text before it because it was already read. Therefore it returns an empty string. The solution you found is not correct. The correct solution involves making another call to a Scanner method to get rid of the carriage return. See if you can figure it out instead of expecting to be spoonfed the solution.

  9. #9
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,049
    Rep Power
    6

    Default Re: Program Skipping Input

    Quote Originally Posted by Zelaine View Post
    Why the secrecy? Can't you just please tell me what's wrong? xD :)
    Jos is a Dutch a-hole. He wants you to learn from this by making you figure it out yourself, he doesn't want to spoonfeed the answer to you so you won't learn anything. I can assure you that not all Dutch people are like that; most won't care about you and just tell you the answer so you go away.

    PS:
    </joke> He's actually a boozing saint.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  10. #10
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,650
    Rep Power
    5

    Default Re: Program Skipping Input

    You know, there should be some read only forum at this site which covers FAQ's on commonly recurring programming issues. As you pointed out in #2, this issue has been covered ad nauseum. Perhaps a blog would suffice.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,036
    Rep Power
    20

    Default Re: Program Skipping Input

    Considering they're using nextLine() everywhere, this bit really stands out:
    Java Code:
    c = hej.next().charAt(0);
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  12. #12
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default Re: Program Skipping Input

    Quote Originally Posted by jim829 View Post
    You know, there should be some read only forum at this site which covers FAQ's on commonly recurring programming issues. As you pointed out in #2, this issue has been covered ad nauseum. Perhaps a blog would suffice.
    Well, this forum does have a blog section; it started off nicely but nowadays it is totally polluted and those crappy entries can't be deleted by an ordinary moderator (as far as I know); but I agree with Junky and you: this topic has been beaten to death more than often but people don't read, so a blog entry or FAQ list would be completely useless ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  13. #13
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,049
    Rep Power
    6

    Default Re: Program Skipping Input

    Yeah but something you can keep bookmarked so you can quickly look up the standardized answer and just post a link to it in stead of having to regurgitate the same answer over and over again...

    A simple external blog would suffice really. But that has management issues attached to it.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  14. #14
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    161
    Rep Power
    2

    Default Re: Program Skipping Input

    Sorry, but I can't figure out what's wrong. You're telling me that I need to call another Scanner method, but since I don't know which one or how it is used, how will I learn anything by just testing? I don't even know where to put the method in the first place.

  15. #15
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    161
    Rep Power
    2

    Default Re: Program Skipping Input

    Never mind... I got it working by putting "hej = new Scanner(System.in);" in front of the input of the Swedish word.

  16. #16
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,650
    Rep Power
    5

    Default Re: Program Skipping Input

    Ok, here is the problem.

    Say you type in a Y and hit a return or enter key. So here is what is in the buffer
    (in this example \n is the result of the enter key, referred to by the
    Scanner API as the line terminator.

    Y\n

    You read the input by doing a next().
    Now the input buffer has

    \n

    in it. So the issue is that some methods comsume the line terminator and other methods do not.
    You need to both read the documentation and try a few
    examples of just scanning input to see how it works. Believe me, it will be worth the effort.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  17. #17
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,036
    Rep Power
    20

    Default Re: Program Skipping Input

    Essentially you should either be using nextLine() throughout, which does read (and dispose of) the new line character, or next(), which disposes of any buffered separator character (in this case a new line) and then reads up to the next one (as jim describes).
    By using both you end up with your current situation with left over new lines.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Skipping code on second run through?
    By Qwertyness_ in forum New To Java
    Replies: 12
    Last Post: 03-11-2013, 02:11 PM
  2. Skipping an Integer
    By BrainMelt in forum New To Java
    Replies: 9
    Last Post: 03-30-2011, 04:54 AM
  3. Skipping Scanner
    By chari_autumn in forum New To Java
    Replies: 2
    Last Post: 01-04-2011, 06:29 AM
  4. Skipping links!!!
    By jvh in forum New To Java
    Replies: 2
    Last Post: 03-04-2010, 12:11 AM
  5. skipping input
    By windie_86 in forum New To Java
    Replies: 1
    Last Post: 02-12-2008, 07:09 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •