java.util.NoSuchElementException:未找到任何行*
我一直在犯这个错误java.util.NoSuchElementException:未找到任何行*,java,nosuchelementexception,Java,Nosuchelementexception,我一直在犯这个错误 java.util.NoSuchElementException找不到行 当我使用这个方法时 public boolean hasMoreCommands() { if (input.hasNextLine()) { return true; } else { //input.close(); return false; } } public void advance() { String st
java.util.NoSuchElementException
找不到行
当我使用这个方法时
public boolean hasMoreCommands() {
if (input.hasNextLine()) {
return true;
} else {
//input.close();
return false;
}
}
public void advance() {
String str;
if(hasMoreCommands() == true){
do {
str = input.nextLine().trim();
// Strip out any comments
if (str.contains("//")) {
str = (str.substring(0, str.indexOf("//"))).trim();
}
} while (str.startsWith("//") || str.isEmpty() || hasMoreCommands());
command = str;
}
}
我这里有主要代码:
public class Ptest
{
public Ptest(String fileName)
{
String line = null;
String nName = fileName.replace(".vm", ".asm");
Parser p = new Parser();
try{
File neF = new File(nName);
if(!neF.exists()){
neF.createNewFile();
}
File tempFile = new File("temp.txt");
if(!tempFile.exists()){
tempFile.createNewFile();
}
FileReader fr = new FileReader(fileName);
BufferedReader br = new BufferedReader(fr);
FileWriter fw = new FileWriter(nName);
BufferedWriter bw = new BufferedWriter(fw);
FileWriter writR = new FileWriter(tempFile);
BufferedWriter buffR = new BufferedWriter(writR);
while((line = br.readLine()) != null) {
buffR.write(line+ "\n");
//System.out.println(line);
}
buffR.flush();
buffR.close();
p.insertTitle(tempFile);
String ctype = p.commandType();
int len = ctype.length();
int spaces = 13 - len;
String sp = " ";
String asp = " ";
String a1 = null;
int a2;
int alen;
boolean t = false;
while(p.hasMoreCommands()){
for(int i= 0; i < spaces; i++){
sp += " ";
}
t = p.hasMoreCommands();
a1 = p.arg1();
alen = (10 - a1.length());
for(int i= 0; i < alen; i++){
asp += " ";
}
//a2 = p.arg2();
if (ctype == "C_PUSH" || ctype == "C_POP" || ctype == "C_FUNCTION" || ctype == "C_CALL") {
a2 = p.arg2();
bw.write(ctype + sp + a1 + asp + a2);
}
else {
bw.write(ctype + sp + a1);
}
p.advance();
ctype = p.commandType();
len = ctype.length();
spaces = 13 - len;
}
bw.flush();
bw.close();
}
catch(FileNotFoundException ex){
System.out.println("File not found!");
}
catch(IOException ex){
System.out.println("Error reading file '" + fileName + "'");
}
}
}
公共类Ptest
{
公共Ptest(字符串文件名)
{
字符串行=null;
字符串nName=fileName.replace(“.vm”和“.asm”);
Parser p=newparser();
试试{
文件neF=新文件(nName);
如果(!neF.exists()){
neF.createNewFile();
}
File tempFile=新文件(“temp.txt”);
如果(!tempFile.exists()){
tempFile.createNewFile();
}
FileReader fr=新的FileReader(文件名);
BufferedReader br=新的BufferedReader(fr);
FileWriter fw=新的FileWriter(nName);
BufferedWriter bw=新的BufferedWriter(fw);
FileWriter writer=新的FileWriter(tempFile);
BufferedWriter Buffer=新的BufferedWriter(Writer);
而((line=br.readLine())!=null){
buffer.write(行+“\n”);
//系统输出打印项次(行);
}
buffer.flush();
buffer.close();
p、 插入标题(临时文件);
字符串ctype=p.commandType();
int len=ctype.length();
整数空间=13-len;
字符串sp=“”;
字符串asp=“”;
字符串a1=null;
int a2;
int alen;
布尔t=false;
while(p.hasMoreCommands()){
for(int i=0;i
我通过了调试器,调试器会逐字逐句地处理整个文件,然后在完成时给我一个错误。我猜你的问题在这里:
if(hasMoreCommands() == true){
do {
str = input.nextLine().trim();
// Strip out any comments
if (str.contains("//")) {
str = (str.substring(0, str.indexOf("//"))).trim();
}
} while (str.startsWith("//") || str.isEmpty() || hasMoreCommands());
command = str;
}
遇到的异常(
NoSuchElementException
)通常发生在有人试图迭代某个对象(字符串标记、映射等)而没有首先检查是否还有更多元素要获取时。第一次执行上述代码时,它会检查是否有更多命令,然后进入循环。但是,如果while()
所做的测试成功,下一次迭代将在尝试执行input.nextLine()
时失败。在调用此方法之前,必须检查是否有下一行要获取。用if(input.hasNextLine())
环绕这一行,我认为你应该没事。像@hfontanez一样,我认为你的问题在于这段代码:
if(hasMoreCommands() == true){
do {
str = input.nextLine().trim();
// Strip out any comments
if (str.contains("//")) {
str = (str.substring(0, str.indexOf("//"))).trim();
}
} while (str.startsWith("//") || str.isEmpty() || hasMoreCommands());
command = str;
}
但是,我的解决方案是将while子句更改为while(str.isEmpty()&&hasMoreCommands())代码>
我假设“advance”应该返回下一个非注释/空行
如果上一个过程中的字符串为空(去掉任何注释后),如果不是最后一行,它将再次循环。但是,如果这是最后一行或str中仍然有某些内容,那么它将退出循环。注释应该已经被剥离,所以暂时不需要测试
我想如果你只是在循环中测试hasNextLine,那么如果最后一行是comment/blank,它将永远不会退出循环。请在你的帖子中包含整个异常消息。很高兴它能工作!我看你是新来的——当你得到一个你需要的答案时,你要做的就是接受这个答案。