Results 1 to 4 of 4

Thread: Return Issues

  1. #1
    Join Date
    Aug 2006
    Posts
    30
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Post Return Issues

    I'm trying to pass a string into the method and have it return the results from mydata2. It keep getting the error "missing return statement".

    Code:
    // Main method
    	public static String readData (String arg1)
    	{
    		// Stream to read file
    		FileInputStream fin;		
    
    		try
    		{
    		    // Open an input stream
    		    String  mydata2="";
    		    fin = new FileInputStream (arg1);
    
    		    // Read a line of text
    		    mydata2 = new DataInputStream(fin).readLine();
    		    // Close our input stream
    		    fin.close();	
    		    return mydata2;
    		}
    		// Catches any error conditions
    		catch (IOException e)
    		{
    			//System.err.println ("Unable to read from file");
    			System.exit(-1);
    		}
    	}
    Last edited by magicgavin; 11-25-2008 at 04:37 PM.

  2. #2
    Join Date
    Jun 2008
    Posts
    589
    Thanks
    13
    Thanked 54 Times in 54 Posts
    Blog Entries
    1

    Default

    Code:
    // Main method
    	public void static String readData (String arg1)
    	{
    		// Stream to read file
    		FileInputStream fin;		
    
    		try
    		{
    		    // Open an input stream
    		    String  mydata2="";
    		    fin = new FileInputStream (arg1);
    
    		    // Read a line of text
    		    mydata2 = new DataInputStream(fin).readLine();
    		    // Close our input stream
    		    fin.close();	
    		    return mydata2;
    		}
    		// Catches any error conditions
    		catch (IOException e)
    		{
    			//System.err.println ("Unable to read from file");
    			System.exit(-1);
    		}
    	}
    Might this work?

    -magicyte

  3. #3
    Join Date
    Aug 2006
    Posts
    30
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Thanks for your help. I removed void because I though string replaced it.

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    It does. I doubt that will even compile — it's a nonsensical type signature.

    What you should have done is:
    Code:
    public static String readData(String arg1) {
        // Stream to read file
        FileInputStream fin;
        String r;
    
        try {
            // Open an input stream
            fin = new FileInputStream(arg1);
    
            // Read a line of text
            r = (new DataInputStream(fin)).readLine();
    
            // Close our input stream
            fin.close();
        } catch (IOException e) {
            // Catches any error conditions
            System.err.println("Unable to read from file");
            System.exit(-1);
        }
    
        return r;
    }
    Style notes: spaces do not go between method names and their arguments (you seemed a bit on the fence about this one); keep as much out of the try block as you can (catching exceptions is expensive); braces go on the same line as the keyword that opens the block in Java (the style you were using for the braces was a form of K&R C style).
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

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