Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 13 of 13

Thread: User Input File Name

  1. #1
    Junior Member
    Join Date
    May 2011
    Location
    Boulder, Colorado
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default User Input File Name

    I'm optimizing a java program and one of the aspects I would really like to integrate into my program is an message box which prompts the user to input the name of an excel file the program writes. I've been using the JOptionPane to easily construct my message box but I'm unsure of how to return this string and then assign this as the name to the created .xls file. I've been playing around with BufferedReader and readLine() but I was just wondering if I'm on the right track or if I should be going at this another way?


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: User Input File Name


  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: User Input File Name

    using the JOptionPane to easily construct my message box but I'm unsure of how to return this string
    The JOptionPane class has several methods that return a String.

  4. #4
    Junior Member
    Join Date
    May 2011
    Location
    Boulder, Colorado
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: User Input File Name

    Quote Originally Posted by Norm View Post
    The JOptionPane class has several methods that return a String.
    I can easily return the string to the system but i'd like to return this string to the system and then assign this string as the name of a .xls file.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: User Input File Name

    i'd like to return this string to the system and then assign this string as the name of a .xls file.
    Sorry, I don't understand your problem.
    If you get the filename into a String variable, you can use it in your program.

    What is "the system" that you talk about?

  6. #6
    Junior Member
    Join Date
    May 2011
    Location
    Boulder, Colorado
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: User Input File Name

    By system I just meant visible in the console of Eclipse when I run the application. I know how to enter a string variable but I don't know how to actually have the program use that "user defined" name for the purpose of saving an exported excel file of data. For example if I enter the string TrialTGFB in a message box I would then like the program to create and name an excel file, TrialTGFB.xls.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: User Input File Name

    don't know how to actually have the program use that
    Sorry, I can't understand your problem here. Once you get the user enter data into a String variable, then you can use that variable in a file class's constructor.
    If the problem is concatenating the extension: ".xls" to the data in the String:
    String fName = userEnterData + ".xls"; // add the file extension to user's data

  8. #8
    Junior Member
    Join Date
    May 2011
    Location
    Boulder, Colorado
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: User Input File Name

    I guess my question is I am creating an excel spreadsheet from exported data. I need to create a message box that allows the user to name the file and then once this name has been indicated I need my program to write this file. How should I go about this?

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: User Input File Name

    Sorry, I still can't see the problem you are having.
    You get the name of the file from the user.
    You create an object of a class to write to that file using the name given above in the class's constructor.
    (I don't know what class you should use here.)
    You call methods on this class to write the data to a file.

  10. #10
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: User Input File Name

    See the link I posted above. A JFileChooser is like any file chooser you see in many applications, which allows users to select the location to place a file and define a name for the file. You can then retrieve the File object after the selection has been made and use this to write the file (for example open up a FileOutputStream), if you wish adding whatever extension you want to the name. If you are using POI to write the .xls file, then pass a FileOutputStream to the workbook to write the file.

  11. #11
    Junior Member
    Join Date
    May 2011
    Location
    Boulder, Colorado
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: User Input File Name

    Thanks Copeg I think the Save Dialog of JChooser is exactly what I'd like to implement however I'm a little confused on how to integrate the JChooser into the portion of my code where I use FileOutputStream to write the excel file. Do I want to actually write the file with FileOutput and then pass it to the JChooser or will the JChooser actually end up writing the file?

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: User Input File Name

    I'd suggest you write a small simple program to use JFileChooser and print out what its various methods return. Use it several ways such as selecting an existing file or entering the name of a new file.

  13. #13
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: User Input File Name

    The file chooser just lets the user tell you where the file should go. You use this information to create and write a file at that location.

Similar Threads

  1. Allow user input to edit text file??
    By dannyyy in forum Java Theory & Questions
    Replies: 2
    Last Post: April 6th, 2011, 06:53 AM
  2. Help with making the user input to store in file
    By dannyyy in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 6th, 2011, 06:52 AM
  3. user input strings to vectors
    By vudoo in forum What's Wrong With My Code?
    Replies: 9
    Last Post: February 7th, 2011, 10:30 PM
  4. Allowing user to input variable.
    By That1guy in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 15th, 2010, 11:30 PM
  5. User Input Loop
    By cfmonster in forum Loops & Control Statements
    Replies: 7
    Last Post: August 24th, 2009, 01:52 PM