我已经在这工作了一段时间并且我一直收到同样的错误。
Exception in thread "main" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:504)
at java.lang.Integer.valueOf(Integer.java:582)
at StarsTable.addArray(StarsTable.java:50)
at StarsTable.<init>(StarsTable.java:24)
at Stars.main(Stars.java:7)
Stars.java
public class Stars
{
public static void main(String args[])
{
String inFile = args[0];
StarsTable stars = new StarsTable(inFile); line 7
System.out.println("Program to output a star map.");
System.out.println("Written by ");
System.out.println(stars.title());
Integer[][] a = stars.getArray();
System.out.print("-");
for (int x = 0; x < a.length; x ++)
System.out.print("--");
for (int i = 0; i < a[0].length; i++)
{
System.out.print("\n:");
for (int j = 0; j < a.length; j++)
{
if(stars.checkIfStar(i,j) == 1)
{
System.out.print(" *");
}
else
System.out.print(" ");
}
}
}
}
StarsTable.java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
public class StarsTable
{
private ArrayList<Integer> a = new ArrayList<Integer>();
private String title;
private String readLine = null;
private int rows = 0;
private Integer array[][];
public StarsTable( String fileName)
{
try
{
BufferedReader br = new
BufferedReader(new FileReader( fileName));
title = br.readLine();
while(( readLine = br.readLine()) != null)
{
addArray(readLine); line 24
}
br.close();
}
catch(IOException e)
{
System.err.println("File Not Found. Please "
+ "enter filename in command line.");
System.exit(1);
}
}
public String title()
{
return title;
}
public void addArray(String readLine)
{
int i = 0;
String[] splitLine = readLine.split(" ");
for(i = 0; i < splitLine.length; i++)
{
a.add(Integer.valueOf(splitLine[i])); Line 50
}
rows++;
}
public Integer[][] getArray()
{
toArray();
return array;
}
public void toArray()
{
array = new Integer[a.size() / rows][rows];
int g = 0;
for (int i = 0; i < (a.size() / rows); i++)
{
for (int k = 0; k < rows; k++)
{
array[i][k] = a.get(g);
g++;
}
}
}
public int checkIfStar(int i, int k)
{
Integer check = 0;
// Corners
if (i == 0 && k == 0)
check = array[i][k] + array[i + 1][k] + array[i][k + 1];
else if (i == array[0].length && k == 0)
check = array[i][k] + array[i - 1][k] + array[i][k-1];
else if (i == 0 && k == array.length)
check = array[i][k] + array[i][k - 1] + array[i + 1][k];
else if (i == array[0].length && k == array.length)
check = array[i][k] + array[i][k - 1] + array[i - 1][k];
// Edges
else if (i == 0) //Top
check = array[i][k] + array[i][k + 1] + array[i - 1][k] + array[i + 1][k];
else if (k == 0) // Left
check = array[i][k] + array[i][k - 1] + array[i][k + 1] + array[i + 1][k];
else if (i == array[0].length) //Right
check = array[i][k] + array[i][k - 1] + array[i][k + 1] + array[i - 1][k];
else if (k == array.length) // Bottom
check = array[i][k] + array[i][k - 1] + array[i - 1][k] + array[i + 1][k];
// Middle
else
check = array[i][k] + array[i][k - 1] + array[i][k + 1] + array[i - 1][k] + array[i + 1][k];
check = check / 5;
if (check < 5)
return 0;
else
return 1;
}
}
我不能为我的生活弄清楚它有什么问题。任何帮助将不胜感激。谢谢!
答案 0 :(得分:2)
尝试更改此代码,
for (i = 0; i < splitLine.length; i++)
{
a.add(Integer.valueOf(splitLine[i].trim()));
}
而不是
for (i = 0; i < splitLine.length; i++)
{
try
{
a.add(Integer.valueOf(splitLine[i].trim()));
}
catch (NumberFormatException ex)
{
System.out.println("Exception Occurs while formating : " + splitLine[i]);
}
}
答案 1 :(得分:1)
与您的(看似)期望相反,值“”没有整数值;尝试其中一个 -
if (! splitLine[i].equals("")) {
a.add(Integer.valueOf(splitLine[i]));
} else {
a.add(0); // <-- to just add 0.
}
或
// this is slower, and I probably wouldn't do this.
try {
a.add(Integer.valueOf(splitLine[i]));
} catch (NumberFormatException nfe) {
nfe.printStackTrace();
}
答案 2 :(得分:1)
最简单的解决方案是完全摆脱addArray
。只需使用Scanner
即可读取文件,而不是BufferedReader
。然后,您可以使用nextInt()
方法按编号读取数字,并使用hasNext()
检查文件何时完成。
这样,您不必逐行阅读,您不必使用split
将行拆分为数字,并且您不必解析每个数字。实际上,您已经在代码中重新发明了三个独立的轮子;并且在此过程中引入了一个错误。
最好知道API,并使用它给你的好东西。
答案 3 :(得分:0)
我认为这就是你所需要的:
public void addArray(String readLine)
{
int i = 0;
String[] splitLine = readLine.split(" ");
for(i = 1; i < splitLine.length; i++) // start at 1
{
a.add(Integer.valueOf(splitLine[i].trim()));
}
rows++;
}
答案 4 :(得分:0)
您可以编写一个util函数来处理异常,例如,
int safeParseInt(String input){
try{
return Integer.parseInt(input);
}catch(Exception e){
//error log or print
return 0;
}
}
String[] strs = line.split(" ");
for(i = 1; i < strs.length; i++)
{
array.add(safeParseInt(strs[i]));
}