Sorry of the title is a little bit confusing. What I need to do is read a text file with a bunch of cities and states on separate lines like this:
Salem, Oregon
St. George, Utah
Augusta, Maine
Portland, Maine
Jefferson City, Missouri
Kansas City, Missouri
Portland, Oregon
Salt Lake City, Utah
And then make an output from that like this:
Maine: Augusta, Portland
Missouri: Jefferson City, Kansas City
Oregon: Portland, Salem
Utah: Salt Lake City, St. George
I have to get it done in a single method and sent into a multidimensional array or arraylist where the first dimension would be the states and the second dimension would be the corresponding cities.
I assumed the easiest way to go about this would be to make sort-of tokens for each city and state but I have no idea how to go about sorting them properly afterwards. I've gotten as far as creating the tokens and then just re-printing them on to separate lines, which is nothing short of useless.
Here is my current code:
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();
}
}
I really have no idea what I'm doing and my understanding of Java and any programming is extremely limited. I've been at this for way too many hours now. If anyone can help me out I would really appreciate it.
You need to have a list of cities for each state.
So, you will have something like Map<String, List<String>>, and after parsing (i.e. splitting) your input, you look up the right list for your state and put in the city.
At the end, you iterate through your map to print out everything in the right order.
I would suggest using a HashMap that maps each state name to an ArrayList of city names. As you process each input record, retrieve the ArrayList for the state from the HashMap. If it's not there, then this is the first record for the state, so create a new ArrayList and put it in the HashMap under the state name. Assuming that any particular city/state pair occurs only once, you don't need to check for duplicates. If you need it as a multidimensional array at the end, then you can extract all the key/value pairs from the HashMap after everything has been processed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With