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

Thread: scanner problems

  1. #1
    Junior Member
    Join Date
    Jun 2010
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default scanner problems

    i'm trying to use
     Scanner input = new Scanner(new File("wherefits.dat"));
    to load a file, but i get the exception

    Exception in thread "main" java.io.FileNotFoundException: wherefits.dat (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 blahblahblah.wherefits.main(wherefits.java:11)

    the file is in the same package as my java file in eclipse.

    my computer science teacher says it should be working.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: scanner problems

    Eclipse's base path is actually in the main project folder, not the source or bin folders.

    Move your file to the main project folder and it should work.

  3. #3
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: scanner problems

    I would say you should check all aspects of the location and name of the file.

    You should find the file and check the name for case and any other minor details. I'm not sure about Eclipse, but with my IDE, you should check that the file is in the same folder as your .class files. The .class files are what will run when you test the program, it will search that directory for the file.

    If the problem still exists after you do those checks, you should check the ability for the program to read the file. It probably isnt an issue, but it couldnt hurt. The easiest way to do this would be by create a JFileChooser and selecting the file manually. Here is the code for that so you don't have to worry about learning the GUI aspect yet(I have not tested it, but I've done this enough that I'm pretty sure I have it down pack. If there are any problems, tell me and Ill fix it):
    import javax.swing.JFileChooser;
    import java.io.File;
     
    public class Test
    {
    	public static void main(String[] args)
    	{
    		try{
    		JFileChooser fileChooser = new JFileChooser();
    		fileChooser.showOpenDialog(null);
    	    	File file = fileChooser.getSelectedFile();
    		System.out.println(file.getAbsolutePath());
    		}catch(Exception ex){System.out.println(ex);}
    	{
    }

    When the JFileChooser opens, go to the file, select it, and press Open. This should Open the file and print out the file's path. If there are any exceptions thrown, they will print to the console instead. If there is an error, please respond with the error so we can help from there.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: scanner problems

    Another very simple minded way, when all else fails, to find where a system like an IDE looks for files is to create many files with the same name and put one in each folder that could be looked in and have the first line of the file say what folder it is located in. When you execute your program print the first line read from the file which will tell you where the file was read from.

Similar Threads

  1. How to take a char value in scanner
    By andaji in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 1st, 2010, 02:50 AM
  2. [SOLVED] Problem with Scanner ?
    By derekeverett in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 19th, 2010, 04:34 AM
  3. Help With Scanner
    By jtphenom in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: October 12th, 2009, 08:49 PM
  4. Homework problem (scanner problems)
    By TommyFiz in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: September 20th, 2009, 06:10 PM
  5. network scanner
    By vivek494818 in forum Java Networking
    Replies: 0
    Last Post: August 17th, 2009, 11:07 PM