Results 1 to 7 of 7
  1. #1
    Gaud is offline Member
    Join Date
    Jan 2015
    Posts
    4
    Rep Power
    0

    Default Do-While loop does a strange thing

    Hello everyone
    I do not think anyone has posted a similar problem, so here I go with mine.

    My code is:
    Java Code:
    // Demonstrates the do-while loop.
    
    class DoWhileDemo {
    	public static void main(String arg[]) throws java.io.IOException {
    		
    		char ch;
    		
    		do {
    			System.out.print("Press a key followed by ENTER: ");
    			ch = (char) System.in.read();				// get a char
    		}while(ch != 'q');
    	}
    }
    But the output is:
    Java Code:
    Press a key followed by ENTER: c
    Press a key followed by ENTER: Press a key followed by ENTER: Press a key followed by ENTER: q
    The string appears too many times as you can see above. Why? I only want it to appear once when I press a key followed by enter.
    Regards from a beginner programmer

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Do-While loop does a strange thing

    You're reading in a character at a time.
    The ENTER key counts as 1 or more characters (depending on the OS).

    Add:
    Java Code:
    System.out.println("Character #" + ch + "#");
    after reading in the character and you'll see.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Gaud is offline Member
    Join Date
    Jan 2015
    Posts
    4
    Rep Power
    0

    Default Re: Do-While loop does a strange thing

    Do you mean:
    Java Code:
    // Demonstrates the do-while loop.
    
    class DoWhileDemo {
    	public static void main(String arg[]) throws java.io.IOException {
    		
    		char ch;
    		
    		do {
    			System.out.print("Press a key followed by ENTER: ");
    			ch = (char) System.in.read();								// get a char
    			System.out.println("Character #" + ch + "#");
    		}while(ch != 'q');
    	}
    }
    But then the output is:
    Java Code:
    Press a key followed by ENTER: a
    Character #a#
    Press a key followed by ENTER: Character #
    #
    Press a key followed by ENTER: Character #
    #
    Press a key followed by ENTER: q
    Character #q#
    Not what I wanted :(

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Do-While loop does a strange thing

    It would be best to use the Scanner class. You can still read a character but there are other methods which let you read the rest of the input buffer to eliminate the trailing line delimiters.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    Gaud is offline Member
    Join Date
    Jan 2015
    Posts
    4
    Rep Power
    0

    Default Re: Do-While loop does a strange thing

    Quote Originally Posted by jim829 View Post
    It would be best to use the Scanner class. You can still read a character but there are other methods which let you read the rest of the input buffer to eliminate the trailing line delimiters.

    Regards,
    Jim
    Juhuu
    I got it working using the Scanner class. Thank you.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Do-While loop does a strange thing

    Quote Originally Posted by Gaud View Post
    Do you mean:
    Java Code:
    // Demonstrates the do-while loop.
    
    class DoWhileDemo {
    	public static void main(String arg[]) throws java.io.IOException {
    		
    		char ch;
    		
    		do {
    			System.out.print("Press a key followed by ENTER: ");
    			ch = (char) System.in.read();								// get a char
    			System.out.println("Character #" + ch + "#");
    		}while(ch != 'q');
    	}
    }
    But then the output is:
    Java Code:
    Press a key followed by ENTER: a
    Character #a#
    Press a key followed by ENTER: Character #
    #
    Press a key followed by ENTER: Character #
    #
    Press a key followed by ENTER: q
    Character #q#
    Not what I wanted :(
    I know it's not what you wanted.
    I wasn't giving you a solution, I was showing you what was happening.

    That's how we learn.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    Gaud is offline Member
    Join Date
    Jan 2015
    Posts
    4
    Rep Power
    0

    Default Re: Do-While loop does a strange thing

    Quote Originally Posted by Tolls View Post
    I know it's not what you wanted.
    I wasn't giving you a solution, I was showing you what was happening.

    That's how we learn.
    Yep. I discovered what you meant right after I posted that. Sometimes I am a fool

Similar Threads

  1. Need help with this whole thing :(
    By cvt in forum New To Java
    Replies: 2
    Last Post: 10-17-2012, 08:33 PM
  2. Strange for loop
    By Grimmjow in forum New To Java
    Replies: 5
    Last Post: 01-23-2011, 10:52 PM
  3. Replies: 7
    Last Post: 01-17-2011, 07:02 AM
  4. Explanation of Nested Loop (very strange)
    By Jonotron in forum New To Java
    Replies: 5
    Last Post: 01-09-2011, 02:54 AM

Posting Permissions

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