mardi 28 juin 2016

Java File Input/Output Using filereader(), bufferedreader(), fiewriter(), printwriter()

I'm sorry if my post is in the wrong place, has the wrong header, formatted incorrectly etc. It's my first post so I really don't know what the proper etiquette is.

I have this assignment that I just can't figure out. The assignment is to modify the code below to read off of another file (file name SuperMarketIn.dat) using FileReader() and BufferedReader() (I will add the input file below). I am then supposed to take that report and output it to another file named "SuperMarketOut.dat" in a specific format(shown in the picture). The report is supposed to be for hypothetical supermarket manager to keep track of employee hours. I've tried but my program seems get stuck on Monday and doesn't move on to read the hours from the rest of the days. On top of that my total hours come out to be 81 which i have no clue as to why. (Its a beginner programming class that I'm taking which is on par with my skill, hence my confusion)

This is what the input file contains "SuperMarketIn.dat"

Monday 4 Monday 6 Monday 2 Tuesday 3 Tuesday 2 Wednesday 3 Wednesday 5 Wednesday 7 Thursday 5 Friday 4 Friday 3 Friday 4 Saturday 8 Saturday 8 Saturday 8 Sunday 0

End of input file

This Is the Output Format, should output to file "SuperMarketOut.dat" Format of Output

Unedited Source

// SuperMarket.java - This program creates a report that lists weekly hours   worked 
// by employees of a supermarket. The report lists total hours for 
// each day of one week. 
// Input:  Interactive
// Output: Report. 


import javax.swing.*;


public class SuperMarket
{
    public static void main(String args[]) 
    {
        // Declare variables.
        final String HEAD1 = "WEEKLY HOURS WORKED";
        final String DAY_FOOTER = "          Day Total ";  // Leading spaces are intentional.
        final String SENTINEL = "done";     // Named constant for sentinel value. 
        double hoursWorked = 0;             // Current record hours.
        String hoursWorkedString = "";      // String version of hours
    String dayOfWeek;           // Current record day of week.
    double hoursTotal = 0;              // Hours total for a day.
    String prevDay = "";            // Previous day of week.
    boolean done = false;               // loop control

    // Print two blank lines.
    System.out.println(); 
    System.out.println(); 
    // Print heading.
    System.out.println(HEAD1);
    // Print two blank lines.
    System.out.println(); 
    System.out.println(); 

    // Read first record 
    dayOfWeek = JOptionPane.showInputDialog("Enter day of week or done to quit: ");
    if(dayOfWeek.compareTo(SENTINEL) == 0)
        done = true;
    else
    {
        hoursWorkedString = JOptionPane.showInputDialog("Enter hours worked: ");
        hoursWorked = Integer.parseInt(hoursWorkedString); 
        prevDay = dayOfWeek;
    }


    while(done == false)
    {   
        // Implement control break logic here
            // Include work done in the dayChange() method
    }

    System.out.println(DAY_FOOTER + hoursTotal);

    System.exit(0);

} // End of main() method.

} // End of SuperMarket class.

Aucun commentaire:

Enregistrer un commentaire