PDA

Click to See Complete Forum and Search --> : code needs a quick look =C)


Dillinger4
May 20th, 2001, 02:13 PM
Does anyone know why i would be an error would be generated
from this code. I keep getting:
static public String createFile(){
^

missing return statement.


I dont understand, im returning a String? I should be able to invoke a method from within a constructor call right???

import java.io.*;

class fileTest{
public static void main(String[] args){

try{
FileOutputStream fout = new FileOutputStream(createFile());
}catch(IOException e){System.err.println(e);}


} // end main


static public String createFile(){
File f = new File("C:\tempfile.txt");

try{
f.createNewFile();
String filename = f.getName();
return filename;
}catch(IOException e){System.err.println(e);}
}



} // end class

Dillinger4
May 20th, 2001, 03:57 PM
I finally got it but i still want to return a String from a method to initialize an FileOutputStream. So my question still stands. Does any one know if i can invoke a method from within a constructor call? And if anyone knows of an easier way to create a Directory and a file on the fly that would be cool. Thanks........


import java.io.*;

class fileTest{
public static void main(String[] args){

createFile();

} // end main


static public void createFile(){
try{
File dir = new File("C:\\BrandonsDirectory");
dir.mkdir();
File f = new File("TempFile.txt");
f.createNewFile();
File dest = new File("C:\\BrandonsDirectory\\" + f.getName());
f.renameTo(dest);

String dirname = f.getParent();
String filename = f.getName();
System.out.println(dirname);
System.out.println(filename);

}catch(IOException e){System.err.println(e);}
}
} // end class

billrogers
May 21st, 2001, 04:11 PM
You can invoke a method as a parameter, i do not see anything sticking out to me on what is wrong with the code, but I dont have a java compiler at home, so i will check it out at work manana.

Dillinger4
May 23rd, 2001, 10:18 AM
I wasnt too sure if i could. It makes sense to me that a constructor should be able to invoke a method and the method should be able to return a value that is used as an arguement by the constructor. Thanks..........

billrogers
May 23rd, 2001, 02:28 PM
i ran your code at work and got the same error. It is really starting to annoy me has to why it would think there is no return on that.

VirtuallyVB
May 24th, 2001, 02:47 PM
If you make sure that all paths of execution provide an appropriate return statement, then you should be good to go. You can add one in the catch clause, or have one only in a finally clause. Or if you scope filename properly, you can have only one outside and after the try/catch/finally clauses.

The way your first post stands, it is possible that an exception will be thrown before reaching your present return statement and then there will be a "missing return statement" due to execution flow through the catch clause.