Java - 从文件中读取值?

时间:2016-03-16 21:33:25

标签: java

所以我试图构建一个图并找到两个对象之间的最短路径。要构建图形,我正在从输入文件中读取名称和值:

Location1 0 0 0 
Location2 5 0 0 
Location3 5 5 0 
Location4 0 5 0 

我的问题是如何阅读这些值?这是我正在为我的主要方法做的事情:

   public static void main(String[] args) {
      // You can test your program with something like this.
      In in = new In( args[0] );
      int T = in.readInt();
      for (int t=1; t<=T; t++) {
         System.out.println("Case " + t + ":") ;
         Edge w = new Edge( in );
      }
   }

然后在另一个找到每个位置坐标的方法中,将每个点存储在哈希表中,然后转到下一行:

public In coordinates(in){
    while (in.hasNext()){
        String location = in.next();
        String point1 = args[1];
        String point2 = args[2];
        String point3 = args[3];
    }
}

我无法弄清楚的是我如何将这些值添加到has表中,然后始终将位置与这些坐标相关联。我想找到使用Floyd - Warshall算法的最短路径,我可以这样做:

for (int k = 0; k < n; k++) {
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            dist[i][j] = Math.min(dist[i][j], dist[i][k] + dist[k][j]);
        }
    }
}

我希望这些值进入名为dist的二维数组,但我不知道如何将这些值分配给数组。

2 个答案:

答案 0 :(得分:0)

您可以使用bufferedReader和文件阅读器

来完成
File file = new File(replace with file location);
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
while ((line = br.readLine()) != null) {
    //process line will be in the format of a String
}
br.close();

行将等于文件中当前行的行。它将继续读取文件直到文件结束

答案 1 :(得分:0)

我建议您制作一个自定义对象说Location,然后使用HashMap保留链接信息。

阅读完整输入并准备好HashMap后,您可以迭代它并进行计算。

请参阅以下代码段:

位置分类:

public class Location 
{
    private String location;
    private int point1;
    private int point2;
    private int point3;

    public Location(String location, int point1, int point2, int point3) {
        this.location = location;
        this.point1 = point1;
        this.point2 = point2;
        this.point3 = point3;
    }

    public void setLocation(String location) {
        this.location= location;
    }

    public String getLocation() {
        return this.location;
    }

    public void setPoint1(int point1) {
        this.point1 = point1;
    }

    public int getPoint1() {
        return this.point1;
    }

    public void setPoint2(int point2) {
        this.point2 = point2;
    }

    public int getPoint2() {
        return this.point2;
    }

    public void setPoint3(int point3) {
        this.point3 = point3;
    }

    public int getPoint3() {
        return this.point3;
    }
}

主要类别:

public class Foo
{
    public static void main (String[] args)
    {
        Foo f = new Foo(); 
        f.run();
    }

    private void run() {
        Map<String,Location> locationMap = new HashMap<>();
        Scanner in = new Scanner(System.in);
        String line = null;
        while(in.hasNext()) {
            line = in.nextLine();
            String[] split = line.split(" ");
            locationMap.put(split[0],new Location(split[0],Integer.parseInt(split[1]),
                            Integer.parseInt(split[2]),Integer.parseInt(split[3])));
        }

        /* Iterate locationMap and do your calculations */
    }
}

这里我使用System.in来读取控制台的输入。您可以替换它以从任何所需的文件中获取输入。