我试图为Dijkstra算法(最短路径树)实现java。图形节点是从一个文本文件中读取的,文本文件包含在顶点之间的字符串(顶点)和int (权重)。但是当运行程序时,它会抛出一个错误。
Exception in thread "main" java.util.InputMismatchException
>at java.util.Scanner.throwFor(Unknown Source)
>at java.util.Scanner.next(Unknown Source)
>at java.util.Scanner.nextInt(Unknown Source)
>at java.util.Scanner.nextInt(Unknown Source)
>at ASD4_dijkstra.main(ASD4_dijkstra.java:94)这是java的代码
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class ASD4_dijkstra {
// A utility function to find the vertex with minimum distance value,
// from the set of vertices not yet included in shortest path tree
static final int V = 5;
int minDistance(int dist[], Boolean sptSet[]) {
// Initialize min value
int min = Integer.MAX_VALUE, min_index = -1;
for (int v = 0; v < V; v++)
if (sptSet[v] == false && dist[v] <= min) {
min = dist[v];
min_index = v;
}
return min_index;
}
// A utility function to print the constructed distance array
void printSolution(int dist[], int n) {
System.out.println("Distance from starting vertex");
for (int i = 0; i < V; i++)
System.out.println(i + " \t\t " + dist[i]);
}
// Funtion that implements Dijkstra's single source shortest path
// algorithm for a graph represented using adjacency matrix
// representation
void dijkstra(int graph[][], int src) {
int dist[] = new int[V]; // The output array. dist[i] will hold
// the shortest distance from src to i
// sptSet[i] will true if vertex i is included in shortest
// path tree or shortest distance from src to i is finalized
Boolean sptSet[] = new Boolean[V];
// Initialize all distances as INFINITE and stpSet[] as false
for (int i = 0; i < V; i++) {
dist[i] = Integer.MAX_VALUE;
sptSet[i] = false;
}
// Distance of source vertex from itself is always 0
dist[src] = 0;
// Find shortest path for all vertices
for (int count = 0; count < V - 1; count++) {
// Pick the minimum distance vertex from the set of vertices
// not yet processed. u is always equal to src in first
// iteration.
int u = minDistance(dist, sptSet);
// Mark the picked vertex as processed
sptSet[u] = true;
// Update dist value of the adjacent vertices of the
// picked vertex.
for (int v = 0; v < V; v++)
// Update dist[v] only if is not in sptSet, there is an
// edge from u to v, and total weight of path from src to
// v through u is smaller than current value of dist[v]
if (!sptSet[v] && graph[u][v] != 0 &&
dist[u] != Integer.MAX_VALUE &&
dist[u] + graph[u][v] < dist[v])
dist[v] = dist[u] + graph[u][v];
}
// print the constructed distance array
printSolution(dist, V);
}
// Driver method
public static void main(String[] args) {
File file = new File("C:\\Users\\leotr\\Downloads\\alg4.txt");
try {
Scanner sc = new Scanner(file);
int graph[][] = new int[5][5];
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
graph[i][j] = sc.nextInt();
}
}
sc.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}我在谷歌搜索,发现了错误的原因。这是因为文本文件包含int和string,并且在代码中它声明的仅获得int,但无法理解如何更改代码,以使其工作。
编辑:
文本文件
5// number of nodes into graph
A,B-6,C-1//name of node and connection with other nodes , and her weight
B,A-6,C-3,D-7,E-2
C,A-1,B-3,D-1
D,B-7,C-1,E-2
E,B-2,D-2 文件的第一行是指图中的节点数,其他行表示节点的名称、与其他节点的连接以及连接权重。
例如,A,B-12,C-5表示A与B连接,该连接的权重为12,节点a连接到节点c,权重为5。
我的问题是如何更改循环(主空),使程序工作(读取文本文件,并计算最短路径树)。
发布于 2020-06-16 20:59:12
对输入使用nextInt(),它不仅包含整数,还包含逗号和字母等其他字符。你需要把整数和文本分开。
我这样做的方法是使用String.charAt();
你需要确定每个数字从哪里开始。例如,假设每个节点只能被称为一个1字符长的名称,那么您知道在第5、9、13和17位置有一个整数,特别是您要查找的整数(因为破折号表示为负数)。
因此,要将这些特定整数提取为字符,可以使用String.charAt()并插入数字的索引,这些索引是4的倍数(因为字符串中的位置是4+1的倍数,字符串从索引0开始)。
要获取整数本身,您可以使用String.valueOf()或Integer.parseInt(),在这里您只需插入索引处的字符。
总之,我要这样做的方式如下:
int nodes = Integer.parseInt(sc.nextLine()); //This takes the first line, the number of nodes and converts it to an integer.
for(int i = 0; i < nodes; i++){ //This loop will go through each line
String line = sc.nextLine(); //This extracts each individual line into a string
for(int j = 4; j < line.length; i+=4){
//This goes through each individual line, through the indexes that are multiples of 4, which we established are the integers you're looking for above.
String.valueOf(line.charAt(j)); //This returns each integer inside each line in the input text file.
Integer.parseInt(line.charAt(j)); //This line does the same thing as above.
/*I'm not sure how to implement this into your algorithm, but I've done the integer extracting bit.
* Either of the two lines of code above will give you the integers you're looking for.
*/
}
}要明确的是,我给你的代码只提取了你想要的整数,它实际上并没有把它们放在你需要它们的地方。希望这能帮上忙。
https://stackoverflow.com/questions/62416367
复制相似问题