Results 1 to 4 of 4
  1. #1
    chronoz1300 is offline Member
    Join Date
    Dec 2009
    Posts
    4
    Rep Power
    0

    Default duplicating output

    Java Code:
    public class Ch6SleepStatistics {
     
        private Scanner scanner;
        
        public static void main(String[] args) {
     
            Ch6SleepStatistics prog = new Ch6SleepStatistics();
            prog.start();
        }
     
        public Ch6SleepStatistics() {
     
            scanner = new Scanner(System.in);
        }
     
        public void start() {
     
            double sleepHour,
                   sum = 0;
     
            int cnt = 0;
     
            // enter the dorm name
            System.out.print("Dorm name: ");
            String dorm = scanner.next();
     
            // Loop: get hours of sleep for each resident
            //       until 0 is entered.
            sleepHour = getDouble("Enter sleep hours (0 - stop: )"); // THIS PART
     
            while (sleepHour != 0) {
     
                sum += sleepHour;
                cnt++;
     
                sleepHour = getDouble("Enter sleep hours (0 to stop):"); //DONT MIND THIS PART
     
                if (cnt == 0) {
     
                    System.out.println("No Data Entered");
                }
                else {
     
                    DecimalFormat df = new DecimalFormat("0.00");
                    System.out.println("Averatge sleep time for " + dorm + " is \n\n    " +
                                       df.format(sum/cnt) + " hours.");
                }
            }
        }
     
        private double getDouble(String message) {
     
            double result;
                    
            System.out.print(message);
            result = scanner.nextDouble();
     
            return result;
        }
    }
    OUTPUT:
    Java Code:
    Dorm name: Dorm
    Enter sleep hours (0 - stop: )Enter sleep hours (0 - stop: )  // this should only print once
    how come that it displayed twice?

    Java Code:
    sleepHour = getDouble("Enter sleep hours (0 - stop: )"); //  this is the  one that is duplicating..
    Java Code:
    sleepHour = getDouble("Enter sleep hours (0 - stop:)"); // and when i remove the whitespace BETWEEN the Colon and the closing parenthesis
    when i remove the whitespace BETWEEN the Colon and the closing parenthesis, the program runs fine





    and i've been encountering this problem manytimes.. and i dont know where do this error come from..

    some other fprums said that they have no problems with printing the output....and they made some changes
    regarding with the errors and changes that i've stated,

    but they said that what ever they do, it doesnt duplicate the output..

    im using NetBeans 6.7 IDE..

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

  3. #3
    Joeschmo is offline Member
    Join Date
    Apr 2010
    Posts
    2
    Rep Power
    0

    Default

    I know this thread is old but I just found it on Google looking for an answer to the same problem (duplicate output with Java and NetBeans when using System.out.println()).

    Turns out the problem IS the colon, since you are using NetBeans. I remember reading somewhere that the colon causes some sort of bug-like problem in NetBeans with either output buffering or running programs on multiprocessor machines. I can't remember the exact reason for the problem and I can't find any Internet sources that pinpoint the bug, but this one backs up what I'm saying:

    Java Programming - System.out.print() prints twice?

    Nor can I remember how to avoid the problem and still be able to use colons. Still, you will notice that removing the colon does solve the problem with duplicate output in your (and my) case.

    Sorry I can't be more specific (I'm a Java newbie too) but at least it's something to get other people started with if they Google to this page looking for debugging help on this issue.

  4. #4
    Joeschmo is offline Member
    Join Date
    Apr 2010
    Posts
    2
    Rep Power
    0

    Default

    My bad, I meant System.out.print().

    SYstem.out.Println() clears the buffer so there are no problems with it. And I've already tried flushing the buffer using system.out.flush()...

    Edit: OKay, so after some more testing, I found you can still use the colon with System.out.print(). The catch is you can't have ANYTHING ELSE in the output stream after the colon -- spaces, other characters, etc. If you try something like System.out.print("enter some string:"); it will work just fine,. but if you try something like System.out.print("enter some string: (blah) ");, since there are characters aftert eh colon, it will print twice in netbeans. Try it and see!
    Last edited by Joeschmo; 04-30-2010 at 07:38 PM. Reason: Adding more information instead of making a third post

Similar Threads

  1. Java, output string, getting correct output? HELP!
    By computerboyo in forum New To Java
    Replies: 2
    Last Post: 02-25-2009, 11:44 PM
  2. Output
    By Twiggy in forum New To Java
    Replies: 14
    Last Post: 12-31-2008, 10:03 AM
  3. what the outPut
    By alksam in forum Advanced Java
    Replies: 5
    Last Post: 12-25-2008, 01:44 PM
  4. Sick of duplicating JOptionPane.showMessageDialog
    By gdanelian in forum New To Java
    Replies: 2
    Last Post: 11-18-2008, 05:53 PM
  5. What will be output and why
    By huma in forum Threads and Synchronization
    Replies: 4
    Last Post: 06-26-2008, 10:14 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
  •