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

Thread: Input file Not Opening =/

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Input file Not Opening =/

    Hello,

    I am currently working on a program that calculates several things about a Banks savings account for my intro to java class.
    The program is supposed to open the file named "Deposits.txt" that contains several numbers (double). The Code is currently in my USB as well as the txt file --> (E:\Bernal_Daniel_COP2250_U03_SPRING12\Projects\Pr oject5). i have Tried everything to make the file open but i get an error. I don't know whats going on.


    This is what i have to open the file: (minus the rest of the code)

    //Open the Deposits.txt file.
    File f = new File("Deposits.txt");

    Scanner inputFile = new Scanner (f);

    // Get the deposits from the Deposits.txt file.
    while (inputFile.hasNext())
    {
    double number = inputFile.nextDouble();
    instance.deposit(number);
    System.out.println("balance is : " + instance.getBalance()); //checks that method is adding the deposits/Another class
    }


    I have no Syntax errors in netbeans at all, this is my run window:

    run:
    Please Enter the Saving's Account Annual Interest Rate:
    .065
    Exception in thread "main" java.io.FileNotFoundException: Deposits.txt (The system cannot find the file specified)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.jav a:120)
    at java.util.Scanner.<init>(Scanner.java:636)
    at SavingsAccountTest2.main(SavingsAccountTest2.java: 43)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 2 seconds)

    This is how the course book tells us to open the file.
    Any help would be extremely appreciated.
    Thanks
    Last edited by dannybrgc; April 11th, 2012 at 04:51 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,405
    Thanks
    49
    Thanked 2,213 Times in 2,186 Posts

    Default Re: Input file Not Opening =/

    FileNotFoundException
    The program is not able to find that file. Either the name is misspelled or the file is not in the correct location.
    Print out the value of the File object's absolute path 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
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    939
    Thanks
    6
    Thanked 202 Times in 178 Posts

    Default Re: Input file Not Opening =/

    Check where the program is looking for the file. You can do this with:

    System.out.println("Looking for " + f.getAbsolutePath());

  4. #4
    Junior Member
    Join Date
    Apr 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Input file Not Opening =/

    Triple checked spelling and the path is definately E:\Bernal_Daniel_COP2250_U03_SPRING12\Projects\Pro ject5.

    Is there any way that i can tell the program the file's exact path?

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,405
    Thanks
    49
    Thanked 2,213 Times in 2,186 Posts

    Default Re: Input file Not Opening =/

    Use the full path in the File class's constructor.

    What was printed out when you printed the absolute path?
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    939
    Thanks
    6
    Thanked 202 Times in 178 Posts

    Default Re: Input file Not Opening =/

    Also check the name of the file as it appears on the disk. Use the command line (terminal) for this.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,405
    Thanks
    49
    Thanked 2,213 Times in 2,186 Posts

    Default Re: Input file Not Opening =/

    Good old MS will hid the extension depending on your settings. The filename could be: Deposits.txt.txt
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Junior Member
    Join Date
    Apr 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Input file Not Opening =/

    Yesss!! thank you guys it was "Deposits.txt.txt" and its because i put the extension when i saved it.

    awsomee! dumb mistake haha

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,405
    Thanks
    49
    Thanked 2,213 Times in 2,186 Posts

    Default Re: Input file Not Opening =/

    Programmers should change their Windows Explorer settings to show all file extensions.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Junior Member
    Join Date
    Apr 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Input file Not Opening =/

    ok now that I opened the "deposits.txt" file, i have to open another file for input as well called "Withdrawals.txt"

    How would i go about opening the second file?

    would i write ? :

    // Open the Withdrawals.txt file.
    File f2 = new File("Withdrawals.txt");
    Scanner inputFile = new Scanner (f2);

    i get an error tha says:
    "Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - inputFile is already defined in main(java.lang.String[])
    at SavingsAccountTest2.main(SavingsAccountTest2.java: 55)

    =/

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,405
    Thanks
    49
    Thanked 2,213 Times in 2,186 Posts

    Default Re: Input file Not Opening =/

    Change the name to inputFile2
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. opening a new jframe using a seperate file
    By bigblondguy in forum AWT / Java Swing
    Replies: 2
    Last Post: November 6th, 2011, 06:16 AM
  2. Windows 7 - jar file not opening a window
    By Yaten13 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 10th, 2011, 05:53 PM
  3. Opening a file with a java program
    By hafunui in forum Java Theory & Questions
    Replies: 4
    Last Post: March 1st, 2011, 03:52 PM
  4. Opening a File for User
    By aussiemcgr in forum Java Theory & Questions
    Replies: 3
    Last Post: July 29th, 2010, 03:00 AM
  5. Replies: 8
    Last Post: January 6th, 2010, 08:59 AM