Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2010
    Posts
    59
    Rep Power
    0

    Cool Converting a File Format Variable To String

    I would be grateful if someone could help me. I need to change a file format variable to a string, but do not know how to do this.

    What I am doing is shown in the below code:

    File file;
    file=filepicker.filechoice();

    (I have created a variable called file which is in a FILE FORMAT. I have a class which gets the file using JFILECHOOSER. No problems here!)

    data=analysis3.vowel(file);

    What I want to do know is to use the class analysis3.vowel to do a process with the file. Here are the two key lines in analysis3.vowel which are causing the problems for me.

    public static int[] vowel(File filename)
    {
    File inFile = new File(filename); // create File object

    When I try compiling I get the following error:

    analysis3.java:33: cannot find symbol
    symbol : constructor File(java.io.File)
    location: class java.io.File
    File inFile = new File(filename); // create File object
    ^
    1 error

    The program does not like the filename in the second line. It works fine if filename is a string variable. So, I believe I need to (1) either change the filename to a string formatted variable or (2) do something different!

    Please can anyone tell me how to do either solution?

    Many thanks!

  2. #2
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    10

    Default

    Looks like you probably want file.getName() but read this anyway:

    How to Use File Choosers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)

    -Gary-

  3. #3
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    312
    Rep Power
    11

    Default

    If you already have the File Object, why are you trying to recreate it here?
    Java Code:
    File inFile = new File(filename); // create File object
    Why don't you just use the File parameter?

  4. #4
    Join Date
    Apr 2010
    Posts
    59
    Rep Power
    0

    Cool

    Thank you for the posts.

    gcalvin writes:
    "Looks like you probably want file.getName() ..."

    I believe that only gives the name of the file (not the location of the file) so is no use. I don't see where in the coding one is going to use file.getName().


    Meanwhile, StormyWaters writes "If you already have the File Object, why are you trying to recreate it here?"

    Code:
    File inFile = new File(filename); // create File object

    Why don't you just use the File parameter?


    Are you saying one does not need to use
    File inFile = new File(filename); // create File object

    Or it should be
    File inFile= File(filename);

    ??

    Many thanks.

  5. #5
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    312
    Rep Power
    11

    Default

    I'm saying if you are passing the method the File, why do you try to make a new one? Why not use the File you passed the method?

    So it would be
    Java Code:
    public static int[] vowel(File inFile) {
         //Process using the parameter inFile
    }
    Last edited by StormyWaters; 04-15-2010 at 09:50 PM. Reason: clarity

Similar Threads

  1. Converting to String
    By darek9576 in forum New To Java
    Replies: 1
    Last Post: 03-13-2010, 10:07 PM
  2. Is it OK to do this... (converting int to string)
    By Psyclone in forum New To Java
    Replies: 1
    Last Post: 02-16-2010, 05:51 PM
  3. converting .pdf to .doc(microsoft word)format
    By ashik03 in forum Java Applets
    Replies: 1
    Last Post: 01-30-2010, 02:07 PM
  4. Read a file and converting this file into a string
    By kostinio in forum New To Java
    Replies: 7
    Last Post: 12-26-2009, 03:54 PM
  5. Converting ASCII format
    By right2001 in forum New To Java
    Replies: 7
    Last Post: 02-17-2009, 03:55 PM

Posting Permissions

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