Results 1 to 9 of 9
Thread: Missing return statement
- 12-31-2010, 03:42 PM #1
Member
- Join Date
- Sep 2010
- Posts
- 24
- Rep Power
- 0
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?
- 12-31-2010, 04:19 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,380
- Blog Entries
- 7
- Rep Power
- 17
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:
Imagine what would happen if a <= 42: nothing would be returned; your compiler doesn't like that and flags it as an error.Java Code:public String foo(int a) { if (a > 42) return "bar"; }
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 12-31-2010, 04:44 PM #3
Member
- Join Date
- Sep 2010
- Posts
- 24
- Rep Power
- 0
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?
- 12-31-2010, 04:51 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,380
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 12-31-2010, 05:09 PM #5
Member
- Join Date
- Sep 2010
- Posts
- 24
- Rep Power
- 0
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
- 12-31-2010, 05:14 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,380
- Blog Entries
- 7
- Rep Power
- 17
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;".Java Code:while(!success & attempts++ < 200) { // a lot of code here } // but no return statement here
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 12-31-2010, 05:40 PM #7
Member
- Join Date
- Sep 2010
- Posts
- 24
- Rep Power
- 0
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.
- 12-31-2010, 06:51 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,380
- Blog Entries
- 7
- Rep Power
- 17
- 01-01-2011, 02:52 AM #9
Senior Member
- Join Date
- Dec 2010
- Posts
- 165
- Rep Power
- 3
here's the part that's got error, inside the placeDotCom method
you have a return statement inside the while(!success & attempts++ < 200) loop, but not outside.Java Code:....... System.out.println("\n"); return alphaCells; } // end while(!success & attempts++ < 200) } // end public placedotcom
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
-
Missing return statement error.
By Fortu in forum New To JavaReplies: 2Last Post: 12-11-2010, 09:15 PM -
missing return statement
By bayan in forum New To JavaReplies: 6Last Post: 04-26-2010, 03:15 PM -
Missing Return Statement Error
By darkblue24 in forum New To JavaReplies: 13Last Post: 02-16-2010, 08:22 PM -
Missing return statement help and format method help
By Chewart in forum New To JavaReplies: 18Last Post: 12-02-2009, 12:01 PM -
Missing Return Statement error
By anilanar in forum New To JavaReplies: 2Last Post: 08-20-2009, 01:02 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks