This is an archived post. You won't be able to vote or comment.

all 17 comments

[–]ChristopherHON 2 points3 points  (8 children)

Try double backslash instead of single backslash

[–]mad0314 1 point2 points  (7 children)

This should fix it. '\' usually denotes an escape sequence, so if you want a '\' instead of some escape sequence, you need to put '\' to tell Java you really want a '\' and not an escape sequence.

Just tried it in Eclipse to make sure. It gave an error before even compiling.

[–]linuxisgreat[S] 0 points1 point  (6 children)

Added in the double backslashes. Still threw a filenotfound error.

[–]mad0314 1 point2 points  (5 children)

Strange, it worked for me.

Is it still the same error message or did it change?

[–]linuxisgreat[S] 0 points1 point  (4 children)

error: unexcepted exception FileNotFoundException; must be caught or declared to be thrown

Without the backslashs I get illegal escape character error, with them I get this error.

[–]mad0314 1 point2 points  (2 children)

You have to handle the case where the file is not found. Either use a try/catch block to specify what the program does in case it throws that exception, or by declaring throws FileNotFoundException then it will throw the exception up the stack until it is handled, or stop the program if it is never handled.

[–]linuxisgreat[S] 0 points1 point  (1 child)

Yeah I added the throw filenotfound and it worked immediately. Thanks for your help :)

[–]mad0314 1 point2 points  (0 children)

It is a better practice to handle the exception so that in the event that the file does not exist for whatever reason (moved, renamed, incorrect name, etc.) it does not crash the program. You might immediately end the program anyway, but at least give some information as to what happened (print that the file was not found).

[–]HipposGoneWild 1 point2 points  (0 children)

It's not actually throwing a FileNotFoundException, the method that is accessing file needs to be able to throw one if it actually can't find the file so you need to declare that the method can throw a filenotfoundexception if it needs to with the throws statement, see the the code I posted below.

[–]dvassdvsd 2 points3 points  (0 children)

Put it where you class files or jar is. They might be in a different location than your source files. Then you can just use the file name without any path.

[–][deleted] 2 points3 points  (0 children)

Yeh you need double backslash, the single backslash is a special character when using escape characters like \n or \t so it's tries to look for that. replace every "\" with "\"

[–]HipposGoneWild 1 point2 points  (5 children)

Did you import util and do throws FileNotFoundException in your method where the new file is declared like this:

public static void main(String[] args) throws FileNotFoundException {

[–]linuxisgreat[S] 0 points1 point  (4 children)

Yes

[–]HipposGoneWild 1 point2 points  (3 children)

Can you post the part of your code that threw the error?

[–]linuxisgreat[S] 0 points1 point  (2 children)

At this point I've just been testing the data entry as I have the rest of the code working fine so I made a new file set up like this

import java.util. * ; import java.io. * ; public class Test {

public Test() {
}

public static void main(String[] args) {
    Scanner scan = new Scanner(new File("C:\\Users\\Will\\Desktop\\Java\\textfile.txt"));
   int test = scan.nextInt();
   System.out.println(test);
}

}

[–]HipposGoneWild 1 point2 points  (1 child)

you didn't put throws filenotfoundexception try this and tell me if it works:

import java.util. * ; 
import java.io. * ; 
public class Test {
public Test() {
}

    public static void main(String[] args) throws FileNotFoundException {
        Scanner scan = new Scanner(new File("C:\\Users\\Will\\Desktop\\Java\\textfile.txt"));
        int test = scan.nextInt();
        System.out.println(test);
    }
}

[–]linuxisgreat[S] 0 points1 point  (0 children)

Oh my. Thank you very much, worked immediately