Sorry if my title is confusing.
I have an assignment due that has to do with Java. The goal of the assignment is to create a program that will read an input file of provinces and cities and store the data into a 2D array, one for provinces and one for cities (no duplicates). After that, itt will output it in alphabetic order. If that is confusing here is a sample of what it is supposed to look like.
Sample Input File
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 File
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
Here is my Main class
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
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();
}
}
An here is my Munge class as of now.
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Formatter;
import java.util.Scanner;
public class Munge {
private String inputFileName, outputFileName;
private Scanner inputFile;
private Formatter outputFile;
private int line = 0;
private String[] data;
public Munge(String inFileName, String outFileName) {
this.inputFileName = inFileName;
this.outputFileName = outFileName;
data = new String[100];
}
public void openFiles() throws FileNotFoundException {
File newFile = new File(inputFileName);
}
public void readRecords() {
while (inputFile.hasNext()) {
data[line] = inputFile.nextLine();
System.out.println(data[line]);
line++;
}
}
public void writeRecords() {
for (int i = 0; i < line; i++) {
String done[] = data[i].split(", ");
Arrays.sort(done);
for (int j = 0; j < done.length; j++) {
outputFile.format("%s\r\n", done[j]);
}
}
}
public void closeFiles() {
if (inputFile != null) {
inputFile.close();
}
if (outputFile != null) {
outputFile.close();
}
}
}
My problem is mainly happening when I try to run the program. I keep getting The message that is only suppose to occur when args.length < 2. Why is that so & how can I fix this? Do I set args[0] and args[1] to equal files?
You either don’t provide the two filename arguments when you run the program or provide only one of them. For example:
will set only the first argument (to the string “infile”), the second one is missing