显示错误的 Java 算法实现



我正在尝试为Dijkstra算法(最短路径树(实现java。正在从包含字符串| 的文本文件中读取图形节点顶点之间的(顶点(和整数(权重(。但是在运行程序时,它给我抛出了一个错误

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 + " tt " + 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和字符串,并且在代码中声明它只得到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。

我的问题是在循环(主空(中更改什么,以使程序工作(读取文本文件,并计算最短路径树(

你在输入上使用 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.
*/
}
}

需要明确的是,我给你的代码只提取你要找的整数,它实际上并没有把它们放在你需要的地方。我希望这是有帮助的。

最新更新