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 5 of 5

Thread: Trying to input a text file but the program won't read it.

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    29
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Trying to input a text file but the program won't read it.

    I have to open a text file named Grades.txt and everytime I try to open it, it gives me a FileNotFoundException. I am not sure how to overcome this and get my program to read the file.

    import java.io.File;
    import java.io.IOException;
    import java.util.Scanner;
     
    public class EchoFileData
    {
        public static void main(String [] args) throws IOException
        {
            int grade;
     
            File inputFile = new File( "Grades.txt" );
            Scanner scan = new Scanner( inputFile );
     
            while ( scan.hasNext() )
            {
                grade = scan.nextInt();
                System.out.println( grade );
            }
        }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Trying to input a text file but the program won't read it.

    Is the program looking in the right place for the file? Or is the file in the right place?
    Print out the File object's absolutePath to see where the program is looking for the file.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    29
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Trying to input a text file but the program won't read it.

    I'm not sure how to do that. It is a download file that I downloaded for an assignment. That is where the text file is located. I tried moving it to my computer, but I'm not exactly sure where to move it to get it to open or how to print the file object's absolutePath.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Trying to input a text file but the program won't read it.

    how to print the file object's absolutePath.
    Try something like: System.out.println(inputFile.getabsolutepath());
    Check the API doc for the File class to get the correct spelling for the method.

    The printed out path will show you where the program is looking for the file.
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    Spanky_10 (February 27th, 2013)

  6. #5
    Junior Member
    Join Date
    Feb 2013
    Posts
    29
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Trying to input a text file but the program won't read it.

    Thank You so much! I finally figured it out!

Similar Threads

  1. GUI program won't take 'char' input code, can't seem to find the correct one...
    By Eclecstatic in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 1st, 2012, 07:12 AM
  2. Why won't it read file?
    By tai8 in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: May 6th, 2012, 10:42 AM
  3. Read input, read file, find match, and output... URGENT HELP!
    By MooseHead in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 3rd, 2012, 11:01 AM
  4. Replies: 8
    Last Post: March 25th, 2011, 02:34 PM
  5. Read a text file and parse the contents of file
    By HelloAll in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: March 3rd, 2011, 05:47 AM