简体   繁体   中英

What does this program mean by Unknown Source and how do I fix it?

I have been trying to copy a program from a workbook try to use disk Scanners, and I have been running into a problem where the on lines 14-17 (the disk scanners.next lines) I keep getting the error "Unknown Source"

This is the code:

public class SomeClassName {
    public static void main(String args[]) throws IOException {
      Scanner diskScanner = new Scanner(new File("C:\\Users\\student\\Workspace\\TextFiles\\Test.txt"));

      diskScanner.nextInt();
      diskScanner.nextDouble();
      diskScanner.next();
      diskScanner.nextLine();
    }
}

This was the error I got in the console:

Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at SomeClassName.main(SomeClassName.java:14)

I have tried to follow all of Eclipse's suggestions, but none of them have worked. Is there something I am missing?

Edit:wrong error was posted, fixed

This error usually comes when there in no further data to read for scanner or data in wrongly formatted. Check your file see all data is present and in the format you are reading. With Scanner you need to check if there is a next line with hasNextLine() or next int with hasNextInt() before actually reading. Something like this

while(sc.hasNextLine()){
    str=sc.nextLine();
    //...
}

Looks like in your case its not able to get the next int because its not present. Please change your data again and do something like this

if(disKScanner.hasNextInt()){
diskScanner.nextInt();
}
  // similarly do this check for all reads then only read it

You probably are using JRE instead of JDK. The runtime can't tell you what's wrong you need the development kit. I think there is already a similar bug please flag as duplicate if so I'm in mobile right now.

Edit: you do have most likely have a filenotfound exception make sure the path is correct. Eclipse does make it's folder usually in lower case and not "Workspace"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM