Results 1 to 7 of 7
  1. #1
    Anagha is offline Member
    Join Date
    Mar 2011
    Posts
    30
    Rep Power
    0

    Default 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...

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,763
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Anagha View Post
    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...
    If you don't know what to return, make your method return nothing, i.e. make your method a void method. (also make it either handle an IOException or mention it in a throws clause).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Anagha is offline Member
    Join Date
    Mar 2011
    Posts
    30
    Rep Power
    0

    Default

    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.....

  4. #4
    eLancaster is offline Member
    Join Date
    Jan 2011
    Posts
    93
    Rep Power
    0

    Default

    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.

  5. #5
    Anagha is offline Member
    Join Date
    Mar 2011
    Posts
    30
    Rep Power
    0

    Default

    Thanks eLancaster and JosAH,

    It is Working now..

  6. #6
    Navin Israni is offline Member
    Join Date
    Feb 2011
    Posts
    11
    Rep Power
    0

    Default

    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...
    so in short, whatever method u r declaring...if it does not return anything, it should specify return type as void...like this

    Java Code:
     
    public void WriteToFile(....) { // do something
    
    }

  7. #7
    Anagha is offline Member
    Join Date
    Mar 2011
    Posts
    30
    Rep Power
    0

Similar Threads

  1. why constructor doesn't have any return type?
    By kaka in forum New To Java
    Replies: 1
    Last Post: 09-30-2010, 09:02 PM
  2. Static String Return Type
    By Java_Developer in forum New To Java
    Replies: 17
    Last Post: 10-03-2009, 07:07 PM
  3. Method return type problem
    By McChill in forum New To Java
    Replies: 7
    Last Post: 05-05-2009, 10:21 PM
  4. The return type
    By Marcus in forum New To Java
    Replies: 1
    Last Post: 07-05-2007, 07:28 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
  •