Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Can't get java to read my file input

807588Mar 23 2009 — edited Mar 23 2009
I have a program that ask at prompt what the name of your input file will be... something for example like input.txt can be typed and a scanner puts that text "input.txt" into a string called maybe inputs.

The problem I am having is I can not get the scanner to then look at the string and open the file with the same name in the folder and read it.

I not sure if im putting the name correct in the new File so to make it look at "input.txt" and I keep getting Class FileNotFoundException
any ideas?

my code is below


File f = new File(inputs);
Scanner scan = new Scanner( f );
String name = "";


while(scan.hasNextLine()) // as long as there is another line to be read
{
String text = scan.nextLine(); // read the first line
Scanner scan2 = new Scanner(text); // set up a second scanner to process the String we just read in

if(scan2.hasNext())
{
name = scan2.next();
}
else
{
continue; // go to the next line
}

System.out.println(name);
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 20 2009
Added on Mar 23 2009
6 comments
1,289 views