Stars.java中的NumberFormatException
我已经在这方面工作了一段时间,我一直得到相同的错误 线程“main”java.lang.NumberFormatException中的异常:对于输入字符串:“ 位于java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) 在java.lang.Integer.parseInt(Integer.java:504) 位于java.lang.Integer.valueOf(Integer.java:582) 位于StarsTable.addArray(StarsTable.java:50) 在StarsTable。(StarsTable.java:24) 在Stars.main(Stars.java:7) Stars.javaStars.java中的NumberFormatException,java,numberformatexception,Java,Numberformatexception,我已经在这方面工作了一段时间,我一直得到相同的错误 线程“main”java.lang.NumberFormatException中的异常:对于输入字符串:“ 位于java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) 在java.lang.Integer.parseInt(Integer.java:504) 位于java.lang.Integer.valueOf(Integer.java:582
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(" ");
}
}
}
公共级明星
{
公共静态void main(字符串参数[])
{
字符串infle=args[0];
恒星稳定星=新恒星稳定星(infle);第7行
System.out.println(“输出星图的程序”);
System.out.println(“编写人”);
System.out.println(stars.title());
整数[][]a=stars.getArray();
系统输出打印(“-”);
对于(int x=0;x
}
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;
}
}
导入java.io.BufferedReader;
导入java.io.FileReader;
导入java.io.IOException;
导入java.util.*;
公共类星宿
{
私有ArrayList a=新ArrayList();
私有字符串标题;
私有字符串readLine=null;
私有int行=0;
专用整数数组[];
公共StarsTable(字符串文件名)
{
尝试
{
BufferedReader br=新
BufferedReader(新文件读取器(文件名));
title=br.readLine();
而((readLine=br.readLine())!=null)
{
添加数组(读线);第24行
}
br.close();
}
捕获(IOE异常)
{
System.err.println(“未找到文件,请”
+“在命令行中输入文件名。”);
系统出口(1);
}
}
公共字符串标题()
{
返回标题;
}
公共void addArray(字符串读取行)
{
int i=0;
String[]splitLine=readLine.split(“”);
对于(i=0;i
我一辈子都搞不清楚它有什么问题。任何帮助都将不胜感激。谢谢 我想这就是你需要的:
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++;
}
public void addArray(字符串读取行)
{
int i=0;
String[]splitLine=readLine.split(“”);
对于(i=1;i
我想这就是您需要的:
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++;
}
public void addArray(字符串读取行)
{
int i=0;
String[]splitLine=readLine.split(“”);
对于(i=1;i
尝试这样更改代码
for (i = 0; i < splitLine.length; i++)
{
a.add(Integer.valueOf(splitLine[i].trim()));
}
for(i=0;i
而不是
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]);
}
}
for(i=0;i
尝试这样更改代码
for (i = 0; i < splitLine.length; i++)
{
a.add(Integer.valueOf(splitLine[i].trim()));
}
for(i=0;i
而不是
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]);
}
}
for(i=0;i