Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

.useDelimiter(System.getProperty("line.separator"));

843789Dec 19 2009 — edited Dec 19 2009
a simple exercise on the book that im reading
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);
        scanner.useDelimiter(System.getProperty("line.separator"));  // THE PROGRAM RUNS FINE WITHOUT THIS?? why...?
    }

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

        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;
    }
}
as you can see i put a comment on the line that makes the error..
the problem is (i think) the loop is looping forever... try to run it first.. please..


im relieved that i found the error statement ... but i want to know why because it came from the book ( i want to be clarified) .. and i never
encounter any programs in my school using this method of the System properties with input routines.

the whole code that you see above were all came from the book that im reading ryt now.. no more no less (exactly as it is)..
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 16 2010
Added on Dec 19 2009
1 comment
608 views