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 9 of 9

Thread: duplicating output

  1. #1
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default duplicating output

    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):");
     
                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:

    Dorm name: Dorm
    Enter sleep hours (0 - stop: )Enter sleep hours (0 - stop: )  // this should only print once
    how come that it displayed twice?



    sleepHour = getDouble("Enter sleep hours (0 - stop: )"); //  this is the  one that is duplicating..

    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


  2. #2
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: duplicating output

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

    some other guys 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..

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: duplicating output

    I didn't get this problem... However, I am getting another issue. You're not calculating the sum and average using the latest data. Instead of using a 1 and a half while loop, I'd suggest using a do-while loop here.

    do
    {
         sleephour = this.getDouble("Enter sleep hours (0 to stop):");
         count++;
         sum += sleephour;
         System.out.println("Your average sleep time is " + (sum/count));
    } while(sleephour != 0);
    I'm using eclipse, and there's nothing in the code that could suggest such an error... maybe you should re-install your IDE and/or Java?

  4. The Following User Says Thank You to helloworld922 For This Useful Post:

    chronoz13 (December 31st, 2009)

  5. #4
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: duplicating output

    oh world tnx for giving a time to check my error... yah some other says that they dont get any problems
    with that... but i dont know why is it happening on my console panel in my IDE...

    ahh world... does it have anything to do with .next() or .nextLine() of String class?
    Last edited by chronoz13; December 31st, 2009 at 11:37 PM.

  6. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: duplicating output

    It might, but I doubt it. If it did, I would have gotten the same results regardless of the IDE because they all use the same JVM. What version of Java do you have?

  7. #6
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: duplicating output

    jdk 6 update 14, w/ Netbeans 6.7

  8. #7
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: duplicating output

    hmm.. have you tried running the program straight from the command line?

  9. #8
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: duplicating output

    i beg your pardon world.. i dont know what do you mean by command line...

    in a cmd output?

  10. #9
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: duplicating output

    oh by the way world, have a look at these,

    public class Sample {
     
        private static enum Option {YES, NO}
        private Scanner scanner;
     
        private int gameNumber;
        private static int DEFAULT_NUMBER = 10;
     
        public static void main(String[] args) {
     
            Sample samp = new Sample();
     
            samp.start();
        }
     
        public Sample() {
     
            scanner = new Scanner(System.in);
        }
        private void start() {
     
            Option opt;
     
            opt = play(); // prompt the user if he wants to play the game
     
            System.out.print("\n");
     
            setTheNumber(); // ask the user if he wants to set the game number
     
            while (opt == Option.YES) {
     
                // this is where the game take place
                // NOT YET IMPLEMENTED
     
                opt = play(); // this will ask the user if he wants to play again
            }
        }
     
        private Option play() {
     
            String input;
     
            Option op;
     
            System.out.print("Play? (Yes - y, No - n): ");
            input = scanner.next();
     
            if (input.equals("Y") || input.equals("y")) {
     
                op = Option.YES;
            }
            else {
     
                op = Option.NO;
            }
     
            return op;
        }
     
        private void setTheNumber() {
     
            String response;
     
            System.out.print("Set The Number: ");
            response = scanner.nextLine();
     
            if (response.equalsIgnoreCase("y")) {
     
                System.out.print("Enter Your Desired Number: ");
                int num = scanner.nextInt();
     
                gameNumber = num;
            }
            else {
     
                System.out.println("Default" + DEFAULT_NUMBER);
                gameNumber = DEFAULT_NUMBER;
            }
        }
    }

    this has a different error.. but someone simply told me to do this
                    System.out.print("Set The Number: ");
    		response = scanner.next(); // its in the part of the .next() method

    he says i have to change that one into this one Otherwise it's going to look for the \n.


    i think there is something to do with the methods of the scanner class, unlike BuffferedReader,
    something about buffer reading... (i dont know much about buffers yet)
    anyway the error of this program is that
    it Skips a block of a statement(in the setTheNumber() method).



    i just want to have some clarification of this kind of erros because i've been encountering this kind of output(errors) since i've been using the
    scanner class, and when i write a complex program with lots of code, im having a hard time looking at the part where this error took place
    Last edited by chronoz13; January 3rd, 2010 at 12:14 AM.

Similar Threads

  1. non well-formed output
    By luca.santaniello in forum Web Frameworks
    Replies: 1
    Last Post: November 19th, 2009, 02:52 AM
  2. how to output using JLabel?
    By qaromi in forum AWT / Java Swing
    Replies: 1
    Last Post: August 30th, 2009, 02:09 PM
  3. OutPut.
    By chronoz13 in forum Java Theory & Questions
    Replies: 3
    Last Post: August 29th, 2009, 10:54 AM
  4. Replies: 3
    Last Post: August 19th, 2009, 11:30 AM
  5. [SOLVED] Java program error in displaying Output
    By crazydeo in forum AWT / Java Swing
    Replies: 9
    Last Post: May 14th, 2008, 10:42 AM