Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-15-2008, 04:54 PM
Member
 
Join Date: Jun 2008
Posts: 4
ronyosi is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 06-15-2008, 05:33 PM
Senior Member
 
Join Date: Jun 2008
Posts: 180
Fubarable is on a distinguished road
Quote:
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:
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:

Code:
[code] // your code block goes here. // note the differences between the tag at the top vs the bottom. [/code]
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 06-15-2008, 09:39 PM
Member
 
Join Date: Jun 2008
Posts: 4
ronyosi is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 06-15-2008, 09:40 PM
Member
 
Join Date: Jun 2008
Posts: 4
ronyosi is on a distinguished road
oh, sorry I misread what you wrote. I will try this now. :-)
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 06-15-2008, 09:49 PM
Member
 
Join Date: Jun 2008
Posts: 4
ronyosi is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 06-15-2008, 10:51 PM
Senior Member
 
Join Date: Jun 2008
Posts: 180
Fubarable is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 06-16-2008, 04:37 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,968
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
(Close on September 4, 2008)

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 06-16-2008, 12:33 PM
sukatoa's Avatar
Senior Member
 
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 524
sukatoa is on a distinguished road
Send a message via Yahoo to sukatoa
Quote:
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,
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?
__________________
A specific, detailed, simple, well elaborated, and "tested before asking" question may gather more quick replies. hopefully
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Last edited by sukatoa : 06-16-2008 at 12:36 PM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
"Jumble" or "Scramble" Program Shadow22202 Java Applets 8 04-30-2008 04:42 AM
".hotjava/properties" file on mac willemjav Java Applets 0 03-08-2008 01:53 AM
Hwlp with "Open", "Save", "Save as..." trill New To Java 1 07-31-2007 08:53 AM
Exception in thread "main" java.net.ConnectException: Connection timed out osval Advanced Java 1 07-27-2007 11:59 PM
Error: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException romina New To Java 1 07-25-2007 11:55 PM


All times are GMT +3. The time now is 05:07 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org