-
duplicating output
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:
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?
Code:
sleepHour = getDouble("Enter sleep hours (0 - stop: )"); // this is the one that is duplicating..
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..
-
-
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.
-
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!