Exception in thread "main" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at HelloWorld.main(HelloWorld.java:169)
当我针对示例输入运行代码时,会继续弹出这些错误。我们给出了一个带有多边形的文档,并且必须使用kruskal的算法并构建一个最小的生成树来找到每个岛的最短距离而不创建一个循环。如果有人可以帮助或提供如何摆脱这些错误的建议,这将是伟大的!我不明白如何在字符串“”....
上有一个numberformatexceptionimport java.util.Scanner;
import java.util.ArrayList;
import java.io.File;
import java.io.IOException;
public class HelloWorld {
static class Point {
int x = 0;
int y = 0;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public String toString() {
return "(x: " + x + " y: " + y + ")";
}
}
static class Polygon {
int numberOfVertices = 0;
ArrayList<Point> points = new ArrayList<Point>();
public Polygon(int numberOfVertices, ArrayList<Point> points) {
this.numberOfVertices = numberOfVertices;
this.points = points;
}
public String toString() {
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < points.size(); i++) {
Point point = this.points.get(i);
stringBuilder.append(point);
}
return stringBuilder.toString();
}
}
static class PQItem implements Comparable<PQItem> {
int node1;
int node2;
double edge;
public PQItem(int node1, int node2, double edge) {
this.node1 = node1;
this.node2 = node2;
this.edge = edge;
}
public int compareTo(PQItem T) {
if (edge - T.edge < 0)
return 1;
else if (edge - T.edge > 0)
return -1;
else
return 0;
}
}
public static void BuildMinimalSpanningTree(int numberOfIslands, ArrayList<Polygon> polygons) {
PriorityQueue q = new PriorityQueue((numberOfIslands * numberOfIslands) / 2);
PQItem Temp;
int[] CheckPad = new int[numberOfIslands];
int FootPrint = 0;
int counter = 0;
double length = 0;
for (int i = 0; i < polygons.size(); i++)
for (int j = 0; j < polygons.size(); j++) {
Temp = new PQItem(i, j, ShortestDistance(polygons.get(i), polygons.get(j)));
}
for (int i = 0; i < polygons.size(); i++)
CheckPad[i] = -1 - i;
while (counter < polygons.size() - 1) {
Temp = (PQItem) q.Remove();
for (int i = 0; i < polygons.size(); i++)
for (int j = 0; j < polygons.size(); j++)
if (CheckPad[Temp.node1] != CheckPad[Temp.node2]) {
if (CheckPad[Temp.node1] < 0 && CheckPad[Temp.node2] < 0) {
CheckPad[Temp.node1] = FootPrint;
CheckPad[Temp.node2] = FootPrint;
FootPrint = FootPrint + 1;
}
else if (CheckPad[Temp.node1] < 0) {
CheckPad[Temp.node1] = CheckPad[Temp.node2];
}
else if (CheckPad[Temp.node2] < 0) {
CheckPad[Temp.node2] = CheckPad[Temp.node1];
}
else {
if (CheckPad[Temp.node1] < CheckPad[Temp.node2]) {
for (i = 0; i < polygons.size(); i++) {
if (CheckPad[i] == CheckPad[Temp.node2])
CheckPad[i] = CheckPad[Temp.node2];
else
for (j = 0; j < polygons.size(); j++)
if (CheckPad[j] == CheckPad[Temp.node2])
CheckPad[j] = CheckPad[Temp.node2];
}
}
System.out.println(Temp.edge);
length += Temp.edge;
counter++;
}
}
}
}
static double ShortestDistance(Polygon polygon1, Polygon polygon2) {
double shortestdistance = 0;
double Temporary = 0;
for (int i = 0; i < polygon1.numberOfVertices; i++)
for (int j = 0; j < polygon2.numberOfVertices; j++) {
Temporary = Math.pow(polygon1.points.get(i).x - polygon2.points.get(j).x, 2)
+ Math.pow(polygon1.points.get(i).y - polygon2.points.get(j).y, 2);
if (Temporary < shortestdistance)
shortestdistance = Temporary;
}
return Math.sqrt(shortestdistance);
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Please enter the name of the file");
File file = new File(scanner.nextLine());
try {
Scanner fileScanner = new Scanner(file);
int numberOfIslands = Integer.parseInt(fileScanner.nextLine());
ArrayList<Polygon> polygons = new ArrayList<Polygon>();
while (fileScanner.hasNext()) {
String line = fileScanner.nextLine();
String[] numbers = line.split(" ");
ArrayList<Point> points = new ArrayList<Point>();
// PQItem NewItem = new PQItem(Node1, Node2, edge);
// info.Insert(NewItem);
for (int i = 1; i < numbers.length; i += 2) {
Point point = new Point(Integer.parseInt(numbers[i]), Integer.parseInt(numbers[(i + 1)]));
points.add(point);
}
// build tree
Polygon polygon = new Polygon(points.size(), points);
polygons.add(polygon);
// BuildMinSpanTree(numberOfIslands, polygons);
}
for (int i = 0; i < polygons.size(); i++) {
Polygon polygon = polygons.get(i);
System.out.println(polygon);
}
int minimalInterconnect = 0;
int totalLength = 0;
System.out.printf("The minimal interconnect consists of %d bridges with a total length of %d",
minimalInterconnect, totalLength);
} catch (IOException e) {
System.out.println(e);
}
}
这是示例程序 3 4 0 0 0 1 1 1 1 0 4 2 0 2 1 3 1 3 0 3 4 0 5 0 5 1
public class PriorityQueue {
private Comparable[] HeapArray;
int Last, Limit;
PriorityQueue
public PriorityQueue(int Capacity) {
HeapArray = new Comparable[Capacity + 1];
Last = 0;
Limit = Capacity;
return;
}
// end constructor
public PriorityQueue() {
HeapArray = new Comparable[101];
Last = 0;
Limit = 100;
return;
}
// end constructor
public void Insert(Comparable PQI) {
if (Last == Limit) {
System.out.println("Priority Queue Overflow!");
System.exit(0);
}
// end if
HeapArray[++Last] = PQI;
this.UpHeap(Last);
return;
}
// end public method Insert
private void UpHeap(int k) {
Comparable V;
V = HeapArray[k];
while (k > 1 && HeapArray[k / 2].compareTo(V) < 0) {
HeapArray[k] = HeapArray[k / 2];
k = k / 2;
}
// end while
HeapArray[k] = V;
return;
}
// end private method UpHeap
public Comparable Remove() {
Comparable PQI;
if (Last == 0) {
System.out.println("Priority Queue Underflow!");
System.exit(0);
}
// end if
PQI = HeapArray[1];
HeapArray[1] = HeapArray[Last--];
this.DownHeap(1);
return PQI;
}
// end public method Remove
private void DownHeap(int k) {
Comparable V;
int j;
V = HeapArray[k];
while (k <= Last / 2) {
j = k + k;
if (j < Last && HeapArray[j].compareTo(HeapArray[j + 1]) < 0)
j++;
// end if
if (V.compareTo(HeapArray[j]) >= 0)
break;
// end if
HeapArray[k] = HeapArray[j];
k = j;
}
// end while
HeapArray[k] = V;
return;
}
// end private method DownHeap
public boolean IsEmpty() {
if (Last == 0)
return true;
else
return false;
// end if
}
// end public method IsEmpty
public boolean IsFull() {
if (Last == Limit)
return true;
else
return false;
// end if
}
// end public method IsFull
public int Length() {
return Last;
}
// end public method Length
} //结束类PriorityQueue
答案 0 :(得分:1)
Exception in thread "main" java.lang.NumberFormatException: For input string: ""
这是自我解释的。输入预期为数字,但输入的输入是字符串“”
读取输入并期望值为整数的行:
int numberOfIslands = Integer.parseInt(fileScanner.nextLine());
提供正确的输入。
您也可以将.nextLine()
更改为nextInt()