NoSuchElementException
occurs when no more tokens are available.
try this when you are reading
if(scanner.hasNext())
scanner.next();
سؤال
Following Java code:
File folder = new File("/home");
File[] listofFiles = folder.listFiles();
for (j = 0; j < listofFiles.length; j++) {
File file = listofFiles[j];
if (file.isFile() && file.getName().endsWith(".ucf")) {
String content = new Scanner(new File(file.getName())).useDelimiter("\\Z").next();
for(i=0;i<content.length-10;i++)
// do something..
content = null;
} // close if..
}
throws
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:907)
at java.util.Scanner.next(Scanner.java:1416)
at c.main(c.java:18)
However, earlier when I was using Scanner to read a particular file to String content
, there was no such error at Scanner.next()
. Now it gives this exception when I try to traverse through all files.
المحلول
NoSuchElementException
occurs when no more tokens are available.
try this when you are reading
if(scanner.hasNext())
scanner.next();