Results 1 to 13 of 13
  1. #1
    merehephet is offline Member
    Join Date
    Nov 2009
    Posts
    7
    Rep Power
    0

    Question String to file name

    Apart from a command-line argument, can I ask a user to input the name of a .txt file which can then be read by the program?

    I have tried, for example:

    System.out.print("Enter the file name, eg, NameOfFile.txt");
    Scanner myScanner = new Scanner(System.in);
    String myFile = myScanner.next();
    Scanner diskScanner = new Scanner(new File(myFile)); // this line doesn't work..

    Can I get the new File() to take a String value that is inputed by the user?

    I left out the throws IOException from my example but have included it in the source..

    The diskScanner works if I write = new Scanner(new File("NameOfFile.txt"));

    but I want the user to able to enter the data when the program is running.
    Last edited by merehephet; 11-17-2009 at 03:33 PM.

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    Define this line doesn't work. stack trace?
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    merehephet is offline Member
    Join Date
    Nov 2009
    Posts
    7
    Rep Power
    0

    Default

    Sorry, I don't understand

  4. #4
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    You posted: Scanner diskScanner = new Scanner(new File(myFile)); // this line doesn't work..

    What does it do? Does that line let your stove explode?
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  5. #5
    merehephet is offline Member
    Join Date
    Nov 2009
    Posts
    7
    Rep Power
    0

    Default string to filename

    yes that's correct.

    But apart from that, the program can read from the text file if I place the name of the file in quotation marks suffixed by .txt within the source code.

    I created a String whose value is inputted by the user. I tried to use this String as the file name but the program is not able to read the String as a filepath.


    I wish to know if it is possible to convert a String to a filepath and then pass this to the new File() method.

  6. #6
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    What does it do? Does it throw an exception? Does it make something explode? Jeez
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  7. #7
    merehephet is offline Member
    Join Date
    Nov 2009
    Posts
    7
    Rep Power
    0

    Default String to file name

    No, it doesn't throw an exception. I'll have to look at it later. I'm not using my own pc at the moment. And relax a little with the sarcasm and the exasperation. Even you were a beginner at some point.

  8. #8
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    What is that supposed to mean? Do you also bring your car to the mechanic and tell him that it doesn't work? If you want help, provide at least some information.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  9. #9
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    You were asked about the error in the very first reply, you should have stated that you can't get it then. It's pointless to ask help about an error when you can't provide the error message.
    Statements like "If you don't know how to help it is okay to admit your limitations" will get you nowhere when it is all clearly your fault. You should be grateful that you are getting free help.

  10. #10
    merehephet is offline Member
    Join Date
    Nov 2009
    Posts
    7
    Rep Power
    0

    Default

    Okay, apologies.

  11. #11
    merehephet is offline Member
    Join Date
    Nov 2009
    Posts
    7
    Rep Power
    0

    Default

    Thank you for replying to my posts. I am sorry I was so quick to be defensive. I initially didn't quite understand what you were asking for and it was utterly unacceptable for me to question your credentials.

  12. #12
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    No worries, keep cool. If you get upset by forum posts anywhere, step away from the keyboard and think before posting. You have to get used to the form of communication, which varies from forum to forum.

    EDIT: I just posted that: The Ubiquitous Newbie Tips
    Last edited by PhHein; 11-17-2009 at 04:01 PM.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  13. #13
    ocean is offline Senior Member
    Join Date
    Sep 2009
    Location
    Sweden/Borås
    Posts
    107
    Rep Power
    0

    Default

    If im not all lost

    Java Code:
    String myFile = myScanner.next();
    My file has the info you need.

    Java Code:
    File file = new File(myFile);
    Would do what you want

Similar Threads

  1. Creating PDF file from a String
    By faroafro in forum Advanced Java
    Replies: 6
    Last Post: 11-16-2009, 11:38 PM
  2. get string from a text file
    By Kruptein in forum New To Java
    Replies: 11
    Last Post: 08-14-2009, 04:45 PM
  3. how to store string in text file
    By santhosh_el in forum AWT / Swing
    Replies: 2
    Last Post: 04-03-2009, 06:21 AM
  4. can i update the tag value of XMl file by other value(string
    By pankaj_salwan in forum Advanced Java
    Replies: 7
    Last Post: 07-04-2008, 08:12 AM
  5. Read a string from a txt file
    By cachi in forum Java Applets
    Replies: 1
    Last Post: 08-07-2007, 07:50 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •