#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    5
    Rep Power
    0

    I need some help with this Exception error


    I am doing a program my class that reads in a "txt file. I can compile the program with no problems. When I run it in Eclipse I get this error.

    Exception in thread "main" java.io.FileNotFoundException: file.txt (The system cannot find the file specified)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(Unknown Source)
    at java.util.Scanner.<init>(Unknown Source)
    at music.main(music.java:12)


    Here is the segment of code:
    Code:
    	Scanner input = new Scanner (new File("file.txt"));
    		line = input.nextLine();
    		while (!line.equals("***")) {
    			String inputNotes [] = line.split(" +");
    			n = input.nextInt(); input.nextLine();

    I would appreciate any assistance. If I need to post more code, just let me know.
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,696
    Rep Power
    347
    The system cannot find the file specified
    The program/system can not find the file. Does the file exist in the folder where the program is looking?
    To see where the program is looking for the file, create a File object for the file and print the value of the absolute path for that File object. See the API doc for the method that returns the absolute path.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    5
    Rep Power
    0
    Originally Posted by NormR
    The program/system can not find the file. Does the file exist in the folder where the program is looking?
    To see where the program is looking for the file, create a File object for the file and print the value of the absolute path for that File object. See the API doc for the method that returns the absolute path.

    The file is in the same folder as the program. The funny thing is I can run it with the ms dos just find. I think it is Eclipse. I just want to be able to figure out for the future. I like using Eclipse but if it can not show me what I am doing than it is kind of pointless.
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,696
    Rep Power
    347
    What printed when the code printed the absolute path for the file? Is the file at the location shown?
    An IDE can have the source in one folder, the class file in another folder and set the current directory to yet another folder. The posted code looks in the current directory for the file. I don't know how to control where the IDE sets the current directory.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    5
    Rep Power
    0
    Originally Posted by NormR
    What printed when the code printed the absolute path for the file? Is the file at the location shown?
    An IDE can have the source in one folder, the class file in another folder and set the current directory to yet another folder. The posted code looks in the current directory for the file. I don't know how to control where the IDE sets the current directory.
    How do you create a File object for the file???
  10. #6
  11. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,696
    Rep Power
    347
    How do you create a File object for the file???
    The posted code has part of it:
    Code:
      new File("file.txt")
    Just add
    File testFile =
    in front of it.
    Code:
      File testFile = new File("file.txt");  // create File object
    and print the value of the absolute path for testFile.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    5
    Rep Power
    0
    Originally Posted by NormR
    The posted code has part of it:
    Code:
      new File("file.txt")
    Just add
    File testFile =
    in front of it.
    Code:
      File testFile = new File("file.txt");  // create File object
    and print the value of the absolute path for testFile.

    Code:
    File testFile = new File("file.txt");
    String fullPath = file.getAbsolutePath("file.txt");
    I put these lines of code in and got this error:

    Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    file cannot be resolved

    at Music.main(Music.java:14)


    Line 14 is the String fullPath line. I know I am doing something wrong I just do no know what.
  14. #8
  15. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,696
    Rep Power
    347
    Code:
    String fullPath = file.getAbsolutePath("file.txt");
    What does the variable: file contain? Where is it defined and given a value?
    What class is it? Does it have a method getAbsolutePath() that takes a String as an arg?

    Did you look at the API doc for the File class?
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    5
    Rep Power
    0
    Originally Posted by NormR
    Code:
    String fullPath = file.getAbsolutePath("file.txt");
    What does the variable: file contain? Where is it defined and given a value?
    What class is it? Does it have a method getAbsolutePath() that takes a String as an arg?

    Did you look at the API doc for the File class?
    This what is in "file.txt"


    Code:
    C# E Db G#
    1
    D E D A
    -1
    ***

    The first and third line are notes. The second and fourth lines are the number of half steps that need to be taken.
    "file.txt" is read into the program and is declared as "int n":
    Code:
    import java.util.Scanner;
    import java.io.*;
    
    public class Music {
    	public static void main (String args[])throws IOException{
    		String notes [] = { "A", "A#", "B", "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#" } ;
    		String line;
    		int n;
    		int location;
    		int target;
    This next part of the code reads in "file.txt"


    Code:
    Scanner input = new Scanner (new File("file.txt"));
    		line = input.nextLine();
    		while (!line.equals("***")) {
    			String inputNotes [] = line.split(" +");
    			n = input.nextInt(); input.nextLine();

    I am lost. I don't know why I can not get the line of code that you gave me to give me the path of the file. Did you want me to make a whole new file and test out "file.txt"?? If that was the case I misunderstood you.
  18. #10
  19. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,696
    Rep Power
    347
    To see where the program is looking for the file, You need to print out the absolute path for a File object that points to the file.
    Until you find out where the program is looking for the file, you won't know where to put the file so the program can read it.

    Please copy the full text of the compiler's error message and post it here.

    I gave you the code for defining a File object named testFile.
    Now you need to use that variable to call the File class's method to get the path and then you need to print that path so you can see where the program is looking.


    Another way to see where the program is looking for the file is to make many different copies of the file.txt file and put a different copy of the file in all the possible folders that the program could look in. Look at the data that is read from the file and you will know where the program read it from.
    Last edited by NormR; September 17th, 2013 at 10:01 PM.

IMN logo majestic logo threadwatch logo seochat tools logo