从文本文件中提取ArrayList数据

时间:2014-05-25 18:09:51

标签: java arraylist dot

我正在尝试实现读取DOT文件(.txt)的Dijkstra算法并且应该返回一个地图,首先我必须将数据从文本文件提取到ArrayList,所以我为Verticle创建了两个类和边缘的另一个类。

文本文件如下所示:

Digraph {
  A -> B [label="10,90"];
  A -> C [label="8,80"];
  C -> B [label="1,50"];
  B -> D [label="7,60"];
  C -> D [label="6,80"];
  D -> E [label="4,90"];
  E -> F [label="2,130"];
  D -> F [label="5,130"];
  F -> G [label="5,120"];
  G -> H [label="5,100"];
  A [label="A,5"];
  B [label="B,4"];
  C [label="C,3"];
  D [label="D,2"];
  E [label="E,1"];
  F [label="F,6"];
  G [label="G,7"];
  H [label="H,8"];
  }

例如在第1行中,我应该提取出来的" A"和目的地是" B"并且speedLimit为10,距离为90,这与边缘有关。从第11行我应该提取名称和时间,例如在第11行,名称应该是" A"时间应该是5。

这是我前两个简单的类:

VerticlesRep类:

    package lab;

public class VerticesRep {
    private String name;
    private int time;

    public VerticesRep(String name, int time) {
        this.name = name;
        this.time = time;
    }

    public String getName() {
        return name;
    }

    public int getTime() {
        return time;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setTime(int time) {
        this.time = time;
    }

    }

这是我的EdgesRep类:

package lab;
public class EdgesRep {
    private String Source;
    private String Destination;
    private int speedLimit;
    private int distance;

    public EdgesRep(String Source, String Destination, int speedLimit, int distance) {
        this.setSource(Source);
        this.setDestination(Destination);
        this.setSpeedLimit(speedLimit);
        this.setDistance(distance);
    }

    public String getSource() {
        return Source;
    }

    public void setSource(String source) {
        Source = source;
    }

    public String getDestination() {
        return Destination;
    }

    public void setDestination(String destination) {
        Destination = destination;
    }

    public int getSpeedLimit() {
        return speedLimit;
    }

    public void setSpeedLimit(int speedLimit) {
        this.speedLimit = speedLimit;
    }

    public int getDistance() {
        return distance;
    }

    public void setDistance(int distance) {
        this.distance = distance;
    }
    }

这是导航类:

package lab;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

/**
 * The class Navigation finds the shortest (and/or) path between points on a map
 * using the Dijkstra algorithm
 */
public class Navigation {
/**
 * Return codes: -1 if the source is not on the map -2 if the destination is
 * not on the map -3 if both source and destination points are not on the
 * map -4 if no path can be found between source and destination
 */

public static final int SOURCE_NOT_FOUND = -1;
public static final int DESTINATION_NOT_FOUND = -2;
public static final int SOURCE_DESTINATION_NOT_FOUND = -3;
public static final int NO_PATH = -4;

// added variables
private String filename = null; // filename initialization

/**
 * The constructor takes a filename as input, it reads that file and fill
 * the nodes and edges Lists with corresponding node and edge objects
 * 
 * @param filename
 *            name of the file containing the input map
 */
public Navigation(String filename) {
    ArrayList<VerticesRep> Vertex = new ArrayList<VerticesRep>();
    ArrayList<EdgesRep> Edges = new ArrayList<EdgesRep>();

    this.filename = filename;
    try {
        FileReader fr = new FileReader(filename);
        BufferedReader in = new BufferedReader(fr);

        String line = null;
        while ((line = in.readLine()) != null) {
            // here i must fill the ArrayLists with the Information
                            // I need from the text file.
        }

         in.close();
         fr.close();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
}

我试图找出我实际上应该使用什么来填充文本文件中的ArrayList,但.split在我的情况下实际上不起作用。

谢谢。

1 个答案:

答案 0 :(得分:0)

使用正则表达式。 首先导入:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

然后写下这样的东西:

    String line = null;
    Pattern patternEdge = Pattern.compile("\\s*([A-Z])\\s*->\\s*([A-Z])\\s*\\[label=\"(\\d+),(\\d+)\"\\];");
    Pattern patternVertex = Pattern.compile("\\s*([A-Z])\\s*\\[label=\"([A-Z]),(\\d+)\"\\];");

    Matcher m = null;

    while ((line = in.readLine()) != null) {

                   m = patternEdge.matcher(line);

                  if (m.find()) {
                     String source = m.group(1);
                     String destination = m.group(2);
                     int speedLimit = Integer.parseInt(m.group(3));
                     int distance = Integer.parseInt(m.group(4));

                     Edges.add(new EdgesRep(source, destination, speedLimit, distance));
                  }
                  else {

                    m = patternVertex.matcher(line);

                    if (m.find()) {
                      String name = m.group(2);
                      int time = Integer.parseInt(m.group(3));

                      Vertex.add(new VerticesRep(name, time));
                    }
                    else
                      System.err.println("Expression is incorrect. No matches");

                  }
    }

    //for debugging
    for(EdgesRep e : Edges)
      System.out.println(e.getSource() + " " + e.getDestination() + " " + e.getSpeedLimit() + " " + e.getDistance());

    //for debugging
    for(VerticesRep v : Vertex)
      System.out.println(v.getName() + " " + v.getTime());

因为第一行并不意味着你可能只想跳过它... 在这个代码块上,我假设它以A - &gt;开头。 B [label =&#34; 10,90&#34;]; 希望它有所帮助:)