Hello, I'm in my first year in software development program and need help with the following program. I've spent hours working on it. Any help is greatly appreciated.

Write a java program that will open a file, read each of the records from the file, use that data to populate a two dimensional array and write the sorted data out to a new text file.
Define a class named Munge which will do all the 'heavy lifting' in this program.
Build your project in NetBeans using the Run - Build Project, or Run - Clean and Build Project menu item.
From the dist directory you will execute your program from the command line with two filenames as arguments, input file name and output file name. For example:
java -jar lab5.jar ..\input.txt ..\output.txt
The input file will be a plain text file with one city name followed by a comma, followed by a single space, followed by the name of the province. The input file will have no more than 15 cities from one province so you can hard-code that value into your program when you are defining the size of your array. Some provinces may not exist in the data. The input file could be empty and your program should still run fine. Hand in all the input files you used to show how your data design helped to thoroughly test your software.
The output file will consist of the name of a province (assuming it has at least one city) followed by a colon, followed by a space a then a list of the cities from that province sorted alphabetically. The list of cities is comma separated. Notice the last city does not have a comma after it.
The provinces are sorted west to east followed by the three territories also from west to east.
The data must be stored in a two dimensional array of java Strings. Index 0 will represent the array of city Strings from British Columbia, index 1 will represent the array of city Strings from Alberta ... index 11 will represent the array of city Strings from Nunavut. By structuring it this way the provinces are already sorted.
When opening the files, catch the following exceptions: FileNotFoundException and SecurityException. Exit the program cleanly with an informative message to the user if these occur.
When reading records from the input file catch a NoSuchElementException and exit your program cleanly with an informative error message if this occurs.
When writing data to the output file catch a FormatterClosedException and exit your program cleanly with an informative error message if this occurs.

sample input
Hamilton, Ontario
Montreal, Quebec
Vancouver, British Columbia
Sarnia, Ontario
Sherbrooke, Quebec
Winnipeg, Manitoba
Red Deer, Alberta
Edmonton, Alberta
Niagara Falls, Ontario
Port Elgin, Ontario
Victoria, British Columbia
Truro, Nova Scotia
Regina, Saskatchewan
Kingston, Ontario
Fredricton, New Brunswick
Grand Prarie, Alberta
Calgary, Alberta
Collingwood, Ontario

sample output
British Columbia: Vancouver, Victoria
Alberta: Calgary, Edmonton, Grand Prarie, Red Deer
Saskatchewan: Regina
Manitoba: Winnipeg
Ontario: Collingwood, Hamilton, Kingston, Niagara Falls, Port Elgin, Sarnia
Quebec: Montreal, Sherbrooke
New Brunswick: Fredricton
Nova Scotia: Truro

well, if you've spent hours working on it, no doubt you have some code to show us?
show us what you have, tell us what it's doing (wrong) and add any error messages you get.

This is what I've got so far, I'm not sure if what I'm doing is correct:

import java.io.File;
import java.util.Scanner;
import java.util.Formatter;
import java.io.FileNotFoundException;
import java.util.Arrays;

public class Munge
{

    private String inFileName, outFileName;
    private Scanner inFile;
    private Formatter outFile;
    private int line = 0;

    private String[] data;

    public Munge(String inFileName, String outFileName)
    {
        this.inFileName = inFileName;
        this.outFileName = outFileName;

        data = new String[100];
    }

    public void openFiles()
    {
        try
        {
            inFile = new Scanner(new File(inFileName));
        }
        catch(FileNotFoundException exception)
        {
            System.err.println("File not found.");
            System.exit(1);
        }
        catch(SecurityException exception)
        {
            System.err.println("You do not have access to this file.");
            System.exit(1);
        }

        try
        {
            outFile = new Formatter(outFileName);
        }
        catch(FileNotFoundException exception)
        {
            System.err.println("File not found.");
            System.exit(1);
        }
        catch(SecurityException exception)
        {
            System.err.println("You do not have access to this file.");
            System.exit(1);
        }
    }

    public void readRecords()
    {
        while(inFile.hasNext())
        {
            data[line] = inFile.nextLine();
            System.out.println(data[line]);
            line++;
        }
    }

    public void writeRecords()
    {
        for(int i = 0; i < line; i++)
        {
            String tokens[] = data[i].split(", ");
            Arrays.sort(tokens);

            for(int j = 0; j < tokens.length; j++)
                outFile.format("%s\r\n", tokens[j]);
        }
    }

    public void closeFiles()
    {
        if(inFile != null)
            inFile.close();

        if(outFile != null)
            outFile.close();
    }
}

Edited 4 Years Ago by Ezzaral: Fixed code formatting. Please be sure to format your code when posting.

Can you explain what your problem is? What happens when you execute the program?
If there are errors, please post the full text of the error messages.

There is no error in the code jus that i can't get it to open the file, and sort the data. this is what my main class looks like:

public static void main(String[] args)
{
if(args.length < 2)
{
System.err.println("Usage: java -jar lab5.jar infile outfile");
System.exit(99);
}

Munge dataSorter = new Munge(args[0], args[1]);

dataSorter.openFiles();
dataSorter.readRecords();
dataSorter.writeRecords();
dataSorter.closeFiles();
}

How do you know it is not openning the files? Does the code execute all the methods?
What is printed out when the program is executed? Add some println statements to show when each method starts execution so you'll know that the method was called.
If nothing is printed by any of the printlns then you know that statement was not executed and you need to look at the code to see why it was not executed.

the only thing being printed is: "Usage: java -jar lab5.jar infile outfile
Java Result: 99

that's because you are not providing command line parameters, which you can't when you run your application in netbeans. this is very basic knowledge, someone smart enough to use the command line arguments would/should know that.

just check this part of your code:

if(args.length < 2)
{
System.err.println("Usage: java -jar lab5.jar infile outfile");
System.exit(99);
}

so, is it really so hard to understand when those lines are printed?

Edited 4 Years Ago by stultuske

For testing you could hardcode an assignment statement to give args a value:
args = new String[]{"inputfilename", "outputfilename"};
Then you would not need to pass args on the command line while you are testing.
When done testing, remove that assignments statement.

wow ... getting flagged 'Bad post' because I assume a Jave developer understands what

if ( booleanExpression ){
    runThisCode;
}

means, and knows when 'runThisCode' is actually ran? if you really feel that my post is 'bad' at least take the effort to inform me of 'what' is bad about it. I can hardly try to improve towards my next posts without this, now can I?

so, what was 'bad'?
the information provided? my attitude? the fact that I assume developers are smart enough to think/google before posting here?

I see some very decent code up there, especially if written by a beginner. very decent exception handling and all that. excuse me if I become a bit sceptic if it turns out that the 'author' doesn't know what an if structure does, or is completely unaware of command line arguments (what is usually taught before going over to Object Oriƫnted design).

I was just trying to move the OP to show that he actually did write the code, and understands it, since the community rules include "show some effort". copy-pasting a class, IMHO is NOT the same as showing an effort. yes, I am aware of the fact that I might be wrong in thinking the OP might not write the code him(her)self, but then I can expect some basic knowledge about Java and the use of Google, right?

For starters I don't think you understand my point. I'm half way through getting the program runnig, I'm just stuck on few points but I think I can figure it out. And I tried finding clues on google before posting here.

This article has been dead for over six months. Start a new discussion instead.