c_sharp
c_sharp

Reputation: 281

problems with parsing a text file in Java

I have looked at all the links and cannot seem to get what I am looking for. I have a text file I need to read in. First the text file format:

3 STL NY Chi //all on one line
STL NY 575 //on its own line
NY Chi 550 //on its own line
STL Chi 225 //on its own line

I need to read the int into an int variable, say we call it count. Then the actual cities on that same line into an array. The next lines need to read into an array to where the mileage is associated with the array, such as [STL NY]=575. I can only use arrays. No hash tables, list, stacks or queues. Here is what I got so far and honestly it isn't much. I could really use some help for I am pretty stumped on the "howto" on this.

import java.io.*;
import java.util.*;

public class P3 {

/**
 * @param args the command line arguments
 */
public static int count;

public static void main(String[] args) {

    try {

        FileInputStream dataFile = new FileInputStream("Data.txt");
        //BufferedReader br = new BufferedReader(new InputStreamReader(dataFile));

        String line = br.readLine();

    }

    catch (IOException e) {
        System.err.println ("Unable to open file");
        System.exit(-1);
    }
  }
}

I think I'm getting there, but I am getting an error code of: "non-static variable cities cannot be referenced from a static context." I am trying to test my code by printing. Can anyone help me with this printing? I would like to see what is in the arrays to make sure I did it correctly. Here is my code:

package p3;

import java.io.*;
import java.util.*;



class citiesDist {
    String cityOne;
    String cityTwo;
    int miles;
}

class city {
    String cityName;
    int numberLinks;
    citiesDist[] citiesDists;
}

public class P3 {

    city[] cities;

    void initCity(int len) {
        for (int i = 0; i < len; i++) {
            cities[i] = new city();
        }
    }

    void initCitiesDist (int index, int len) {
        for (int i = 0; i < len; i++) {
            cities[index].citiesDists[i] = new citiesDist();
        }
    }

    void parseFile() throws FileNotFoundException, IOException { 
        FileInputStream fstream = new FileInputStream("Data.txt"); 
        DataInputStream in = new DataInputStream(fstream); 
        BufferedReader br = new BufferedReader(new InputStreamReader(in)); 

        int numberCities = Integer.parseInt(br.readLine());
        cities = new city[numberCities];
        initCity(numberCities);

        for (int i = 0; i < numberCities; i++) {
            String line = br.readLine();
            int numberLink = Integer.parseInt(line.split(" ")[1]);
            cities[i].cityName = line.split(" ")[0];
            cities[i].numberLinks = numberLink;
            initCitiesDist (i, numberLink);

            for (int j = 0; j < numberLink; j++){
                line = br.readLine();
                cities[i].citiesDists[j].cityOne = line.split(" ")[0];
                cities[i].citiesDists[j].cityTwo = line.split(" ")[1];
                cities[i].citiesDists[j].miles = Integer.parseInt(line.split(" ")[2]);
            }
        }

    }

    public static void main(String args[]) {
        System.out.println("city" + cities.city);
    }
}

Upvotes: 0

Views: 185

Answers (1)

Makoto
Makoto

Reputation: 106389

If you're ever stumped on code, don't think about the programming language; it only serves to further muddle your logic. (Separate the algorithm from the language.) When you have a clear idea of what you want to accomplish, add your language in (insofar as, "how do I accomplish this particular task?").

Ultimate Goal

From your design, your goal is to have a graph relating the distances between each city. It would appear something like this:

     [STL][NY] [Chi]
[STL][0]  [575][25]
[NY] [575][0]  [550]
[Chi][25] [550][0]

This isn't too terribly difficult to accomplish, in terms of the file input and the Scanner class.

First Steps

You have to extract the dimensions of your graph (which is a 3 by 3). This is provided for you in the first line of your input file. Getting an integer from a Scanner with a File in it isn't too difficult, just make sure you have the proper classes imported, as well as the proper error handling (either try...catch or throwing the exception).

Scanner sc = new Scanner(new File("input.txt"));

You'll need two arrays - one for the cities, and one for the distances themselves. We don't know how large they are (you never assume the data in a file, you just assume the form of the data), so we have to get that from the file itself. Luckily, we are given an integer followed by the cities themselves. We will read this integer once and use it in multiple different locations.

String[] cities = new String[sc.nextInt()];
int[][] distances = new int[cities.length][cities.length];
for(int i = 0; i < cities.length; i++) {
    // Surely there's a method in Scanner that returns String that reads the _next_ token...
}

The Exercise to the Reader

You now have your data structure set up and ready to go. What you would need to do from here is bridge the gap between the cities array and distances array. Consider the order in which they arrived in the file, and the order in which we're encountering them. You would be well-served with some methodology or way to answer the question, 'Which came first - STL or NY?'

Give it a whirl and see if you can get further.

Upvotes: 1

Related Questions