• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

File Not Found Exception not working

 
Tracy Villa
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I am trying to print out "That is not a valid file" when a user inputs a file that does not exist. However, when I enter a file that does not exist in my program, it comes up with this:

This is my code:

Help would greatly be appreciated!
 
Maneesh Godbole
Saloon Keeper
Posts: 11165
14
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In future, while posting code, please UseCodeTags. I have added them for you this time. As you can see, the tags make the code much more easier to read and understand.

You are checking if the file exists after you try to create the Scanner! Check it first and create the Scanner only if it exists.
 
Tracy Villa
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I apologize, I will use them in the future!

Thank you so much I have a new problem however...

I changed my if statement into a while loop. When I input a file that exists, the loop stops. However, when I put a file that doesn't exist, it tells me that the file does not exist and prompts me to enter again. Then when I input a file that DOES exist, it tells me that it does not exist and becomes an infinite loop...

I'm very confused
 
Maneesh Godbole
Saloon Keeper
Posts: 11165
14
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tracy Villa wrote:
I'm very confused

So are we
Show us the code to help us help you!
 
Tracy Villa
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


 
Maneesh Godbole
Saloon Keeper
Posts: 11165
14
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


What do you think is happening to lunchFile (on 1) at 2? Did the lunchFile object change?
 
Tracy Villa
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes it did change...I'm unsure how to go about it. Am I approaching this in the wrong manner?

 
Maneesh Godbole
Saloon Keeper
Posts: 11165
14
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are almost there.
Change to
The first creates a new inputfile (but your while condition still refers to the original)
The second points to the original. Now your while condition will evaluate with the changed file object
 
Tracy Villa
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ohh, I see. It works now Thank you so much for the help; I appreciate it!
 
Maneesh Godbole
Saloon Keeper
Posts: 11165
14
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are Welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic