从文本文件中读取输入 Java

huangapple go评论76阅读模式
英文:

reading input from text file java

问题

以下是翻译后的内容:

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

public class Adjacency_matrix {

    /**
     * @param args the command line arguments
     */
    static int a[][];
    private static boolean[] checks;

    public static void main(String[] args) {
        // TODO code application logic here
        try {
            FileReader f = new FileReader("E:\\dijkstra.txt");
            BufferedReader b = new BufferedReader(f);
            String[] v = b.readLine().split(" ");
            for (int i = 0; i < v.length; i++) {
                int ver = Integer.parseInt(v[0]);
                int edg = Integer.parseInt(v[1]);
                int x = Integer.parseInt(v[2]);
                int test = Integer.parseInt(v[3]);
                a = new int[ver][ver];
            }
            String[] v1 = b.readLine().split(" ");
            for (int i = 0; i < 4; i++) {
                int ver1 = Integer.parseInt(v1[0]);
                int edg1 = Integer.parseInt(v1[1]);
                int w = Integer.parseInt(v1[2]);
                a[ver1][edg1] = w;
            }
            String s;
            while ((s = b.readLine()) != null) {
                StringTokenizer st = new StringTokenizer(s, " ");
                int src = Integer.parseInt(st.nextToken());
                int d = Integer.parseInt(st.nextToken());
            }
            System.out.println("Adjacency Matrix");
            for (int i = 0; i < 4; i++) {
                for (int j = 0; j < 4; j++) {
                    System.out.print(a[i][j]);
                }
                System.out.println();
            }
        } catch (Exception e) {
            System.out.println(e);
        }
    }
}

请注意,这是您提供的代码的翻译部分,我已经按照您的要求进行了翻译,只返回了代码的翻译内容。

英文:

This is my input file:

4 4 3 2   //edges//vertex//must visit node//case
1 2 4// this are the graph node and its a directed weighted grap//
1 3 20
2 3 4 
3 4 4
1 4// source 1 to 4//
2 4 // source  2 to 4//

how to read the input file ?
1.for the first line user give the vertices,edges,a must visit node,case
2.
3.those line are representing graph vertices ,edges and weight between them
4.
5.
using dijkstra algo from source 1 and destination 4
for last two line
6.

7.
i upload the new edited code also facing problem can you please go through it

import java.util.*;
import java.io.*;
public class Adjacency_matrix {
/**
* @param args the command line arguments
*/
static int a[][];
private static boolean[] checks;
public static void main(String[] args) {
// TODO code application logic here
try {
FileReader f = new FileReader(&quot;E:\\dijkstra.txt&quot;);
BufferedReader b = new BufferedReader(f);
//    int[]v=new int[4];
String[] v = b.readLine().split(&quot; &quot;);
for (int i = 0; i &lt; v.length; i++) {
int ver = Integer.parseInt(v[0]);
int edg = Integer.parseInt(v[1]);
int x = Integer.parseInt(v[2]);
int test = Integer.parseInt(v[3]);
a = new int[ver][ver];
}
String[] v1 = b.readLine().split(&quot; &quot;);
for (int i = 0; i &lt; 4; i++) {
int ver1 = Integer.parseInt(v1[0]);
int edg1 = Integer.parseInt(v1[1]);
int w = Integer.parseInt(v1[2]);
a[ver1][edg1] = w;
}
String s;
while ((s = b.readLine()) != null) {
StringTokenizer st = new StringTokenizer(s, &quot; &quot;);
int src = Integer.parseInt(st.nextToken());
int d = Integer.parseInt(st.nextToken());
}
System.out.println(&quot;Adjacency Matrix&quot;);
for (int i = 0; i &lt; 4; i++) {
for (int j = 0; j &lt; 4; j++) {
System.out.print(a[i][j]);
}
System.out.println();
}
} catch (Exception e) {
System.out.println(e);
}
}
}

答案1

得分: 2

  1. 第一行解析了4个元素,但实际只有3个(尝试将catch块更改为e.printStackTrace()以查看程序崩溃的位置)。
  2. 你没有在这段代码中使用e(你自己定义的那个,不是异常)或c。这可能是因为你没有提供完整的代码。
  3. 如果注释掉对c的赋值,你的数组赋值会失败,因为你基于token1token2进行索引赋值,假设数组是从1开始而不是从0开始,因此在输入行3 4 4上会抛出ArrayIndexOutOfBoundsException
  4. 然后它会在下一行失败,因为1 4没有关联的成本,但你的while条件将继续添加图元素,直到没有更多的输入。
英文:
  1. You're parsing 4 elements on the first line when there are only 3 (try changing the catch block to e.printStackTrace() to see where the program crashes).
  2. You're not using e (the one you define, not the exception) or c in this code. That may be because you haven't provided the entire thing.
  3. Commenting out the assignment to c, your array assignment fails because you are assigning indices based on token1 and token2 assuming a 1-based array instead of a 0-based array, thus it will throw ArrayIndexOutOfBoundsException for the input line 3 4 4
  4. It then fails on the next line because there is no cost associated with 1 4, yet your while condition will continue to add graph elements until there is no more input.

huangapple
  • 本文由 发表于 2020年9月5日 02:40:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/63746484.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定