Results 1 to 7 of 7
- 04-14-2011, 11:22 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 30
- Rep Power
- 0
What Should be my Return type....
I am getting an error for return type. what should be the return type..
Java Code:class Ex123 { public static void main(String[] args) { String a="abc.txt"; String b="Hey Good Morning"; WriteToFile(a,b); } public WriteToFile(String f, String arr) { File f1=new File(this.f); f1.createNewFile(); FileWriter fstream = new FileWriter(f,true); BufferedWriter out = new BufferedWriter(fstream); out.write(this.arr); out.close(); } }
Help me how should i do this. is there any other method to do this
Thanks in Advance...
- 04-14-2011, 11:38 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 28
- 04-14-2011, 11:53 AM #3
Member
- Join Date
- Mar 2011
- Posts
- 30
- Rep Power
- 0
I am Trying to Pass File name and Data as parameters to the method,
Which creates a file with that file name and writing data to that created file.
But i Don't know where i am Wrong....
Please help me how to achieve is my code is wrong..
Waiting for positive reply..
Thanks in Advance.....
- 04-14-2011, 12:07 PM #4
Member
- Join Date
- Jan 2011
- Posts
- 93
- Rep Power
- 0
Like Josah said:
1. If a file does not return a value, it's return type is void.
e.g. the main method does not return a value - hence it's called public static void main (String args[])
2. Handle exceptions using try, catch
try
{
//code that the program should try to do <-- the code of WriteToFile method should go here.
}
catch(Exception e)
{
//what the program should do if it could not do the above code.[/COLOR]
e.printStackTrace()//A stack trace basically lists all the points where the program went wrong and what caused things to go bad.
}Last edited by eLancaster; 04-14-2011 at 12:11 PM.
- 04-14-2011, 12:15 PM #5
Member
- Join Date
- Mar 2011
- Posts
- 30
- Rep Power
- 0
Thanks eLancaster and JosAH,
It is Working now..
- 04-14-2011, 12:24 PM #6
Member
- Join Date
- Feb 2011
- Posts
- 11
- Rep Power
- 0
any method in Java SHOULD have a return type....
if it is returning a particular object...then its class name would be the return type of the method
even if your function is not returning anything...the function should still have a return type..as "void"
for example, you very own main() method..does it return anything? no..still we declare it as
Java Code:public static [I]void[/I] main...
Java Code:public void WriteToFile(....) { // do something }
- 04-14-2011, 12:29 PM #7
Member
- Join Date
- Mar 2011
- Posts
- 30
- Rep Power
- 0
Similar Threads
-
why constructor doesn't have any return type?
By kaka in forum New To JavaReplies: 1Last Post: 09-30-2010, 09:02 PM -
Static String Return Type
By Java_Developer in forum New To JavaReplies: 17Last Post: 10-03-2009, 07:07 PM -
Method return type problem
By McChill in forum New To JavaReplies: 7Last Post: 05-05-2009, 10:21 PM -
The return type
By Marcus in forum New To JavaReplies: 1Last Post: 07-05-2007, 07:28 AM
Bookmarks