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

Thread: I have a question.

  1. #1
    Member
    Join Date
    Aug 2013
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default I have a question.

    Could someone please explain to me why my program keeps giving me the following error when I try to read in a text file (test.txt). I placed the text file into the same folder as the .java file. Also, I am using netbeans if it matters. Thank you.

    Error message:

    Exception in thread "main" java.io.FileNotFoundException: test.txt (The system cannot find the file specified)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.jav a:138)
    at java.io.FileInputStream.<init>(FileInputStream.jav a:97)
    at java.io.FileReader.<init>(FileReader.java:58)
    at linearcongruentialgenerator.LinearCongruentialGene rator.main(LinearCongruentialGenerator.java:25)
    Java Result: 1




    Here is my code:


    package linearcongruentialgenerator;
     
    //Imports
    import java.io.*;
    import java.util.*;
     
    /**
     *
     * @author Jared
     */
    public class LinearCongruentialGenerator {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) throws FileNotFoundException, IOException {
            String fileName;
            int Z, I, M, Seed;
     
            Scanner s = new Scanner(System.in);
            System.out.println("Whay is the name of the file?");
            fileName = s.nextLine();
     
            BufferedReader inputFile = new BufferedReader(new FileReader(fileName));
            String[] line;
     
            while (inputFile.ready()) {
                line = inputFile.readLine().split(" ");
                Z = Integer.parseInt(line[0]);
                I = Integer.parseInt(line[1]);
                M = Integer.parseInt(line[2]);
                Seed = Integer.parseInt(line[3]);
     
                int period = period(Z, I, M, Seed);
     
                System.out.println("Case #1: " + period);
     
     
     
                /*  Test
                 LinearCongruentialGenerator result  = new LinearCongruentialGenerator(3,2,10,5);
                 int answer = result.getAnswer();
                 System.out.println(answer);
     
                 System.out.println(result.next());
                 System.out.println(result.next());
                 System.out.println(result.next());
                 System.out.println(result.next());
                 System.out.println(result.next());
                 System.out.println(result.next());
                 System.out.println(result.next());
     
                 int period = period(533, 250, 314322, 7052);
     
                 System.out.println("\n" + period);
                 */
            }
        }
        //Variables 
        int Z, I, M, Seed, N, answer;
     
        /**
         *
         * @param z
         * @param i
         * @param m
         * @param seed
         */
        public LinearCongruentialGenerator(int z, int i, int m, int seed) {
     
            //These three are the constants.
            Z = z;
            I = i;
            M = m;
     
            //This is the seed.
            Seed = seed;
     
            //The generator:
            N = (Z * Seed + I) % M;
     
            answer = N;
     
        }
     
        public int next() {
            LinearCongruentialGenerator temp = new LinearCongruentialGenerator(Z, I, M, N);
     
            this.N = temp.answer;
     
            return temp.answer;
        }
     
        public static int period(int z, int i, int m, int seed) {
     
            //Variables
            int Z, I, M, Seed, N1, N2 = 0;
            int period = 0;
            Z = z;
            I = i;
            M = m;
            Seed = seed;
     
            LinearCongruentialGenerator temp = new LinearCongruentialGenerator(Z, I, M, Seed);
     
            N1 = temp.answer;
     
            while (N1 != N2) {
                N2 = temp.next();
                period++;
            }
     
            return period;
     
        }
     
        public int getAnswer() {
            return answer;
        }
    }



    Here are the contents of my text file.

    3 2 10 5


  2. #2
    Member Kewish's Avatar
    Join Date
    Apr 2013
    Location
    Australia
    Posts
    116
    Thanks
    10
    Thanked 17 Times in 14 Posts

    Default Re: I have a question.

    FileNotFoundException will mean that it cannot find the file or it doesn't exist in that directory.

    What is the path to your file?
    What are you typing in for your input?

    the path can be relative to the root of your project. For example, if you create a folder name textfiles in your root directory, the path will be;
    "textfiles/somefile.txt"

    Alternatively you can pass the complete path like;
    "C:\\somefolder\\maybeAnotherFolder\\somefile. txt"

  3. #3
    Member
    Join Date
    Aug 2013
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I have a question.

    I am simply entering test.txt

    The actual path to the text file is C:\Users\Jared\Desktop\Linear Congruential Generator\LinearCongruentialGenerator\src\linearco ngruentialgenerator

    --- Update ---

    C:\Users\Jared\Desktop\Linear Congruential Generator\LinearCongruentialGenerator\src\linearco ngruentialgenerator\test.txt

    --- Update ---

    And the path to my .java file is C:\Users\Jared\Desktop\Linear Congruential Generator\LinearCongruentialGenerator\src\linearco ngruentialgenerator\LinearCongruentialGenerator.ja va

  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: I have a question.

    Put this in your program and see what it prints:
      System.out.println("Where are we="+new File("Here I Am").getAbsolutePath());
    It should show you the path to a file in the current directory.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Aug 2013
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I have a question.

    This is what it printed: Where are we=C:\Users\Jared\Desktop\Linear Congruential Generator\LinearCongruentialGenerator\Here I Am

    --- Update ---

    This is the example that I am going by:


    public class Input{
        public static void main (String[] args)
            throws FileNotFoundException, IOException 
        {
            BufferedReader inputFile = new BufferedReader(new FileReader("test.txt"));
     
            while (inputFile.ready()) {
                String[] line = inputFile.readLine().split(" ");
                String name = line[0];
                int id = Integer.parseInt(line[1]);
     
     
                System.out.printf("%d - %s\n", id, name);       
            }
        }
    }


    Im just not understanding as to why it is not finding the file.

  6. #6
    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: I have a question.

    That path that was printed shows the folder that the program is looking in when it tries to read the file.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Aug 2013
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I have a question.

    Okay, so I moved the text file to the folder that it said it was looking in and it gave me this error:

    Exception in thread "main" java.io.FileNotFoundException: test.txt (The system cannot find the file specified)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.jav a:138)
    at java.io.FileInputStream.<init>(FileInputStream.jav a:97)
    at java.io.FileReader.<init>(FileReader.java:58)
    at linearcongruentialgenerator.LinearCongruentialGene rator.main(LinearCongruentialGenerator.java:25)
    Java Result: 1


    --- Update ---

    I fixed that issue, it was saved as test.txt.txt
    Thanks for the help.

Similar Threads

  1. Replies: 1
    Last Post: November 4th, 2013, 05:38 AM