Results 1 to 10 of 10
  1. #1
    ronyosi is offline Member
    Join Date
    Jun 2008
    Posts
    4
    Rep Power
    0

    Default trouble with Scanner(new File("input"));

    Hello,

    I am having trouble with the File method, it gives the error, even though the files input1 and input2 are plain text files and located in the directory where it is executing....

    "Exception in thread "main" java.lang.Error: Unresolved compilation problems:
    Unhandled exception type FileNotFoundException
    Unhandled exception type FileNotFoundException

    at compare.main(compare.java:9)

    "

    My code is:

    import java.io.*;
    import java.util.Scanner;

    public class compare
    {
    public static void main(String[] args)
    {
    PrintStream output = System.out;
    Scanner input = new Scanner(new File("input1"));
    Scanner input2= new Scanner(new File("input2"));



    int diffs=0;
    while(input.hasNext())
    {
    if(input.next() == input2.next())
    {
    output.println("The characters are inconsistant");
    diffs++;
    }
    }
    if(diffs == 0)
    {
    output.println("These two text files are identical");
    }
    else
    {
    output.println(diffs + " Characters are inconsistant");
    }


    input.close();
    input2.close();


    }
    }


    thanks,
    Ron

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    are plain text files and located in the directory where it is executing
    And this may be your very problem. This is likely not where Java is looking for your files. To find out where Java is looking, run this program from the same package:
    Java Code:
    import java.io.File;
    
    public class Fubar1
    {
      public static void main(String[] args)
      {
        File file = new File("Fubar.txt");
        System.out.println(file.getAbsolutePath());
      }
    }
    Also, when posting your code, please use code tags so that your code will retain its formatting and be readable. To do this, you place the tag [code] at the top of your block of code and the tag [/code] at the bottom, like so:

    Java Code:
    [code]
      // your code block goes here.
      // note the differences between the tag at the top vs the bottom.
    [/code]

  3. #3
    ronyosi is offline Member
    Join Date
    Jun 2008
    Posts
    4
    Rep Power
    0

    Default

    Hi,

    thanks for the quick reply :-)
    I am using eclipse. The files are in the src directory, and I also checked and saw that eclipse copied the very same files (input1 and input2) to the bin directory (where the compiled class files are when the interpreter runs them.

    Cheers,
    Ron

  4. #4
    ronyosi is offline Member
    Join Date
    Jun 2008
    Posts
    4
    Rep Power
    0

    Default

    oh, sorry I misread what you wrote. I will try this now. :-)

  5. #5
    ronyosi is offline Member
    Join Date
    Jun 2008
    Posts
    4
    Rep Power
    0

    Default

    Hi,

    The method outputted:
    /home/ronny/workspace/Textbook/Fubar.txt

    Based on that (since the directories are the same), I tried moving the 2 files to the directory that was found, the error remains:

    Exception in thread "main" java.lang.Error: Unresolved compilation problems:
    Unhandled exception type FileNotFoundException
    Unhandled exception type FileNotFoundException

    at compare.main(compare.java:11)

    Ron

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    so I assume that you put the files into the
    "/home/ronny/workspace/Textbook/"
    directory, correct? I'm confused. You are 100% sure that you are using the full file names, that you're not missing a simple extension such as ".txt" or something similar? It's probably a simple mistake here that we are both overlooking.

    What you need to do is try to do something even simpler than what you are doing now. Just simply try to open a simple text file, read the lines and output them to the console as you read them. If you can get that working, then you will likely get this working.

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    That's much better I think, working on a simple application. Seems to me he may do the same mistake again. Because on his past thread also, ask about FilwNotFoundException.

  8. #8
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    Exception in thread "main" java.lang.Error: Unresolved compilation problems:
    Unhandled exception type FileNotFoundException
    Unhandled exception type FileNotFoundException
    That means, the specific code block where the operation that deals with persisted data MIGHT throw a FileNotFoundException if that file doesn't exists, the compiler tells you that you have to catch such type of exception before running it....

    or else, your program might behave badly....

    that scenario may relate just like this,
    Java Code:
    public int logic(Object o){
         if(o.equals("hello")
              return 0;
    }
    The sample code will return 0 if the object equals to "hello",

    The compiler will then ask you like this,
    what if the object is not equal to hello? what value should that method return?
    Last edited by sukatoa; 06-16-2008 at 12:36 PM.
    freedom exists in the world of ideas

  9. #9
    mkay is offline Member
    Join Date
    Oct 2010
    Posts
    2
    Rep Power
    0

    Default

    hello,
    i also got a problem with the Scanner class,
    i want the Scanner to read data from a file...
    Scanner sc = new Scanner(new File("file"));
    ->this works,
    but now i'm trying to type the file as a parameter in the console, because i don't want to change the code each time i load a different file
    i tried this..
    Scanner sc = new Scanner(new File(System.in));
    but - fail.
    maybe sb can help me please, thy mkay (:

  10. #10
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Quote Originally Posted by mkay View Post
    hello,
    i also got a problem with the Scanner class,
    i want the Scanner to read data from a file...

    ->this works,
    but now i'm trying to type the file as a parameter in the console, because i don't want to change the code each time i load a different file
    i tried this..

    but - fail.
    maybe sb can help me please, thy mkay (:
    Start a new thread and don't use text speak; not all contributors have English as there first language

Similar Threads

  1. Hwlp with "Open", "Save", "Save as..."
    By trill in forum New To Java
    Replies: 3
    Last Post: 11-02-2010, 10:26 AM
  2. "Jumble" or "Scramble" Program
    By Shadow22202 in forum Java Applets
    Replies: 8
    Last Post: 04-30-2008, 04:42 AM
  3. ".hotjava/properties" file on mac
    By willemjav in forum Java Applets
    Replies: 0
    Last Post: 03-08-2008, 01:53 AM
  4. Replies: 1
    Last Post: 07-27-2007, 11:59 PM
  5. Replies: 1
    Last Post: 07-25-2007, 11:55 PM

Posting Permissions

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