• Post Reply Bookmark Topic Watch Topic
  • New Topic

Checking for a files existence based on user input  RSS feed

 
Nikki Smith
Ranch Hand
Posts: 65
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need a user to be able to input a file name and then check to see if it exists. If it doesn't, then the question needs to be repeated until the user inputs a valid file name. This is what I have so far, but it's giving me an error and I don't understand. We've just started learning how to work with files and it's really confusing me.




***EDIT***

I'm trying to go about it in a different way. Feel like I'm way off base here. It still won't work.

 
Carey Brown
Saloon Keeper
Posts: 3318
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your second bit of code won't compile. Exactly what input are you giving it, and why don't you think it's working? Have you tried inputting a fully qualified path name?
 
Carey Brown
Saloon Keeper
Posts: 3318
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would have preferred
 
Nikki Smith
Ranch Hand
Posts: 65
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't think the first code would work because in the while condition fileName isn't initialized to anything so checking equality is meaningless in that case. I realized I actually need it to use the userinput and store it in File(userInput) only if the while loop determines the file path to be valid. Basically, if a user inputs an invalid file name, then it needs to continually loop and ask the user to input a file again and again until it's valid.
 
Carey Brown
Saloon Keeper
Posts: 3318
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:Have you tried inputting a fully qualified path name?

Depending on how the Java is being executed (e.g. from IDE or BAT file), if you don't use a full path name you'll end up with a relative path to the file name, but, relative to where? It's not always obvious.

And, yes, your second piece of code is heading in the right direction, but it would help if you posted code only after you get it to compile.
 
Nikki Smith
Ranch Hand
Posts: 65
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is, I can't get it to compile and I don't understand why. I can't use .isFile method though, because we haven't learned about it. Not allowed to use things we haven't 'officially' learned. The second code with the if else keeps giving me an error saying theres a missing semicolon at the end of the if () thing, but that doesn't make sense. Also, if it's in an if else block would it still work? Because if it falls through the if part because it's wrong, then drops into the else section and loops around, but when its in the else block the user enters a valid file where would I put a statement that would store that in that condition is met in the else block? I'm so confused.
 
Carey Brown
Saloon Keeper
Posts: 3318
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your while() condition has mismatched parens and is missing a semicolon at the end.

On line 16 you are assigning a String to a File which won't work.
 
Nikki Smith
Ranch Hand
Posts: 65
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But once it drops into the else part it will check if it exists, but at that point there's no way for it to store it like



I'm not sure where that line would fit in.
 
Carey Brown
Saloon Keeper
Posts: 3318
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"fileName" looks like a name to me which is usually a String.

Whether your "if()" is true, or it executes the "else" block, when it's done, the file name is already in userInput. When it done you could assign it to fileName, or you could have the scanner input go directly into fileName.
 
Nikki Smith
Ranch Hand
Posts: 65
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Something like this maybe? That way every time the user enters something invalid, it prompts again and kinda rewrites the new File(userInput) each time the user enters something until it's right.

 
Carey Brown
Saloon Keeper
Posts: 3318
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nikki Smith wrote:But once it drops into the else part it will check if it exists, but at that point there's no way for it to store it like



I'm not sure where that line would fit in.

Chose your variable names carefully. A "File" is much more than a name. You could have something more like
 
Carey Brown
Saloon Keeper
Posts: 3318
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Close. I might have slightly simplified it like this

As a minor hint I'd suggest carefully choosing when to use compound expressions. Sometimes it makes things more difficult to read.

Also suggest using more white space. Again, for readability.
 
Carey Brown
Saloon Keeper
Posts: 3318
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nikki Smith wrote:Something like this maybe? That way every time the user enters something invalid, it prompts again and kinda rewrites the new File(userInput) each time the user enters something until it's right.


Here, lines 8 & 9 both create a new File object for the same file. you should avoid this kind of redundancy. Put it into a variable once and reuse it.
 
Nikki Smith
Ranch Hand
Posts: 65
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:
Here, lines 8 & 9 both create a new File object for the same file. You should avoid this kind of redundancy. Put it into a variable once and reuse it.


That makes sense.

Also I tried to work out the other way I had tried to write it, just because I wanted to see if it would at all work. This is what I came up with:




Does one way seem better than the other?

Really appreciate the help by the way. Thank you.
 
Carey Brown
Saloon Keeper
Posts: 3318
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I like the last way the best. Now you have lines 9, 12, and 18 creating File's. You should be able to reduce the redundancy a bit.
 
Carey Brown
Saloon Keeper
Posts: 3318
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My mistake. Disregard last post.
 
Knute Snortum
Sheriff
Posts: 4279
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does one way seem better than the other?

Personally, I like the do-loop better, but writing the while-loop is a good exercise. The while-loop has one advantage: you can change the prompt after the first time. One slight disadvantage is that you duplicate the Scanner input line, but this is minor.
 
Carey Brown
Saloon Keeper
Posts: 3318
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A minor nit. I always like to have error messages be as informative as possible. I might change

to
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!