希望这将是一个简单的答案,我只是忽略了一些小问题。
目标:获取一个数组列表(当前包含文本文件中的文本行)并将一个 String
变量设置为等于指定的数组列表位置。
目前,每一行都是从百科全书文件中获取的原始文本。我需要能够通过 .replaceAll 函数删除非 alpha。但是,我当前的程序返回空指针异常,并且我无法理解原因。我对 Java 相当陌生,因此非常感谢完整的答案和解释。
我的代码:(我的老师告诉我们使用 EasyReader 类让我们的生活变得更轻松......)
EasyReader fileIn = new EasyReader("Encyclopedia.txt");
public void createList()
{
String x=fileIn.readLine();
ArrayList<String> list = new ArrayList<String>();
while((!fileIn.eof()))
{
String y=fileIn.readLine();
list.add(y);
}
int count=0;
while(count<list.size())
{
String temp=list.get(count);
temp.replaceAll("[^a-zA-z ]", ""); //null pointer points to this line
temp.toLowerCase(); //and this line
list.set(count, temp);
count++;
}
count=0;
while(count<list.size());
{
System.out.println(list.get(count));
count++;
}
System.out.println(list.size());
while(count<list.size())
{
fileOut.println(list.get(count));
count++;
}
fileOut.close();
}
提前感谢您的帮助:)
最佳答案
我想我发现了你的错误!
你的 while 循环应该只转到 list.size() - 1
而不是你那里的 list.size()
。见下文:
while(count<list.size()- 1)
{
String temp=list.get(count);
temp.replaceAll("[^a-zA-z ]", ""); //null pointer points to this line
temp.toLowerCase(); //and this line
list.set(count, temp);
count++;
}
关于java - 如何获取数组列表位置并将其转换为字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34215107/