Data
Data

Reputation: 769

Reading and tokenizing a file in Java

I'm trying to figure out how to read some data about a travelling salesman problem from a file. I've included the first few lines of the file (the format is the same for the remaining 13503 lines so I've removed them). The file looks like this:

    NAME : usa12
    COMMENT : Cities with population at least 500 in 
    TYPE : TSP
    DIMENSION : 13509
    EDGE_WEIGHT_TYPE : EUC_2D
    NODE_COORD_SECTION
    1 245552.778 817827.778
    2 247133.333 810905.556
    3 247205.556 810188.889
    4 249238.889 806280.556
    5 250111.111 805152.778
    6 254475.000 804794.444

I'm interested in two things. The dimension value, and the city coordinates. Cities numbered 1,..,6 are shown (but there are 13509 of them), each of their x and y coordinates are adjacent. E.g. City 4 has x=249238.889 and y=806280.556. Basically I'd like to read my file and store the data like this:

int dimension = read dimension of 13509
Coordinate[] xy = create coordinates array, with coordinates of each city

Where a coordinate object is defined like this:

public class Coordinate {
    double x;
    double y;
    public Coordinate(double x, double y) {
        this.x = x;
        this.y = y;
    }
}

I guess I need to use a Buffered Reader, some IO exceptions and a String Tokenizer. I'm new to this, so I'm not really sure how to implement it. I don't know how to specifically read in the dimension value and the x and y coordinates. Does anyone have some suggested implementations?

Upvotes: 0

Views: 278

Answers (1)

Doc
Doc

Reputation: 11651

So here is a basic sample. Will update in case of changes.

import java.util.*;
import java.io.*;
class SO{
    public static void main(String...a)throws Exception{
        System.out.println("Start");


//Read thing
File f = new File("so_data.txt");

Scanner s = new Scanner(f);

int counts = 0;

s.nextLine();//skip 1
s.nextLine();//skip 2
s.nextLine();//skip 3
counts = Integer.parseInt(s.nextLine().split(" ")[2]);//use 4th
s.nextLine();//skip 5
s.nextLine();//skip 6
System.out.println(counts+" : counts");



counts = 6;//DUMMY DATA FOR TEST FILE - REMOVE FOR ACTUAL FILE

Coordinate[] xy = new Coordinate[counts];


int i = 0;

while(i<counts){ // picking exactly the required number of items.

            String line = s.nextLine();
            String[] vals = line.split(" ");

            double x = Double.parseDouble(vals[1]);
            double y = Double.parseDouble(vals[2]);

            Coordinate c =  new Coordinate(x,y);
//          System.out.println(c);
            xy[i++] = c;
        }


for( i = 0;i<xy.length;i++)
            System.out.println("for index "+i+") "+xy[i]);

    }
}
 class Coordinate {
    double x;
    double y;
    public Coordinate(double x, double y) {
        this.x = x;
        this.y = y;
    }
    public String toString(){
        return "Coord:: "+x+" , "+y;
    }
}

so_data.txt

NAME : usa12
COMMENT : Cities with population at least 500 in 
TYPE : TSP
DIMENSION : 13509
EDGE_WEIGHT_TYPE : EUC_2D
NODE_COORD_SECTION
1 245552.778 817827.778
2 247133.333 810905.556
3 247205.556 810188.889
4 249238.889 806280.556
5 250111.111 805152.778
6 254475.000 804794.444

Upvotes: 1

Related Questions