aspose file tools*
The moose likes Beginning Java and the fly likes File Not Found Exception not working Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "File Not Found Exception not working" Watch "File Not Found Exception not working" New topic
Author

File Not Found Exception not working

Tracy Villa
Greenhorn

Joined: Nov 03, 2012
Posts: 28
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

Joined: Jul 26, 2007
Posts: 9990
    
    7

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.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Tracy Villa
Greenhorn

Joined: Nov 03, 2012
Posts: 28
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

Joined: Jul 26, 2007
Posts: 9990
    
    7

Tracy Villa wrote:
I'm very confused

So are we
Show us the code to help us help you!
Tracy Villa
Greenhorn

Joined: Nov 03, 2012
Posts: 28


Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 9990
    
    7



What do you think is happening to lunchFile (on 1) at 2? Did the lunchFile object change?
Tracy Villa
Greenhorn

Joined: Nov 03, 2012
Posts: 28
Yes it did change...I'm unsure how to go about it. Am I approaching this in the wrong manner?

Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 9990
    
    7

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

Joined: Nov 03, 2012
Posts: 28
Ohh, I see. It works now Thank you so much for the help; I appreciate it!
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 9990
    
    7

You are Welcome
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: File Not Found Exception not working
 
Similar Threads
Need a little help...
Trouble with getting input from keyboard
Write a program that merges two files containing alphabetized lists of student records into a single
Help with writing a program that gets student information from the user (Lengthy Post)
Scanning/Storing a String that the user enters?