1

I'm trying to pass a file to another method which takes a String as its argument, but when I compile it I get contradicting exceptions.

The method I'm calling is:

public static String sortThis(String inputFileName) 
{
//code here
}

The code where I'm calling the method is:

tempFile1 = Sort2.sortThis(tempFile2.getPath());

and I get the exception:

incompatible types
Found: java.lang.String
Required: java.io.File

So if I just pass the file:

tempFile1 = Sort2.sortThis(tempFile2);

I get:

sortThis(java.lang.String) in Sort2 cannot be applied to (java.io.File)

Am I using the wrong method of File to get the file name/path? I'm not sure what the difference is between getAbsolutePath(), getCanonicalPath(), and getPath(), so that could be the reason it's acting strangely?

1 Answers1

0

I'm assuming your tempFile1 is of type File instead of String

The following compiles and works.

import java.io.File;

public class Test
{
    public static void main( String args[] )
    {
        File file = new File( "" );
        String tempFile1 = Test.sortThis( file.getPath() );
        System.out.println( tempFile1 );
    }

    public static String sortThis( String inputFileName )
    {
        return inputFileName;
    }
}
Jonathan Payne
  • 2,223
  • 13
  • 11