Hi there,
So I'm new to this forum, so I feel like I shold introduce myself really quick. I'm 19, and a freshman in college, double majoring in Business Administration and Computer Information Science. (Told you it'd be quick)
Alright, so I'm working on a program that reads a CSV files that contain cities, their coordinates, highways that connect to them, and distance/time between them. The idea of this program is to read that file, and print the distance and time between them, the roads that connect them together and the time it takes to travel between them. It sounds simply, but its giving me a lot of trouble (this is my first year of computer science after all). So far I have three classes, a main class, a City class, and a Road class.
Here's the code I have so far:
Main:
package vince_maps1; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import javax.swing.Timer; import javax.swing.JFileChooser; public class Main { JFileChooser fileChooser = new JFileChooser(); public Timer clock = new Timer(100, new ActionListener() { public void actionPerformed(ActionEvent e) { } }); public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { private void loadMapActionPerformed(java.awt.event.ActionEvent evt) { File file = new File("F:\\simpleMap.csv"); try { BufferedReader reader = new BufferedReader(new FileReader(file)); String s; int j = 0; while ((s = reader.readLine()) != null) { String[] strings = s.split(","); City c = new City(strings[0], Integer.parseInt(strings[1]), Integer.parseInt(strings[2])); Road r = new Road(strings[0], strings[1], strings[2], Integer.parseInt(strings[3]), Integer.parseInt(strings[4])); System.out.print(c); } } catch (Exception e) { System.out.println("File Error"); } } public void run() { for (City c : City.cities) { System.out.println(c.name); } } }); } }
City:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package vince_maps1; import java.util.ArrayList; /** * * @author stu696084 */ public class City { public int x, y; public String name; public static ArrayList<City> cities = new ArrayList<City>(); public City(String name, int x, int y) { this.name = name; this.x = x; this.y = y; } public boolean isCity(City c) { if (c.equals(this)) { return true; } return false; } }
Road:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package vince_maps1; /** * * @author stu696084 */ public class Road { public String startCity, endCity; public int length, time; public String name; public Road(String name, String startCity, String endCity, int length, int time) { this.startCity = startCity; this.endCity = endCity; this.length = length; this.time = time; this.name = name; } @Override public String toString() { return (startCity + "and" + endCity); } }
I can't figure out how to take the information the that's read in, and then manipulate it and print it. Any advice would be greatly appreciated!
Thanks,
Wrathgarr
Edit: I thought I should add that the format of the information in the CSV file looks like this:
Delta,108,291,,
Montrose,127,325,,
Gunnison,230,314,,
Grand Junction,64,256,,
,,,,
US 50,Grand Junction,Delta,43,57
US 50,Delta,Montrose,23,30
US 50,Montrose,Gunnison,65,76
While the output should look like this:
Delta is at (108, 291)
US 50 goes to Grand Junction (43 miles, 57 minutes)
US 50 goes to Montrose (23 miles, 30 minutes)
Grand Junction is at (58, 251)
US 50 goes to Delta (43 miles, 57 minutes)
Montrose is at (127, 325)
US 50 goes to Delta (23 miles, 30 minutes)