Results 1 to 9 of 9
  1. #1
    gkoef is offline Member
    Join Date
    Sep 2010
    Posts
    24
    Rep Power
    0

    Default Missing return statement

    import java.io.*;
    import java.util.*;

    public class GameHelper
    {
    private static final String alphabet = "abcdefg";
    private int gridLength = 7;
    private int gridSize = 49;
    private int[] grid = new int[gridSize]; //array of 49 objects
    private int comCount = 0;

    public String getUserInput(String prompt)
    {
    String inputLine = null;
    System.out.print(prompt + " ");
    try
    {
    BufferedReader is = new BufferedReader(
    new InputStreamReader(System.in));
    inputLine = is.readLine();
    if(inputLine.length() == 0) return null;
    }
    catch(IOException e)
    {
    System.out.println("IOException: " + e);
    }
    return inputLine.toLowerCase();
    }

    public ArrayList<String> placeDotCom(int comSize)
    {
    ArrayList<String> alphaCells = new ArrayList<String>();
    String[] alphacoords = new String[comSize];
    String temp = null;
    int[] coords = new int[comSize];
    int attempts = 0;
    boolean success = false;
    int location = 0;

    comCount++;
    int incr = 1;
    if((comCount % 2) == 1)
    {
    incr = gridLength;
    }

    while(!success & attempts++ < 200) //the System.out lines below are for testing purposes only.
    { // they can be commented out when testing is not needed.
    location = (int) (Math.random() * gridSize); //get random starting point
    System.out.print(" try " + location); //TESTING
    int x = 0;
    success = true;

    while(success && x < comSize) //look for adjacent unused spots
    {
    if(grid[location] == 0) //if not already used
    {
    coords[x++] = location; //save location
    location += incr; //try 'next' adjacent
    if(location >= gridSize) //out of bounds - 'bottom'
    {
    success = false;
    }
    if(x > 0 && (location % gridLength == 0)) //out of bounds - right edge
    {
    success = false;
    }
    else //found already used location
    {
    System.out.print(" used " + location); //TESTING
    success = false;
    }
    }
    }

    x = 0; //turn location into alpha coords
    int row = 0;
    int column = 0;
    System.out.println("\n");
    while(x < comSize)
    {
    grid[coords[x]] = 1; //mark master grid points as 'used'
    row = (int) (coords[x] / gridLength); //get row value
    column = coords[x] % gridLength; //get numeric column value
    temp = String.valueOf(alphabet.charAt (column)); //convert to alpha

    alphaCells.add(temp.concat(Integer.toString (row)));
    x++;
    System.out.print(" coord " +x+ " = " + alphaCells.get(x-1)); //TESTING: statement that tells you exactly
    //where the DotCom is located
    }

    System.out.println("\n");

    return alphaCells;
    }
    }
    }

    I have forgotten how to place code in a block and I am sorry.

    The above code according to the compiler has a missing return statement.

    My Command Prompt came up with:

    GameHelper.java:101:missing return statement
    }
    ^

    Does anyone know what problem is?

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

    Default

    Quote Originally Posted by gkoef View Post
    I have forgotten how to place code in a block and I am sorry.

    The above code according to the compiler has a missing return statement.

    My Command Prompt came up with:

    GameHelper.java:101:missing return statement
    }
    ^

    Does anyone know what problem is?
    Type [code] before you start your code and type [/code] after the last line of code; there's also a button with an '#' icon: mark your code and press that button.

    About your problem: the compiler is whining at you because it found a possible execution path in a method that should return a value but it isn't at the end of that execution path; here's an example:

    Java Code:
    public String foo(int a) {
       if (a > 42)
          return "bar";
    }
    Imagine what would happen if a <= 42: nothing would be returned; your compiler doesn't like that and flags it as an error.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    gkoef is offline Member
    Join Date
    Sep 2010
    Posts
    24
    Rep Power
    0

    Default I haven't got a clue

    I understand what you are saying but the coding is from an exercise in a book I am reading and this particular code, alot of the things I have not learnt yet and in the book it states this also but it is needed so the rest of the classes can be compiled and run. So, there is obviously a problem in the code which I will not find (probably a mistake by the creators of this book) because I do not understand most of this code yet. So do you know which area of the code the problem is?

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

    Default

    Quote Originally Posted by gkoef View Post
    I understand what you are saying but the coding is from an exercise in a book I am reading and this particular code, alot of the things I have not learnt yet and in the book it states this also but it is needed so the rest of the classes can be compiled and run. So, there is obviously a problem in the code which I will not find (probably a mistake by the creators of this book) because I do not understand most of this code yet. So do you know which area of the code the problem is?
    Nope, I didn't read your code because it was hardly readable. Edit your original post and add those tags so your code appears more readable. Also, there's a 'Preview Post' button below the edit window; use it before posting and check how your post would end up.

    It is very well possible that the error of the code is in the book; most books nowadays are crap, they're only in it for the money ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    gkoef is offline Member
    Join Date
    Sep 2010
    Posts
    24
    Rep Power
    0

    Default

    Java Code:
    import java.io.*;
    import java.util.*;
    
    public class GameHelper
    {
    	private static final String alphabet = "abcdefg";
    	private int gridLength = 7;
    	private int gridSize = 49;
    	private int[] grid = new int[gridSize];						
    	private int comCount = 0;
    
    	public String getUserInput(String prompt)
    	{
    		String inputLine = null;
    		System.out.print(prompt + "  ");
    		try
    		{
    			BufferedReader is = new BufferedReader(
    			  new InputStreamReader(System.in));
    			inputLine = is.readLine();
    			if(inputLine.length() == 0)
    			{
    				return null;
    			}
    		}
    		catch(IOException e)
    		{
    			System.out.println("IOException: " + e);
    		}
    		return inputLine.toLowerCase();
    	}
    
    	public ArrayList<String> placeDotCom(int comSize)
    	{
    		ArrayList<String> alphaCells = new ArrayList<String>();
    		String[] alphacoords = new String[comSize];
    		String temp = null;
    		int[] coords = new int[comSize];
    		int attempts = 0;
    		boolean success = false;
    		int location = 0;
    
    		comCount++;
    		int incr = 1;
    		if((comCount % 2) == 1)
    		{
    			incr = gridLength;
    		}
    
    		while(!success & attempts++ < 200)					
    		{
    			location = (int) (Math.random() * gridSize);			
    			//System.out.print(" try " + location);			
    			int x = 0;
    			success = true;
    
    			while(success && x < comSize)
    			{
    				if(grid[location] == 0)
    				{
    					coords[x++] = location;	
    					location += incr;
    					if(location >= gridSize)
    					{
    						success = false;
    					}
    					if(x > 0 && (location % gridLength == 0))
    					{
    						success = false;
    					}
    					else
    					{
    						System.out.print(" used " + location);
    						success = false;
    					}
    				}
    			}
    
    			x = 0;
    			int row = 0;
    			int column = 0;
    			System.out.println("\n");
    			while(x < comSize)
    			{
    				grid[coords[x]] = 1;
    				row = (int) (coords[x] / gridLength);
    				column = coords[x] % gridLength;
    				temp = String.valueOf(alphabet.charAt (column));
    
    				alphaCells.add(temp.concat(Integer.toString (row)));
    				x++;
    			}
    
    			System.out.println("\n");
    			return alphaCells;
    		}
    	}
    }

    Here is the code in more readable form

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

    Default

    Quote Originally Posted by gkoef View Post
    Here is the code in more readable form
    Java Code:
    		while(!success & attempts++ < 200)		
    		{
    		// a lot of code here
    		}
    		// but no return statement here
    As far as the compiler can see nothing is returned after that while loop has finished. It doesn't interpret the code in the while body, even if it always returns there. Check your book and if there's not return statement at the end of the while loop, add a dummy one: "return null;".

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    gkoef is offline Member
    Join Date
    Sep 2010
    Posts
    24
    Rep Power
    0

    Default Still did not work, but...

    When I added a return statement where you said another error popped up along with the original being the return statement was unreachable. I have decided now that because the coding is new to me and most of the errors that come up are going to be new to me, I am going to forget about the coding and carry on with the book. Incedently, the book in question is Head First Java and this is the first major problem I have come up with so far.

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

    Default

    Quote Originally Posted by gkoef View Post
    When I added a return statement where you said another error popped up along with the original being the return statement was unreachable. I have decided now that because the coding is new to me and most of the errors that come up are going to be new to me, I am going to forget about the coding and carry on with the book. Incedently, the book in question is Head First Java and this is the first major problem I have come up with so far.
    I bet you put that "return null;" statement at the wrong location. You should put it after the while statement, not as the last statement of the while-body.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    4

    Default

    here's the part that's got error, inside the placeDotCom method
    Java Code:
    .......
                System.out.println("\n");
                return alphaCells;
            } // end  while(!success & attempts++ < 200)
        } // end public placedotcom
    you have a return statement inside the while(!success & attempts++ < 200) loop, but not outside.
    If the while loop condition is not executed, then you won't have a return statement. That's why the compiler complains. Just bring the return statement outside the while loop just before the end of the function and it should solve the problem
    Java Code:
    .......
                System.out.println("\n");
            } // end  while(!success & attempts++ < 200)
                return alphaCells;
        } // end public placedotcom

Similar Threads

  1. Missing return statement error.
    By Fortu in forum New To Java
    Replies: 2
    Last Post: 12-11-2010, 09:15 PM
  2. missing return statement
    By bayan in forum New To Java
    Replies: 6
    Last Post: 04-26-2010, 03:15 PM
  3. Missing Return Statement Error
    By darkblue24 in forum New To Java
    Replies: 13
    Last Post: 02-16-2010, 08:22 PM
  4. Replies: 18
    Last Post: 12-02-2009, 12:01 PM
  5. Missing Return Statement error
    By anilanar in forum New To Java
    Replies: 2
    Last Post: 08-20-2009, 01:02 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
  •