Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Reading CSV files into a program

  1. #1
    Junior Member Wrathgarr's Avatar
    Join Date
    Apr 2010
    Location
    Colorado
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Reading CSV files into a program

    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)
    Last edited by Wrathgarr; April 14th, 2010 at 06:00 PM. Reason: Clarification


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Reading CSV files into a program

    Hello Wrathgarr. Welcome to the forums.

    Can you please attach the .csv file? I will attempt to compile your code and run it...
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  3. The Following User Says Thank You to JavaPF For This Useful Post:

    Wrathgarr (April 21st, 2010)

  4. #3
    Junior Member Wrathgarr's Avatar
    Join Date
    Apr 2010
    Location
    Colorado
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Reading CSV files into a program

    I actually sat down with some people and got it working last night. Apparently I was just over thinking it, ended up being way simpler than I was trying to make it. Thanks though!

Similar Threads

  1. Reading and Writing Text Files
    By kappasig84 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: March 1st, 2010, 07:16 PM
  2. Reading many files using a scanner
    By jayjames90 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: October 22nd, 2009, 04:35 PM
  3. exe files
    By subhvi in forum Java Theory & Questions
    Replies: 17
    Last Post: September 3rd, 2009, 09:43 AM
  4. Reading IEEE float values from files.
    By username9000 in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: June 30th, 2009, 12:56 PM