java.util.NoSuchElementException问题,代码29行报错
这里是具体代码。importjava.io.*;importjava.util.*;publicclassmyFile{publicstaticvoidmain(Stri...
这里是具体代码。import java.io.*;import java.util.*;public class myFile { public static void main(String args[]) throws IOException { Vector
users=new Vector(); RandomAccessFile localRandomAccessFile = new RandomAccessFile("C:\\1.txt", "rw"); String userInformation=""; String userID="123456789"; String initialID=""; String deletUser=""; StringTokenizer st; while ((userInformation = localRandomAccessFile.readLine()) != null) { users.add(userInformation); } for(Iteratori = users.iterator(); i.hasNext();) { String str=i.next().toString(); st=new StringTokenizer(str,"_"); initialID=st.nextToken(); if(initialID.equals(userID)) { deletUser=str; } } users.remove(deletUser); localRandomAccessFile.seek(0); for(Iterator i = users.iterator(); i.hasNext();) { deletUser=i.next().toString()+"\r\n"; System.out.println(deletUser); localRandomAccessFile.write(deletUser.getBytes("UTF-8")); } localRandomAccessFile.close(); } }
求解答 展开
users=new Vector(); RandomAccessFile localRandomAccessFile = new RandomAccessFile("C:\\1.txt", "rw"); String userInformation=""; String userID="123456789"; String initialID=""; String deletUser=""; StringTokenizer st; while ((userInformation = localRandomAccessFile.readLine()) != null) { users.add(userInformation); } for(Iteratori = users.iterator(); i.hasNext();) { String str=i.next().toString(); st=new StringTokenizer(str,"_"); initialID=st.nextToken(); if(initialID.equals(userID)) { deletUser=str; } } users.remove(deletUser); localRandomAccessFile.seek(0); for(Iterator i = users.iterator(); i.hasNext();) { deletUser=i.next().toString()+"\r\n"; System.out.println(deletUser); localRandomAccessFile.write(deletUser.getBytes("UTF-8")); } localRandomAccessFile.close(); } }
求解答 展开
3个回答
展开全部
多用了一次i.next(),所以报了迭代器错误
追问
能说详细点么,i.next()只用了一次啊,
追答
你打个断点,或者在i.next()前打印下,看打印了几次,再和迭代里面元素比对下,肯定是越界了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
String str = i.next().toString();
//在这里添加对str空值过滤
if(str.equals("")){
continue;
}
st = new StringTokenizer(str);
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询