Java 此程序从文件中读取数据,并按指定添加和删除记录
我得到了这个错误,但我真的不知道这意味着什么,我如何才能摆脱错误。该程序在读取输入文件后,按照输入文件中的规定添加和删除记录。然后,它将输出写入另一个文件 错误:Java 此程序从文件中读取数据,并按指定添加和删除记录,java,file,linked-list,Java,File,Linked List,我得到了这个错误,但我真的不知道这意味着什么,我如何才能摆脱错误。该程序在读取输入文件后,按照输入文件中的规定添加和删除记录。然后,它将输出写入另一个文件 错误: java.lang.NumberFormatException: For input string: "1 Acconci" at java.lang.NumberFormatException.forInputString(Unknown Source) at java.lang.Integer.parseInt(Unknown
java.lang.NumberFormatException: For input string: "1 Acconci"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at ReadArtistList.getList(ReadArtistList.java:23)
at UpdateArtistList.<init>(UpdateArtistList.java:12)
at p3a.main(p3a.java:9)
UpdaterAtistList.java:
import java.io.*;
//import java.lang.*;
public class UpdateArtistList
{
private ArtistList t;
public UpdateArtistList()
{
try
{
t = new ReadArtistList().getList();
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("C:/Users/patel/Desktop/JavaFiles/p2changes.txt")));
String line = reader.readLine();
while (line != null)
{
//splitting file by comma
String[] array = line.split(" ");
//checking the string of whether it is for adding new item or deleting an item
if(array[0].equals("A") || array[0].equals("a"))
{
t.add(array[1]);
}
else if(array[0].equals("D") || array[0].equals("d"))
{
//iterating through array for finding an item to delete
t.delete(Integer.parseInt(array[1]));
}
line = reader.readLine();
}
reader.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public ArtistList getUpdatedList()
{
return t;
}
}
import java.io.*;
import java.lang.*;
public class ReadArtistList
{
/* public ReadArtistList()
{
}
*/ public ArtistList getList()
{
ArtistList a=new ArtistList();
try
{
//reading from file "items.txt"
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("C:/Users/patel/Desktop/JavaFiles/p1artists.txt")));
String line = reader.readLine();
while (line != null)
{
//splitting file by comma
String[] array = line.split(" ");
a.addNode(Integer.parseInt(array[0]),array[1]);
line = reader.readLine();
}
reader.close();
//return a;
}
catch(Exception e)
{
e.printStackTrace();
}
return a;
}
}
ReadArtistList.java:
import java.io.*;
//import java.lang.*;
public class UpdateArtistList
{
private ArtistList t;
public UpdateArtistList()
{
try
{
t = new ReadArtistList().getList();
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("C:/Users/patel/Desktop/JavaFiles/p2changes.txt")));
String line = reader.readLine();
while (line != null)
{
//splitting file by comma
String[] array = line.split(" ");
//checking the string of whether it is for adding new item or deleting an item
if(array[0].equals("A") || array[0].equals("a"))
{
t.add(array[1]);
}
else if(array[0].equals("D") || array[0].equals("d"))
{
//iterating through array for finding an item to delete
t.delete(Integer.parseInt(array[1]));
}
line = reader.readLine();
}
reader.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public ArtistList getUpdatedList()
{
return t;
}
}
import java.io.*;
import java.lang.*;
public class ReadArtistList
{
/* public ReadArtistList()
{
}
*/ public ArtistList getList()
{
ArtistList a=new ArtistList();
try
{
//reading from file "items.txt"
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("C:/Users/patel/Desktop/JavaFiles/p1artists.txt")));
String line = reader.readLine();
while (line != null)
{
//splitting file by comma
String[] array = line.split(" ");
a.addNode(Integer.parseInt(array[0]),array[1]);
line = reader.readLine();
}
reader.close();
//return a;
}
catch(Exception e)
{
e.printStackTrace();
}
return a;
}
}
p3a第9行的NumberFormatException是创建对象的位置。该错误还表示Integer.parseInt正在运行。请执行以下操作: 1.调试堆栈跟踪。
2.在第三个类中,如果要拆分数组,请将其包装在包含parseInt的try-catch块中,或者在拆分后打印数组。你会明白怎么了。还有,你真的是用逗号分开的吗?您的备注和代码在该行中不正确。什么是NumberFormatException,我如何修复它?-->stacktrace告诉您错误发生的行,消息告诉您无效输入和错误发生的原因。剩下的问题可以通过几分钟的谷歌搜索和调试来解决。提示:用于搜索的关键字将是NumberFormatException和Integer.ParseInt。它的可能重复项正在第9行打印p3a的数字格式异常。这是您实例化更新文章列表类的地方。我建议调试堆栈跟踪,看看哪个类行为不正常。另外,在分析数组未读文章列表期间似乎出现错误。将其包装在try-catch块中,或者在拆分后打印数组,以查看得到的结果。