Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Not sure what's wrong with my client class. keep getting "java.io.FileNotFoundException: " when I enter any filename. What am I missing?

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Exclamation Not sure what's wrong with my client class. keep getting "java.io.FileNotFoundException: " when I enter any filename. What am I missing?

    package HeightsSorter;
    import java.util.*;
    import java.io.*;
     
    public class HeightsSorter {
     
        public static void main(String[] args) 
        {
            try
            {
     
               Scanner keyb = new Scanner(System.in);
               System.out.print("Enter the file name> ");
               String dataFile = keyb.next();
               Scanner readFile = new Scanner(new File(dataFile));
               System.out.print("Enter the output file name> ");
               String outFile = keyb.next();
               PrintWriter pw = new PrintWriter(outFile);
               int ft,in;
               ArrayList<Height> heights = new ArrayList<Height>();
               while (readFile.hasNext())
               {
                   ft = readFile.next();
                   in = readFile.next();
                   heights.add(new Height(ft, in));
               }
               readFile.close();
               int i;
               System.out.println("Unsorted Heights from "+dataFile+":");
               for (i=0; i<heights.size();i++){
                   System.out.println(heights.get(i));
               }
               Collections.sort(heights);
               System.out.println();
               System.out.println("Sorted Heights from "+dataFile+":");
               for (i=0; i<heights.size();i++)
               {
                   System.out.println(heights.get(i));
                   pw.println(heights.get(i));
               }
               pw.close();
            }
            catch(IOException e)
            {
                System.out.println(e);
                System.exit(3);
            }        
        }
    }


  2. #2
    Member
    Join Date
    Apr 2012
    Posts
    42
    Thanks
    8
    Thanked 4 Times in 4 Posts

    Default Re: Not sure what's wrong with my client class. keep getting "java.io.FileNotFoundException: " when I enter any filename. What am I missing?

    Are you typing the file name correctly? Remember that when you give it a file name it has to be exact, so as an example look at the following:

    File1 = Wrong

    File1.txt = Correct

    You also need to make sure the file already exists somewhere in the same folder that the Java project is in.

  3. The Following User Says Thank You to bankston13 For This Useful Post:

    Meirikai (December 2nd, 2012)

  4. #3
    Junior Member
    Join Date
    Oct 2012
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Not sure what's wrong with my client class. keep getting "java.io.FileNotFoundException: " when I enter any filename. What am I missing?

    It's saved in the same folder as my project and I'm typing it in correctly. It's still giving me the same message. =(

  5. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Not sure what's wrong with my client class. keep getting "java.io.FileNotFoundException: " when I enter any filename. What am I missing?

    First off, you should be calling Scanner's nextLine() method not the next() method to be sure that you're handling the end-of-line (EOL) token appropriately. Next, if you get a run-time exception it is most helpful if you post the full exception and let us know which line is throwing the exception. But most important I agree that you're likely not looking in the right place for the file. You should either be entering the full path to the file, starting with the drive letter, or else entering the path to the file relative to the user.dir. To find out what the user directory is, somewhere at the top of your main method put in the line

    System.out.println(System.getProperty("user.dir"));

Similar Threads

  1. JButton with "Enter Key" keyboard action
    By chronoz13 in forum Java Swing Tutorials
    Replies: 2
    Last Post: March 14th, 2012, 06:49 AM
  2. className.main(new String[]{"filename.txt"}); - Help
    By knightmetal in forum Java Theory & Questions
    Replies: 4
    Last Post: August 25th, 2011, 07:22 AM
  3. password field, with focused "Enter" button
    By chronoz13 in forum Java Swing Tutorials
    Replies: 1
    Last Post: June 13th, 2010, 05:00 PM
  4. password field, with focused "Enter" button
    By chronoz13 in forum Java Code Snippets and Tutorials
    Replies: 1
    Last Post: June 13th, 2010, 05:00 PM
  5. JButton with "Enter Key" keyboard action
    By chronoz13 in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: January 26th, 2010, 10:53 AM