• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to Find/open a file basedon user entry?  RSS feed

 
saeid jamali
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I am trying to find a way to be able to get the file name from user and open it so I can write it into an array.
If it's just one file we can set up new input as:


But what if We have more than one file and We want user to choose the file.
I tried this:

but this only checks whether or not the file exists.
Any suggestions?

Thank you

 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe I don't understand the question, but what's wrong with

[Edit] or maybe better:
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds like a case for a file chooser.
There are several ways you can take entry from a user. But letting them find the file would be more reliable.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
saeid jamali wrote:but this only checks whether or not the file exists.
Any suggestions?

I hate to say, but this is one case where a GUI component might work better. Have a look at JFileChooser. It seems pretty easy to use, and I think it may even allow your user to choose several files at once. It certainly allows you to filter what they see (eg: only .txt files).

And AFAIK, it's only a dialog window, so you don't need to convert your entire program to a GUI to use it.

HIH

Winston
 
saeid jamali
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:Maybe I don't understand the question, but what's wrong with

[Edit] or maybe better:


That's ok when you know which file you want to open. what if the user has to choose the file. for example you have 5 files and you want to get the name from user (I know how to get the input from user) but when you get the input from the user and they enter the file name now I need set up the file automatically.

I was thinking of a if statement after getting the name of the file from user but it does not work.


I have 5 files and I want the user to enter the file name and I want to set up and open then file for any operation. printing it or reading into an array or ...
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
saeid jamali wrote: . . . That's ok when you know which file you want to open. . . .
It is even better when you are not using “file” on both sides of the = sign 

Don't try lots of if‑elses like that. If the name entered is “Jan”, then you can create the name of the file like this:-
"c:/MyFiles/Sales/" + monthName + "SalesFile.txt"
Your inconsistent indentation makes your code very hard to understand. You will confuse yourself like that, and if this is assessed work, lose marks.
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote: It seems pretty easy to use, and I think it may even allow your user to choose several files at once. It certainly allows you to filter what they see (eg: only .txt files).

And AFAIK, it's only a dialog window, so you don't need to convert your entire program to a GUI to use it.

HIH

Winston

Correct.
 
saeid jamali
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
saeid jamali wrote: . . . That's ok when you know which file you want to open. . . .
It is even better when you are not using “file” on both sides of the = sign 

Don't try lots of if‑elses like that. If the name entered is “Jan”, then you can create the name of the file like this:-
"c:/MyFiles/Sales/" + monthName + "SalesFile.txt"
Your inconsistent indentation makes your code very hard to understand. You will confuse yourself like that, and if this is assessed work, lose marks.


You are a genius. A simple and useful way. I was trying complex things and I forgot all about simple ways. Thank you
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
saeid jamali wrote:You are a genius.

Steady on. He won't get through the door... :

A simple and useful way. I was trying complex things and I forgot all about simple ways. Thank you

There is a quote often attributed to Einstein:

  Everything should be as simple as possible, but no simpler.

It's actually a misquote, but IMO, it's one that all programmers should know.
Sometimes problems just aren't simple; but you should try to keep the solution as simple as possible. And if that fails, there's always:

  If you can't make it simple, make it readable.

Winston
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
saeid jamali wrote:. . . You are a genius. . . . Thank you
Not at all. All you have to do to find that sort of solution is keep your eyes open for ten years and remember a few of the things you see.

And, “You're welcome”.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!