Results 1 to 4 of 4
Thread: duplicating output
- 12-29-2009, 05:00 AM #1
Member
- Join Date
- Dec 2009
- Posts
- 4
- Rep Power
- 0
duplicating output
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; } }
how come that it displayed twice?Java Code:Dorm name: Dorm Enter sleep hours (0 - stop: )Enter sleep hours (0 - stop: ) // this should only print once
Java Code:sleepHour = getDouble("Enter sleep hours (0 - stop: )"); // this is the one that is duplicating..when i remove the whitespace BETWEEN the Colon and the closing parenthesis, the program runs fineJava Code:sleepHour = getDouble("Enter sleep hours (0 - stop:)"); // and when i remove the whitespace BETWEEN the Colon and the closing parenthesis
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..
-
- 04-30-2010, 07:30 PM #3
Member
- Join Date
- Apr 2010
- Posts
- 2
- Rep Power
- 0
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.
- 04-30-2010, 07:33 PM #4
Member
- Join Date
- Apr 2010
- Posts
- 2
- Rep Power
- 0
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
-
Java, output string, getting correct output? HELP!
By computerboyo in forum New To JavaReplies: 2Last Post: 02-25-2009, 11:44 PM -
Output
By Twiggy in forum New To JavaReplies: 14Last Post: 12-31-2008, 10:03 AM -
what the outPut
By alksam in forum Advanced JavaReplies: 5Last Post: 12-25-2008, 01:44 PM -
Sick of duplicating JOptionPane.showMessageDialog
By gdanelian in forum New To JavaReplies: 2Last Post: 11-18-2008, 05:53 PM -
What will be output and why
By huma in forum Threads and SynchronizationReplies: 4Last Post: 06-26-2008, 10:14 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks