Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9199707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:35:47+00:00 2026-06-17T22:35:47+00:00

I’m in a beginner Java course…and my latest assignment seems super difficult for some

  • 0

I’m in a beginner Java course…and my latest assignment seems super difficult for some reason. The assignment is focused around File I/O. It involves many parts, but this part is what is really confusing me. It involves:

  1. Reading a Sales file where each line is supposed to have first the
    Division (N, S, E or W), then the Quarter (1-4) on the next line,
    then the Sales Amount on the third line and then repeats. (can do
    this ok)
  2. We have to validate that each line has one of the allowable
    options/file types (can do this ok)
  3. Then dump any errors to an error file and ask the user to supply
    another file & repeat until the input file is in the right format.
    (have this working)
  4. Once it is in the right format, we have to be able to generate a
    sales report in the below format (can’t figure out how I am
    supposed to be able to get this collected…)

Sales Report

Total Sales by division:
North $total
South $total
East $total
West $total

Total Sales by quarter:
Jan-Mar $total
Apr-Jun $total
Jul-Sep $total
Oct-Dec $total

Year End Total: $total

One menu option has to be to Validate the file is in the correct format, and then another menu option appears to generate the Sales Report.

Note: We aren’t expected to use arrays yet (that’s the next assignment).

 import java.io.*;
 import java.util.Scanner;

 public class SalesReporting {
    public static void main(String[] args) throws Exception {
    String inputData;
    String divisionRead;
    int qtrRead;
    double salesRead;
    String filePath;
    String menuChoice;
    File fileName;
    PrintWriter outputErrorLog;
    PrintWriter outputSalesReport;
    Scanner keyboard = new Scanner(System.in);
    Scanner inputFile;
    String overwriteFile;
    Boolean isDataFileError;
    Boolean isValidSalesFile = false;
    Boolean isValidMenuChoice;
    Boolean isToOverwrite = false;

    System.out.println("\tHarmway Corporation" +
            "\nQuarterly Sales Report Generator");

    do { 
        do {
            isValidMenuChoice = true;

            // display main menu options to user & prompt for choice
            System.out.print("\n\tMain Menu" +
                "\n" +
                "\n[V]alidate Sales Data");
            // only display generate sales report option if a sales report has been validated
            if (isValidSalesFile) {
                System.out.print("\n[G]enerate Sales Report");
            }
            System.out.print("\nE[x]it program" +
                "\n");

            System.out.print("\nEnter choice: ");

            menuChoice = keyboard.next();

            if (!menuChoice.equalsIgnoreCase("v") &&
                !menuChoice.equalsIgnoreCase("g") &&
                !menuChoice.equalsIgnoreCase("x")) {
                isValidMenuChoice = false;
                System.out.print("\n\t**Error** - Invalid menu item");
            }
        } while (isValidMenuChoice == false);

        if (menuChoice.equalsIgnoreCase("v")) {
            do {
                // prompt user for the sales data file path
                System.out.print("\nSales data file path: ");
                filePath = keyboard.next();

                fileName = new File(filePath);

                // if the file path doesn't exist, error displayed
                if (!fileName.exists()) {
                    System.out.println("\n" + fileName + " not found");
                }
            } while (!fileName.exists());

            // create a scanner for the input file
            inputFile = new Scanner(fileName);

            // create an error log to dump invalid sales data errors to
            fileName = new File("errorlog.txt");
            outputErrorLog = new PrintWriter(fileName);

            // resets boolean to allow for error free data file check
            isDataFileError = false;

            // validate and store the sales data
            while (inputFile.hasNext())
            {
                // first line must be division N, S, E or W to be valid
                inputData = inputFile.next();
                try 
                {
                    divisionRead = inputData;

                    if (isValidDivision(divisionRead)) {
                        System.out.println("ok division");
                    }

                    else 
                    {
                        isDataFileError = true;
                        outputErrorLog.println(divisionRead + ",Invalid division value");
                    }
                } 
                catch (Exception e) 
                {
                    isDataFileError = true;
                    outputErrorLog.println(inputData + ",Invalid division value");
                }

                // if second line is a valid quarter
                inputData = inputFile.next();
                try 
                {
                    qtrRead = Integer.parseInt(inputData);

                    if (isValidQuarter(qtrRead)) {
                        System.out.println("ok quarter");
                    }

                    else
                    {
                        isDataFileError = true;
                        outputErrorLog.println(qtrRead + ",Invalid quarter value");
                    }
                } 
                catch (Exception e) 
                {
                    isDataFileError = true;
                    outputErrorLog.println(inputData + ",Invalid quarter value");
                }

                inputData = inputFile.next();
                try 
                {
                    salesRead = Double.parseDouble(inputData);  

                    if (isValidSales(salesRead)) {
                        System.out.println("ok sales");
                    }

                    else
                    {
                        isDataFileError = true;
                        outputErrorLog.println(salesRead + ",Invalid sales amount value");
                    }
                } 
                catch (Exception e) 
                {
                    isDataFileError = true;
                    outputErrorLog.println(inputData + ",Invalid sales amount value");
                }


            }


            // close the input sales data file
            inputFile.close();

            // close the error log file to write
            outputErrorLog.close();

            if (isDataFileError)
            {
                // there was an error in the sales file; not a valid sales data file
                isValidSalesFile = false;
                System.out.print("\nThe data file contains data errors"
                    + " (See error log for details)");
            }
            else
            {
                // there were no errors; valid sales data file
                isValidSalesFile = true;
                System.out.print("\nThe data file is validated");
            }
        }

        if (menuChoice.equalsIgnoreCase("g")) {
            System.out.println("\ntemp g");

            do {
                // prompt user for file path to save sales report
                System.out.print("\nSave report as: ");
                filePath = keyboard.next();

                // identify file to be used
                fileName = new File(filePath);


                // file already exists; ask whether to overwrite file or not
                if (fileName.exists())
                {
                    do 
                    {
                        System.out.print("\nOverwrite (y/n): ");
                        overwriteFile = keyboard.next();

                        if (overwriteFile.equalsIgnoreCase("y"))
                        {
                            isToOverwrite = true;

                            // create printwriter for the sales data file
                            outputSalesReport = new PrintWriter(filePath);

                            System.out.println("\nGenerating sales report...");



                            // add code to write to the results to the file





                            // close sales report
                            outputSalesReport.close();

                            System.out.println("\nReport generated successfully!"); 
                        }

                    } while (!overwriteFile.equalsIgnoreCase("n") &&
                            !overwriteFile.equalsIgnoreCase("y"));
                }
                // file doesn't already exist; save the file
                else if (!fileName.exists())
                {
                    // create printwriter for the sales data file
                    outputSalesReport = new PrintWriter(filePath);

                    System.out.println("\nGenerating sales report...");



                    // add code to write to the results to the file





                    // close sales report
                    outputSalesReport.close();

                    System.out.println("\nReport generated successfully!");
                }   
            } while (!isToOverwrite);


        }

        if (menuChoice.equalsIgnoreCase("x")) {
            System.out.println("\nProgram has been terminated.");
            System.exit(0);
        }

    } while (!menuChoice.equalsIgnoreCase("x"));

}



public static boolean isValidDivision(String divisionRead) {
    if (divisionRead.equalsIgnoreCase("N") ||
            divisionRead.equalsIgnoreCase("S") ||
            divisionRead.equalsIgnoreCase("E") ||
            divisionRead.equalsIgnoreCase("W")) {
        return true;
    }

    else {
        return false;
    }
}

public static boolean isValidQuarter(int qtrRead) {
    if (qtrRead >= 1 && qtrRead <= 4) {
        return true;
    }

    else {
        return false;
    }
}

public static boolean isValidSales(double salesRead) {
    if (salesRead >= 0) {
        return true;
    }

    else {
        return false;
    }
}
}

