Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

How to Find/open a file basedon user entry?

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Sheriff
Posts: 7111
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe I don't understand the question, but what's wrong with

[Edit] or maybe better:
 
Marshal
Posts: 74067
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 74067
332
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 4633
182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 74067
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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”.
 
It runs on an internal combustion engine. This ad does not:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic