英文:
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("E:\\dijkstra.txt");
BufferedReader b = new BufferedReader(f);
// int[]v=new int[4];
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);
}
}
}
答案1
得分: 2
- 第一行解析了4个元素,但实际只有3个(尝试将
catch
块更改为e.printStackTrace()
以查看程序崩溃的位置)。 - 你没有在这段代码中使用
e
(你自己定义的那个,不是异常)或c
。这可能是因为你没有提供完整的代码。 - 如果注释掉对
c
的赋值,你的数组赋值会失败,因为你基于token1
和token2
进行索引赋值,假设数组是从1开始而不是从0开始,因此在输入行3 4 4
上会抛出ArrayIndexOutOfBoundsException
。 - 然后它会在下一行失败,因为
1 4
没有关联的成本,但你的while
条件将继续添加图元素,直到没有更多的输入。
英文:
- You're parsing 4 elements on the first line when there are only 3 (try changing the
catch
block toe.printStackTrace()
to see where the program crashes). - You're not using
e
(the one you define, not the exception) orc
in this code. That may be because you haven't provided the entire thing. - Commenting out the assignment to
c
, your array assignment fails because you are assigning indices based ontoken1
andtoken2
assuming a 1-based array instead of a 0-based array, thus it will throwArrayIndexOutOfBoundsException
for the input line3 4 4
- It then fails on the next line because there is no cost associated with
1 4
, yet yourwhile
condition will continue to add graph elements until there is no more input.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论