I’m not asking for specific code, but just some guidance on how I should best proceed. Any advice would be appreciated. I’ve been stuck on this assignment for over a month!

Example of what the Sales Data file would look like if in a valid format (each would be on its own line…the forum is ignoring the breaks):

N
1
35.50
N
2
26.99
N
3
77.45
N
4
58.30
S
1
132.15
S
2
81.19
S
3
159.06
S
4
83.55
E
1
99.40
E
2
25.39
E
3
50.25
E
4
43.21
W
1
120.89
W
2
392.11
W
3
105.76
W
4
299.95
N
2
66.15
N
3
38.22
N
4
27.66
E
2
135.32
E
3
37.50
E
4
9.10

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T22:35:48+00:00Added an answer on June 17, 2026 at 10:35 pm

    Since you are not dealing (not want to deal ) with arrays or collections, you can consider creating varianles for North, South, East, West, year end total and the 4 quarters update the values of these variables as you iterate through the file.

    Using collection this task would be a little streamlined, but would be prettymuch the same amount of work.

    to generate the actual report you can use the String classes for concatenation such as

    String.format("%s:%s",strDivision, doubleAmount);
    

    which will give you the info in the desired format, this can be extended to address the rest of the report.

    I dont think you need assistance in parsing the file since you say that you are able to validate it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want use html5's new tag to play a wav file (currently only supported
In my XML file chapters tag has more chapter tag.i need to display chapters
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.