-1

I have this homework assignment where i have to input a text file into my java program. the file is a "bed sensor", and tells you if the person is in a deep sleep, a restless sleep, or interrupted sleep(awake), with 0s, 1s, and 2s respectively. each line has a 0, 1, or 2 and there are 86,400 lines (one line for each second of the day).

I have figured out most of the assignment but one part i cannot figure out how to code.

My problem is i have to figure out when the person falls asleep and then output "Sleep time: (answer) hours after midnight".

i've been using counters and if statements and i would like to continue along this path if possible. i've attached my code.. and i feel like this should be pretty simple to figure out based on what i've already done... i just cannot wrap my head around it. i would appreciate any help or advice. thanks

public static void main(String[] args) {
    try {
        File sleepDataFile = new File("/Users/homeWork3/sleep_data.csv");
        Scanner sleepData = new Scanner(sleepDataFile); // scans the data from the file into this java program
        double totalSecondsCounter = 0, wakeCounter = 0, timeAwakeCounter = 0, timeAsleepCounter = 0, deepSleepCounter = 0, restlessSleepCounter = 0, interruptedSleepCounter = 0;
        double wakeUpTime = 0, sleepTime = 0;
        double sleepQuality = 0;

        boolean inSleep = false;

        while (sleepData.hasNextLine()) // this loop writes data to java as long as there is a next line
        {
            String data = sleepData.nextLine(); //  converts the file data to strings
            double val = Double.parseDouble(data); //   changes string type to double type
            totalSecondsCounter++;

            if (inSleep == true) {
                timeAsleepCounter++;
            }
            if (inSleep == false) {
                timeAwakeCounter++;
            }
            if (val == 0) //deep sleep
            {
                deepSleepCounter++;
                inSleep = true;
                wakeCounter = 0;

            }
            if (val == 1) //restless sleep
            {
                restlessSleepCounter++;
                inSleep = true;
                wakeCounter = 0;
            }
            if (val == 2) // interrupted sleep / awake
            {
                wakeCounter++;
                inSleep = false;
            }
            if (val == 2 && wakeCounter < 1800) {
                interruptedSleepCounter++;
                inSleep = true;
            }
            if (val == 2 && wakeCounter > 1800) {
                inSleep = false;
            }
            if (wakeCounter == 1800) // 1800 seconds = 30 minutes. counter is set for 30 min of interrupted sleep.
            {
                wakeUpTime = totalSecondsCounter;
            }
            if (val != 2) {

            }

        }
        sleepData.close();


        System.out.println("Sleep Report for 24 hour period.");
        System.out.println("----------------------------------------------------------");
        System.out.println("Wake Time: \t\t\t\t" + wakeUpTime / 60 / 60 + "\t hours after midnight");
        System.out.println("Sleep Time: \t\t\t\t" + sleepTime / 60 / 60 + "\t\t\t hours after midnight");
        System.out.println("Duration of Deep Sleep: \t\t" + deepSleepCounter / 60 / 60 + "\t hours");
        System.out.println("Duration of Restless Sleep: \t\t" + restlessSleepCounter / 60 / 60 + "\t hours");
        System.out.println("Duration of Interrupted Sleep: \t\t" + interruptedSleepCounter / 60 / 60 + "\t hours");

    } catch (FileNotFoundException e) {
        System.out.println("An error occurred.");
        e.printStackTrace();
    }
}
Barak
  • 1,390
  • 15
  • 27
  • What do you know about your data? During the whole day, will there be exactly one switch between being awake and sleep and exactly one switch vice versa? – kalabalik Mar 08 '20 at 00:05

1 Answers1

1

I'll just give you a (comprehensive) hint, since this is a homework question.

My problem is i have to figure out when the person falls asleep and then output "Sleep time: (answer) hours after midnight".

First of all, you're going to want to initialize two variables before your while loop:

  1. Some boolean flag variable hasFallenAsleepYet
  2. Some double variable fallenAsleepTime

Like so:

boolean hasFallenAsleepYet = false;
double fallenAsleepTime;

Now, inside of your while loop, as you're iterating through sleepData, you're going to want to count the time taken until the person falls asleep, then stop counting.

if(!hasFallenAsleepYet) {
    if(val == someInteger || val == someOtherInteger){ // some condition to tell if the person is asleep
        hasFallenAsleepYet = true;
        fallenAsleepTime = totalSecondsCounter;
    }
}

I'll leave you to figure out what the numbers someInteger and someOtherInteger are, but you should be able to figure it out quickly. They key takeaway here is that you need to initialize some flag variable, so that you stop keeping track of fallenAsleepTime after a certain condition is met.

FrosThrone
  • 11
  • 2