Results 1 to 10 of 10
- 06-15-2008, 03:54 PM #1
Member
- Join Date
- Jun 2008
- Posts
- 4
- Rep Power
- 0
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
-
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:are plain text files and located in the directory where it is executing
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:import java.io.File; public class Fubar1 { public static void main(String[] args) { File file = new File("Fubar.txt"); System.out.println(file.getAbsolutePath()); } }
Java Code:[code] // your code block goes here. // note the differences between the tag at the top vs the bottom. [/code]
- 06-15-2008, 08:39 PM #3
Member
- Join Date
- Jun 2008
- Posts
- 4
- Rep Power
- 0
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
- 06-15-2008, 08:40 PM #4
Member
- Join Date
- Jun 2008
- Posts
- 4
- Rep Power
- 0
oh, sorry I misread what you wrote. I will try this now. :-)
- 06-15-2008, 08:49 PM #5
Member
- Join Date
- Jun 2008
- Posts
- 4
- Rep Power
- 0
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
-
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.
- 06-16-2008, 03:37 AM #7
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 06-16-2008, 11:33 AM #8
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....Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Unhandled exception type FileNotFoundException
Unhandled exception type FileNotFoundException
or else, your program might behave badly....
that scenario may relate just like this,
The sample code will return 0 if the object equals to "hello",Java Code:public int logic(Object o){ if(o.equals("hello") return 0; }
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 11:36 AM.
freedom exists in the world of ideas
- 10-27-2010, 11:23 PM #9
Member
- Join Date
- Oct 2010
- Posts
- 2
- Rep Power
- 0
hello,
i also got a problem with the Scanner class,
i want the Scanner to read data from a file...
->this works,Scanner sc = new Scanner(new File("file"));
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.Scanner sc = new Scanner(new File(System.in));
maybe sb can help me please, thy mkay (:
- 10-27-2010, 11:34 PM #10
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
Similar Threads
-
Hwlp with "Open", "Save", "Save as..."
By trill in forum New To JavaReplies: 3Last Post: 11-02-2010, 09:26 AM -
"Jumble" or "Scramble" Program
By Shadow22202 in forum Java AppletsReplies: 8Last Post: 04-30-2008, 03:42 AM -
".hotjava/properties" file on mac
By willemjav in forum Java AppletsReplies: 0Last Post: 03-08-2008, 12:53 AM -
Exception in thread "main" java.net.ConnectException: Connection timed out
By osval in forum Advanced JavaReplies: 1Last Post: 07-27-2007, 10:59 PM -
Error: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
By romina in forum New To JavaReplies: 1Last Post: 07-25-2007, 10:55 PM


LinkBack URL
About LinkBacks


Bookmarks