Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: system.out prints twice in a small program

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default system.out prints twice in a small program

    hi friends

    i am new to java and started a week back

    even in a simple program shown below

    system.out prints twice i am using netbeans ide

    it prints as bellows

    press a key : a
    press a key : press a key :

    public class DoWhile {
     
        /**
         * @param args the command line arguments
         * @throws java.io.IOException
         */
        public static void main(String[] args)
                throws java.io.IOException
        {
            char ch;
            do {
            System.out.print("press a key : ");
            ch = (char) System.in.read();
            }
            while(ch != 'q');
            }
     
    }

    kindly help
    Last edited by vinaysagar; March 11th, 2014 at 12:47 PM. Reason: missing parts


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: system.out prints twice in a small program

    You have a do while, so it's going to print out at least once. It prints out, then it reads in a single byte, which is a. Notice how after you pushed a, you also pressed enter (the newline character), so that's still "in" System.in.

    It checks whether a is equal to q, and since it's not, it goes through again. It prints out the message, and then reads in the newline character you left in there. It then checks whether the newline character is equal to q, and since it's not, it goes through yet again.

    You should probably be using the Scanner class.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Mar 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: system.out prints twice in a small program

    Quote Originally Posted by KevinWorkman View Post
    You have a do while, so it's going to print out at least once. It prints out, then it reads in a single byte, which is a. Notice how after you pushed a, you also pressed enter (the newline character), so that's still "in" System.in.

    It checks whether a is equal to q, and since it's not, it goes through again. It prints out the message, and then reads in the newline character you left in there. It then checks whether the newline character is equal to q, and since it's not, it goes through yet again.

    You should probably be using the Scanner class.
    thanx Kevin for the reply

    another thanx in anticipation

    can you change that part in the program and let me know

    as i am totally new i am doing it as said in the book and dont know how to use scanner

    thanx once again

    regards

    Vinaysagar

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: system.out prints twice in a small program

    Refer to the Scanner API page and the Java IO tutorial.

  5. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: system.out prints twice in a small program

    Quote Originally Posted by vinaysagar View Post
    as i am totally new i am doing it as said in the book and dont know how to use scanner
    Don't be afraid to google your problem. This is the second result for googling "Java Scanner": Scanning (The Java™ Tutorials > Essential Classes > Basic I/O)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Replies: 11
    Last Post: March 10th, 2013, 07:40 PM
  2. Simple question about a small program
    By azizmaiden in forum Java Theory & Questions
    Replies: 5
    Last Post: March 2nd, 2013, 05:52 PM
  3. when i run the program to find the y values it always prints y2 instead
    By jamesR in forum What's Wrong With My Code?
    Replies: 7
    Last Post: September 30th, 2012, 08:38 AM
  4. Game for Small Children Program
    By Javazoid in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 29th, 2012, 03:02 PM
  5. A program that reads in secounds, and prints out hours minutes
    By CYKO in forum Java Theory & Questions
    Replies: 1
    Last Post: September 13th, 2009, 10:42 PM

Tags for this Thread