Results 1 to 9 of 9
  1. #1
    Nakira is offline Member
    Join Date
    Oct 2008
    Location
    Aberystwyth
    Posts
    55
    Rep Power
    0

    Default [SOLVED] return statement

    Hi all again, i seem to have many issues today, maybe its just not my day. :confused:

    I've made some code but im not sure what return statement i should use.
    Here is my code followed by the compiler error:
    Java Code:
    import java.util.Arrays.*;
    import java.lang.*;
    
    public class SnakeGame
    {
    	char s = ' ';
    	//create main method(All Java prgrams need one to run ;))
    	public static void main(String[] args)
    	{
    		
    	}
    	//Create a 2D grid for the game to be played on
    	public static class Grid
    	{
    		Grid[][] grid = new Grid[31][31]; // 31x31 grid
    		static void fill(char grid[][], char _){}
    	}
    	public static String deepToString (String grid[][])
    	{
    		System.out.println(grid);
    		
    	}
    	
    }
    Java Code:
    SnakeGame.java:29: missing return statement
    	}
    	^
    1 error

  2. #2
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default Look at your deepToString method...

    Your deepToString method is not returning a string ... that's what the
    Java Code:
    public static [B][U]String[/U][/B] deepToString (String grid[][])
    ... means... that this method returns a string.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  3. #3
    Nakira is offline Member
    Join Date
    Oct 2008
    Location
    Aberystwyth
    Posts
    55
    Rep Power
    0

    Default

    What should i change or insert then?
    I tried many different return statements but none seem to work.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    What you want to return from there? I can see that you are trying to return a string. But in the code you don't have specify it.

    If you don't want to return anything set the return type into void.

    Java Code:
    	public static void deepToString (String grid[][])
    	{
    		System.out.println(grid);
    
    	}

  5. #5
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    See this:

    Vector Errors...

    Like I said, go through those tutorials and start paying attention in class. And, in class, when you don't understand something, ask your instructor about it, right away.

  6. #6
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    Just want to point out a couple of things you might want to think over:

    1. Why have you made "grid" a 2D array of Strings? You are only using each element as a char, no?
    2. If "grid" is a 2D array (or even normal array), do you know what happens to it if you pass it into a print statement?

    Try implementing one method at a time, making sure that it works how you want it to before moving on, and even testing the smaller portions of code that work together within the method.

    Unless you're used to writing masses of code with little to no error (and no design flaws), you should check for problems as often as possible. Better to catch them when you're working on them then to have to backtrack and pinpoint the cause of multiple errors.

  7. #7
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default What does the deeepToString method do?

    I'll try to answer your question about the return statement, but first I (we) need to know about the deepToString method:
    • What is this method supposed to do? I sounds like it is to convert something (deep?) to a string.

    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  8. #8
    Nakira is offline Member
    Join Date
    Oct 2008
    Location
    Aberystwyth
    Posts
    55
    Rep Power
    0

    Default

    Quote Originally Posted by CJSLMAN View Post
    I'll try to answer your question about the return statement, but first I (we) need to know about the deepToString method:
    • What is this method supposed to do? I sounds like it is to convert something (deep?) to a string.

    CJSL
    The deepToString method is used to display a 2D array as a string to the screen. I could use toString but deepToString is better as it was designed for 2D arrays i think. i just used info from the web and the api.

  9. #9
    Nakira is offline Member
    Join Date
    Oct 2008
    Location
    Aberystwyth
    Posts
    55
    Rep Power
    0

    Default

    Quote Originally Posted by emceenugget View Post
    Just want to point out a couple of things you might want to think over:

    1. Why have you made "grid" a 2D array of Strings? You are only using each element as a char, no?
    2. If "grid" is a 2D array (or even normal array), do you know what happens to it if you pass it into a print statement?

    Try implementing one method at a time, making sure that it works how you want it to before moving on, and even testing the smaller portions of code that work together within the method.

    Unless you're used to writing masses of code with little to no error (and no design flaws), you should check for problems as often as possible. Better to catch them when you're working on them then to have to backtrack and pinpoint the cause of multiple errors.
    The grid has been created for my game. i have to make a game similar to the classic snake game on mobile phones. The lecturer has recommended that we dont use swing or anything as at this stage its just hard work. We have been taught no string so the game is just a case of printing it to the terminal.
    Im not sure if there are any better ways to create a 2D grid for my game to be based on. once i've coded level 1 i'm hoping levels 2-5 will be easier.

Similar Threads

  1. there is no return statement
    By gabriel in forum New To Java
    Replies: 17
    Last Post: 12-03-2008, 04:55 PM
  2. Using int/int, 7/5 would return 1
    By zoe in forum New To Java
    Replies: 2
    Last Post: 12-02-2008, 11:25 AM
  3. return out of scope?
    By another_steve in forum New To Java
    Replies: 5
    Last Post: 01-28-2008, 09:34 PM
  4. if..else..return
    By mqdias in forum New To Java
    Replies: 1
    Last Post: 08-10-2007, 04:20 PM
  5. Statement or Prepared Statement ?
    By paty in forum JDBC
    Replies: 3
    Last Post: 08-01-2007, 04:45 PM

Posting Permissions

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