Results 1 to 12 of 12
  1. #1
    Beginner's Avatar
    Beginner is offline Member
    Join Date
    Apr 2010
    Posts
    25
    Rep Power
    0

    Default Help with FileNotFoundException

    I'm learning Java from a book, and I'm sure I copied everything exactly, but if I use the example code from the book, it has errors. Maybe outdated? Please check the code.

    Java Code:
    throws FileNotFoundException {
                Scanner diskScanner = new Scanner(new File("rawData.txt"));
                PrintStream diskWriter = new PrintStream("cookedData.txt");
                
                double unitPrice, quantity, total;
    
                unitPrice = diskScanner.nextDouble();
                quantity = diskScanner.nextInt();
    
                total = unitPrice * quantity;
                diskWriter.println(total);
            }
    I imported java.io.File, java.io.FileNotFoundException, java.io.PrintStream

  2. #2
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    This is everything in your code file? You're missing quite a bit. What's the book?

    -Gary-

  3. #3
    Beginner's Avatar
    Beginner is offline Member
    Join Date
    Apr 2010
    Posts
    25
    Rep Power
    0

    Default

    I excluded the package, imports, and main, but thats all there is inside main. The book I learn from is "Beginning Programming With Java For Dummies, 2nd Edition"

  4. #4
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    Show us your entire code file please.

    -Gary-

  5. #5
    Beginner's Avatar
    Beginner is offline Member
    Join Date
    Apr 2010
    Posts
    25
    Rep Power
    0

    Default

    Java Code:
    package myworkspace;
    
    import java.util.Scanner;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.PrintStream;
    public class Main {
    
        
        public static void main(String[] args) {
            throws FileNotFoundException {
                Scanner diskScanner = new Scanner(new File("rawData.txt"));
                PrintStream diskWriter = new PrintStream("cookedData.txt");
                
                double unitPrice, quantity, total;
    
                unitPrice = diskScanner.nextDouble();
                quantity = diskScanner.nextInt();
    
                total = unitPrice * quantity;
                diskWriter.println(total);
            }
        }
    }
    This is everything

  6. #6
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    Here is your problem:
    Quote Originally Posted by Beginner View Post
    Java Code:
        public static void main(String[] args) {
            throws FileNotFoundException {
    You have an extra { there. This is all one method declaration. The book may split it into two lines because of space, but it's logically this:
    Java Code:
            public static void main(String[] args) throws FileNotFoundException {
    -Gary-

  7. #7
    Beginner's Avatar
    Beginner is offline Member
    Join Date
    Apr 2010
    Posts
    25
    Rep Power
    0

    Default

    Oh that was the problem. Thanks. But now it has a compile error:

    Exception in thread "main" java.io.FileNotFoundException: rawData.txt (The system cannot find the file specified)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.jav a:106)
    at java.util.Scanner.<init>(Scanner.java:636)
    at myworkspace.Main.main(Main.java:25)

    I tried to create the file myself besides the .class, .java files, still didn't help

  8. #8
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    That's not a compile error, it's a runtime error. Where are you putting the rawData.txt file? Are you using an IDE?

    -Gary-

  9. #9
    Beginner's Avatar
    Beginner is offline Member
    Join Date
    Apr 2010
    Posts
    25
    Rep Power
    0

    Default

    Isn't it suppose to make the txt file itself? Im using NetBeans.

    I tried placing the rawData.txt and cookedData.txt besides all the files, with the .class file, then with the .jar, then with the .java, but all of them still gives the error.

  10. #10
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    It will create cookedData.txt, but it needs to read from rawData.txt -- that's what a Scanner does. I don't use NetBeans, so maybe somebody else can comment on the layout it uses, but I can tell you the file should not be in a src or bin directory, but probably in the project's root directory.

    -Gary-

  11. #11
    Beginner's Avatar
    Beginner is offline Member
    Join Date
    Apr 2010
    Posts
    25
    Rep Power
    0

    Default

    I finally got it to work! Thanks Gary!

    edit: I gave you +rep!

  12. #12
    siarzuk is offline Member
    Join Date
    Nov 2010
    Posts
    1
    Rep Power
    0

    Default

    Hi, can you tell me how did you get it to work. I have the same problem

Similar Threads

  1. FileNotFoundException
    By ProgrammingPup in forum Advanced Java
    Replies: 4
    Last Post: 12-30-2009, 02:29 AM
  2. filenotfoundexception :@
    By wildheart in forum New To Java
    Replies: 2
    Last Post: 04-25-2009, 10:56 AM
  3. handle FileNotFoundException
    By minifish in forum New To Java
    Replies: 13
    Last Post: 11-08-2008, 03:01 AM
  4. java.io.FileNotFoundException
    By thevoice in forum New To Java
    Replies: 5
    Last Post: 06-13-2008, 09:38 AM
  5. FileNotFoundException
    By PeonLover in forum New To Java
    Replies: 1
    Last Post: 12-26-2007, 07:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •