Return outside method java ошибка

From your comment response above, I am going to make the educated guess that you believe that

boolean openingboard;
{
    return true;
}

defines a Java method called openingboard. This isn’t the case. Java follows the C paradigm of requiring you to specify your parameters in parentheses, regardless of whether you have any parameters or not. So, the method

boolean openingboard() {
    return true;
}

is a valid Java method (assuming it is inside some class), as is a version of openingboard with much more code between the curly braces.

That said, I’m going to pass along a few friendly pointers on Java style:

  • Java (and indeed most higher-level language) programmers tend to frown on «forever» loops such as while (true), since those loops make it much harder to determine when the loop actually stops.
  • There is no need for the label search in the code, and labels are even more discouraged than forever loops are.

So, I would recommend rewriting your code to look something like

private boolean openingboard() {
    Robot robot = new Robot();
    Color color3 = new Color(108, 25, 85);
    Rectangle rect = new Rectangle(0, 0, 1365, 770);
    BufferedImage image = robot.createScreenCapture(rect);
    for(int x = 0; x < rectangle.getWidth(); x++) {
        for(int y = 0; y < rectangle.getHeight(); y++) {
            if(image.getRGB(x, y) == color3.getRGB())
                return true;
        }
    }

    return false;
}

assuming of course that you prefer a debugger to trace prints.


posted 5 years ago

  • Mark post as helpful


  • send pies

    Number of slices to send:

    Optional ‘thank-you’ note:



  • Quote
  • Report post to moderator

Hello,

I am developing a dice game, I make a random draw with 5 dice since a call of my method of the main program which displays the result in a table of String.

an error comes to tell me «return outside method».

The program is so simple that I do not see my mistake is contradictory!

Here is a piece of my code:

error line 10

Regards

Philippe


posted 5 years ago

  • Mark post as helpful


  • send pies

    Number of slices to send:

    Optional ‘thank-you’ note:



  • Quote
  • Report post to moderator


This is incorrect syntax for declaring a method. Additionally, a method name should begin with a lower case letter.

Philippe Ponceblanc

Ranch Hand

Posts: 210


posted 5 years ago

  • Mark post as helpful


  • send pies

    Number of slices to send:

    Optional ‘thank-you’ note:



  • Quote
  • Report post to moderator

Carey Brown wrote:

This is incorrect syntax for declaring a method. Additionally, a method name should begin with a lower case letter.

I wrote my method begin with a lower case letter >> I always have the same problem !

Carey Brown

Saloon Keeper

Posts: 10457

Eclipse IDE
Firefox Browser
MySQL Database
VI Editor
Java
Windows


posted 5 years ago

  • Mark post as helpful


  • send pies

    Number of slices to send:

    Optional ‘thank-you’ note:



  • Quote
  • Report post to moderator

Philippe Ponceblanc wrote:

Carey Brown wrote:

This is incorrect syntax for declaring a method. Additionally, a method name should begin with a lower case letter.

I wrote my method begin with a lower case letter >> I always have the same problem !

Did you fix the syntax error?

Carey Brown

Saloon Keeper

Posts: 10457

Eclipse IDE
Firefox Browser
MySQL Database
VI Editor
Java
Windows


posted 5 years ago

  • Mark post as helpful


  • send pies

    Number of slices to send:

    Optional ‘thank-you’ note:



  • Quote
  • Report post to moderator

Google: java method tutorial

Here is one of the LINKS.


posted 5 years ago


  • Likes 2
  • Mark post as helpful


  • send pies

    Number of slices to send:

    Optional ‘thank-you’ note:



  • Quote
  • Report post to moderator

There is no syntax error

(see follow-up below): you’re just not declaring a method. Rather, you have declared a static class variable. If you format your code correctly, you’ll see what’s wrong.

You probably did not intentionally do this but lines 5-12 above is an instance initialization block.

Carey Brown

Saloon Keeper

Posts: 10457

Eclipse IDE
Firefox Browser
MySQL Database
VI Editor
Java
Windows


posted 5 years ago

  • Mark post as helpful


  • send pies

    Number of slices to send:

    Optional ‘thank-you’ note:



  • Quote
  • Report post to moderator

definition of «syntax error»

«a character or string incorrectly placed in a command or instruction that causes a failure in execution [or compilation].»


posted 5 years ago


  • Likes 1
  • Mark post as helpful


  • send pies

    Number of slices to send:

    Optional ‘thank-you’ note:



  • Quote
  • Report post to moderator

Sorry, there is in fact a syntax error at the line OP indicated and it’s because a return statement is not allowed in an initializer block.

Bartender

Posts: 732


posted 5 years ago

  • Mark post as helpful


  • send pies

    Number of slices to send:

    Optional ‘thank-you’ note:



  • Quote
  • Report post to moderator

And what is this line supposed to mean:

You do not show a class named ToString. Did you mean PokerDAs instead?

Did you mean for Tirage to be the name of a method that returns a String array (in which case you have an extraneous semicolon and no argument list for the method.

And if that is the case, why  do you return an array of ints instead of an array  of Strings?

Philippe Ponceblanc

Ranch Hand

Posts: 210


posted 5 years ago

  • Mark post as helpful


  • send pies

    Number of slices to send:

    Optional ‘thank-you’ note:



  • Quote
  • Report post to moderator

can not return a arrays in my method, the compiler tells me

>> return outside method!


posted 5 years ago

  • Mark post as helpful


  • send pies

    Number of slices to send:

    Optional ‘thank-you’ note:



  • Quote
  • Report post to moderator

>> return outside method!  

Yes.  There is no method there.  See Junilu Lacar’s post.

Here it is again:

Get rid of the ; on line 1 above and make it a proper method declaration statement.


posted 4 years ago

  • Mark post as helpful


  • send pies

    Number of slices to send:

    Optional ‘thank-you’ note:



  • Quote
  • Report post to moderator

I have spotted the following when I run your code,

the first is that you mix up the int[] and string[] arrays in your method. Once you instantiate it as a string or int, you either have to convert those and save it in a different variable or keep it the same type.

The 2nd is that you created a method signature (non declared method) to be overwritten by classes that could implement/extend the class, so the method can not have a return.

Example of the difference:

public static void overrideMe;

public static String giveMeString(){ //code };

When you managed to fix these, you may want to look at how to change line 13 to call a static method.

jQuery in Action, 3rd edition

boolean openingboard;
{   
Robot robot = new Robot();
Color color3 = new Color(108, 25, 85);
Rectangle rectangle = new Rectangle(0, 0, 1365, 770);
    while(true)
    {
    BufferedImage image = robot.createScreenCapture(rectangle);
    search: for(int x = 0; x < rectangle.getWidth(); x++)
        {
        for(int y = 0; y < rectangle.getHeight(); y++)
            {
                if(image.getRGB(x, y) == color3.getRGB())
                {
                    System.out.println("About to finish and return true");
                    return true;
                }
                System.out.println("About to finish and return false");
            }
        }
    }
}

the error is:
java:71: return outside method

return true

^

i don’t know what this is happening please help!

From your comment response above, I am going to make the educated guess that you believe that

boolean openingboard;
{
    return true;
}

defines a Java method called openingboard. This isn’t the case. Java follows the C paradigm of requiring you to specify your parameters in parentheses, regardless of whether you have any parameters or not. So, the method

boolean openingboard() {
    return true;
}

is a valid Java method (assuming it is inside some class), as is a version of openingboard with much more code between the curly braces.

That said, I’m going to pass along a few friendly pointers on Java style:

  • Java (and indeed most higher-level language) programmers tend to frown on «forever» loops such as while (true), since those loops make it much harder to determine when the loop actually stops.
  • There is no need for the label search in the code, and labels are even more discouraged than forever loops are.

So, I would recommend rewriting your code to look something like

private boolean openingboard() {
    Robot robot = new Robot();
    Color color3 = new Color(108, 25, 85);
    Rectangle rect = new Rectangle(0, 0, 1365, 770);
    BufferedImage image = robot.createScreenCapture(rect);
    for(int x = 0; x < rectangle.getWidth(); x++) {
        for(int y = 0; y < rectangle.getHeight(); y++) {
            if(image.getRGB(x, y) == color3.getRGB())
                return true;
        }
    }

    return false;
}

assuming of course that you prefer a debugger to trace prints.

Proper methods look like: boolean openingboard ( )

not like boolean openingboard;

The parenthesis are not optional.

The way you have it: openingboard is a field. There is a init block with a Robot and a color and some for loops nested inside of each other. Inside one of the for loops is a return which is not allowed in an init block.

I had quite a few errors with this code and I’ve weed most of them out but i can’t seem to get this last compiler error to go away I’ve placed it in many places around the code but still seems to error out and when i watch the video over again the answer still seems to escape my grasp. I’m gonna go watch it again maybe ill get it eventually


GoKart.java

public class GoKart {
  public String mColor = "red";
}

This is My Code IDK if it showed up here in the post

public class GoKart {
  public String mColor = "red";
  public String getMColor; {
    return mColor; 
  }
}

1 Answer

Ben Deitch

STAFF

Hey Fredrick! It looks like you’ve got a semi-colon after your function name ‘getMColor;’. After the function name would be where we specify any parameters, but if they’re aren’t any parameters (like in this case), then we just want to put parentheses. So instead you should be using ‘getMColor()’. Hope that helps!

Перейти к контенту


posted 4 years ago

  • Mark post as helpful


  • send pies

    Number of slices to send:

    Optional ‘thank-you’ note:

  • Quote
  • Report post to moderator

Hello,

I am developing a dice game, I make a random draw with 5 dice since a call of my method of the main program which displays the result in a table of String.

an error comes to tell me «return outside method».

The program is so simple that I do not see my mistake is contradictory!

Here is a piece of my code:

error line 10


posted 4 years ago

  • Mark post as helpful


  • send pies

    Number of slices to send:

    Optional ‘thank-you’ note:

  • Quote
  • Report post to moderator


This is incorrect syntax for declaring a method. Additionally, a method name should begin with a lower case letter.

Philippe Ponceblanc

Ranch Hand

Posts: 210


posted 4 years ago

  • Mark post as helpful


  • send pies

    Number of slices to send:

    Optional ‘thank-you’ note:

  • Quote
  • Report post to moderator

Carey Brown wrote:

This is incorrect syntax for declaring a method. Additionally, a method name should begin with a lower case letter.

I wrote my method begin with a lower case letter >> I always have the same problem !

Carey Brown

Saloon Keeper

Posts: 9867

Eclipse IDE
Firefox Browser
MySQL Database
VI Editor
Java
Windows


posted 4 years ago

  • Mark post as helpful


  • send pies

    Number of slices to send:

    Optional ‘thank-you’ note:

  • Quote
  • Report post to moderator

Philippe Ponceblanc wrote:

Carey Brown wrote:

This is incorrect syntax for declaring a method. Additionally, a method name should begin with a lower case letter.

I wrote my method begin with a lower case letter >> I always have the same problem !

Did you fix the syntax error?

Carey Brown

Saloon Keeper

Posts: 9867

Eclipse IDE
Firefox Browser
MySQL Database
VI Editor
Java
Windows


posted 4 years ago

  • Mark post as helpful


  • send pies

    Number of slices to send:

    Optional ‘thank-you’ note:

  • Quote
  • Report post to moderator

Google: java method tutorial

Here is one of the LINKS.


posted 4 years ago


  • Likes 2
  • Mark post as helpful


  • send pies

    Number of slices to send:

    Optional ‘thank-you’ note:

  • Quote
  • Report post to moderator

There is no syntax error

(see follow-up below): you’re just not declaring a method. Rather, you have declared a static class variable. If you format your code correctly, you’ll see what’s wrong.

You probably did not intentionally do this but lines 5-12 above is an instance initialization block.

Carey Brown

Saloon Keeper

Posts: 9867

Eclipse IDE
Firefox Browser
MySQL Database
VI Editor
Java
Windows


posted 4 years ago

  • Mark post as helpful


  • send pies

    Number of slices to send:

    Optional ‘thank-you’ note:

  • Quote
  • Report post to moderator

definition of «syntax error»

«a character or string incorrectly placed in a command or instruction that causes a failure in execution [or compilation].»


posted 4 years ago


  • Likes 1
  • Mark post as helpful


  • send pies

    Number of slices to send:

    Optional ‘thank-you’ note:

  • Quote
  • Report post to moderator

Sorry, there is in fact a syntax error at the line OP indicated and it’s because a return statement is not allowed in an initializer block.

Bartender

Posts: 732


posted 4 years ago

  • Mark post as helpful


  • send pies

    Number of slices to send:

    Optional ‘thank-you’ note:

  • Quote
  • Report post to moderator

And what is this line supposed to mean:

You do not show a class named ToString. Did you mean PokerDAs instead?

Did you mean for Tirage to be the name of a method that returns a String array (in which case you have an extraneous semicolon and no argument list for the method.

And if that is the case, why  do you return an array of ints instead of an array  of Strings?

Philippe Ponceblanc

Ranch Hand

Posts: 210


posted 4 years ago

  • Mark post as helpful


  • send pies

    Number of slices to send:

    Optional ‘thank-you’ note:

  • Quote
  • Report post to moderator

can not return a arrays in my method, the compiler tells me

>> return outside method!


posted 4 years ago

  • Mark post as helpful


  • send pies

    Number of slices to send:

    Optional ‘thank-you’ note:

  • Quote
  • Report post to moderator

>> return outside method!  

Yes.  There is no method there.  See Junilu Lacar’s post.

Here it is again:

Get rid of the ; on line 1 above and make it a proper method declaration statement.


posted 4 years ago

  • Mark post as helpful


  • send pies

    Number of slices to send:

    Optional ‘thank-you’ note:

  • Quote
  • Report post to moderator

I have spotted the following when I run your code,

the first is that you mix up the int[] and string[] arrays in your method. Once you instantiate it as a string or int, you either have to convert those and save it in a different variable or keep it the same type.

The 2nd is that you created a method signature (non declared method) to be overwritten by classes that could implement/extend the class, so the method can not have a return.

Example of the difference:

public static void overrideMe;

public static String giveMeString(){ //code };

When you managed to fix these, you may want to look at how to change line 13 to call a static method.

  1. July 27th, 2014, 04:19 PM

    #1

    lucas29 is offline


    Member


    here is the code:

    import java.util.Scanner;
    public class beersong
    {
    public static void main (String[] args)
    {
    int bottle;
    int bottlenumber;
    int bottlezero;
    int i;
    int bottletens;
    int bottleones;
    String bottleword;

    Scanner scannerObject = new Scanner(System.in);
    System.out.println(«how many bottles of beer?»);

    bottle = scannerObject.nextInt();

    if (bottle <= 0);
    {
    bottle = 0;
    }

    if (bottle > 99);
    {
    bottle = 99;
    }

    bottletens = bottle/10;
    bottleones = bottle%10;

    for (i = bottle; i > 0; i—)
    {

    System.out.println(i + » bottels of beer on the wall, » + i + » bottles of beer!»);
    System.out.println(«you take one down you pass it around » + (i — 1) + » bottles of beer on the wall!»);
    }

    System.out.println(«the song is done»);
    }
    String bottlewordString(int bottleNumber);
    {
    if (bottle < 20)
    {
    switch (bottleNumber)
    {
    case 1:
    return «one»;
    case 2:
    return «two»;
    case 3:
    return «three»;
    case 4:
    return «four»;
    case 5:
    return «five»;
    case 6:
    return «six»;
    case 7:
    return «seven»;
    case 8:
    return «eight»;
    case 9:
    return «nine»;
    case 10:
    return «ten»;
    case 11:
    return «eleven»;
    case 12:
    return «twelve»;
    case 13:
    return «thirteen»;
    case 14:
    return «fourteen»;
    case 15:
    return «fifteen»;
    case 16:
    return «sixteen»;
    case 17:
    return «seventeen»;
    case 18:
    return «eighteen»;
    case 19:
    return «nineteen»;
    default:
    System .out .println(«please enter a different number, and try again»);
    System.exit(0);
    return «error»; //to keep the compiler happy
    }
    }
    else
    {
    switch (bottletens)
    {
    case 2:
    return «twenty»;
    case 3:
    return «thirty»;
    case 4:
    return «fourty»;
    case 5:
    return «fifty»;
    case 6:
    return «sixty»;
    case 7:
    return «seventy»;
    case 8:
    return «eighty»;
    case 9:
    return «ninety»;
    default:
    System .out .println(«please enter a different number, and try again»);
    System.exit(0);
    return «error»; //to keep the compiler happy
    }
    switch (bottleones)
    {
    case 1:
    return «one»;
    case 2:
    return «two»;
    case 3:
    return «three»;
    case 4:
    return «four»;
    case 5:
    return «five»;
    case 6:
    return «six»;
    case 7:
    return «seven»;
    case 8:
    return «eight»;
    case 9:
    return «nine»;
    default:
    System .out .println(«please enter a different number, and try again»);
    System.exit(0);
    return «error»; //to keep the compiler happy
    }
    }
    }
    }

    and here is the problem:

    return «nine»;
    ^
    error: return outside of methood

    what do i do?


  2. Default Related threads:


  3. July 27th, 2014, 04:21 PM

    #2

    Cornix is offline


    Senior Member


    Default Re: return outside method

    Put it inside a method perhaps?
    Akk return statements must be placed within the scope of a method.


  4. July 27th, 2014, 04:33 PM

    #3

    lucas29 is offline


    Member


    Default Re: return outside method


  5. July 27th, 2014, 04:44 PM

    #4

    GregBrannon is offline


    Super Moderator


    Default Re: return outside method

    Please post your code correctly using code or highlight tags which are explained near the top of this link or the thread will be closed.


  6. July 27th, 2014, 04:58 PM

    #5

    lucas29 is offline


    Member


    Default Re: return outside method

    import java.util.Scanner;
    public class beersong
       {
    	public static void main (String[] args)
    	{
    	int bottle;
    	int bottlenumber;
    	int bottlezero;
    	int i;
    	int bottletens;
    	int bottleones;
    	String bottleword;
     
    	Scanner scannerObject = new Scanner(System.in);
    	System.out.println("how many bottles of beer?");
     
    	bottle = scannerObject.nextInt();
     
    	if (bottle <= 0);
    	{
    	bottle = 0;
    	}
     
    	if (bottle > 99);
    	{
    	bottle = 99;
    	}
     
    	bottletens = bottle/10;
    	bottleones = bottle%10;
     
    	for (i = bottle; i > 0; i--)
    	{
     
    System.out.println(i + " bottels of beer on the wall, " + i + " bottles of beer!");
    	System.out.println("you take one down you pass it around " + (i - 1) + " bottles of beer on the wall!");
    	}
     
     
    	System.out.println("the song is done");
    	}
    	String bottlewordString(int bottleNumber);
    		{
    		if (bottle < 20)
    		{
    		switch (bottleNumber)
    		{
    		case 1:
    			return "one";
    		case 2:
    			return "two";
    		case 3:
    			return "three";
    		case 4:
    			return "four";
    		case 5:
    			return "five";
    		case 6:
    			return "six";
    		case 7:
    			return "seven";
    		case 8:
    			return "eight";
    		case 9:
    			return "nine";
    		case 10:
    			return "ten";
    		case 11:
    			return "eleven";
    		case 12:
    			return "twelve";
    		case 13:
    			return "thirteen";
    		case 14:
    			return "fourteen";
    		case 15:
    			return "fifteen";
    		case 16:
    			return "sixteen";
    		case 17:
    			return "seventeen";
    		case 18:
    			return "eighteen";
    		case 19:
    			return "nineteen";
    		default:
    			System .out .println("please enter a different number, and try again");
    			System.exit(0);
    			return "error"; //to keep the compiler happy
    		}
    		}
    		else
    		{
    		switch (bottletens)
    		{
    		case 2:
    			return "twenty";
    		case 3:
    			return "thirty";
    		case 4:
    			return "fourty";
    		case 5:
    			return "fifty";
    		case 6:
    			return "sixty";
    		case 7:
    			return "seventy";
    		case 8:
    			return "eighty";
    		case 9:
    			return "ninety";		
    default:
    			System .out .println("please enter a different number, and try again");
    			System.exit(0);
    			return "error"; //to keep the compiler happy
    			}
    		switch (bottleones)
    		{
    		case 1:
    			return "one";
    		case 2:
    			return "two";
    		case 3:
    			return "three";
    		case 4:
    			return "four";
    		case 5:
    			return "five";
    		case 6:
    			return "six";
    		case 7:
    			return "seven";
    		case 8:
    			return "eight";
    		case 9:
    			return "nine";
    		default:
    			System .out .println("please enter a different number, and try again");
    			System.exit(0);
    			return "error"; //to keep the compiler happy
    			}
    		}
    		}
    	}

    there is the code


  7. July 27th, 2014, 05:08 PM

    #6

    Cornix is offline


    Senior Member


    Default Re: return outside method

    You should indent your code properly and maybe then you will find your error.
    You should really stick to the java style conventions:
    https://google-styleguide.googlecode…javaguide.html


  8. July 27th, 2014, 05:11 PM

    #7

    lucas29 is offline


    Member


    Default Re: return outside method

    i already know what the error is, here is the error:

    return «nine»;
    ^
    error: return outside of methood


  9. July 27th, 2014, 05:27 PM

    #8

    GregBrannon is offline


    Super Moderator


    Default Re: return outside method

    i already know what the error is, here is the error:

    This kind of response is not helpful. If you insist on telling those trying to help you that you already know what they’re telling you, they will stop trying to help you.

    In Java, class names begin with capital letters. «beersong» should be «Beersong» or preferably «BeerSong».

    The code you posted has many syntax errors, (What editor are you using?) The main cause of all of the errors is that there is a semicolon ending this line (line 42 in my editor):

    String bottlewordString(int bottleNumber);

    Remove that semicolon, and the number of errors will be significantly reduced. As it turns out, the second method is never called, so it will compile and run even with the remaining errors.

    Hope this helps.


  10. The Following User Says Thank You to GregBrannon For This Useful Post:

    lucas29 (July 27th, 2014)


  11. July 27th, 2014, 05:37 PM

    #9

    lucas29 is offline


    Member


    Default Re: return outside method

    sorry, but i now have 3 different errors, here they are:

    switch (bottleones)
    ^
    cannot find symbol

    switch (bottletens)
    ^
    cannot find symbol

    if (bottle < 20)
    ^
    cannot find symbol

    those are the errors i am getting now,
    btw, i am not using a editor, i don’t know where i could

    — Update —

    please help me


  12. July 27th, 2014, 05:48 PM

    #10

    Cornix is offline


    Senior Member


    Default Re: return outside method

    And once again, if you just indented your code correctly as per the java style convention you would be able to see your errors quite easily.
    Now they are simply hidden behind a mess of ugly code.


  13. July 27th, 2014, 05:49 PM

    #11

    GregBrannon is offline


    Super Moderator


    Default Re: return outside method

    Of course you’re using an editor. The editor is what you’re writing (editing) your source code in. It’s also probably showing you the errors you’ve posted above, since you didn’t get those by compiling (I think).

    «Cannot find symbol» means the variable or method named has not been defined in the scope it has been found. For example, in what scope is ‘bottle’ defined? In what scope does the error you’ve posted above occur?

    Note: «scope» is usually defined by braces that define blocks of code. The beginning of the scope starts with the left brace, ‘{‘, and the end of the scope is defined by the right brace, ‘}’. This is a basic program construction concept that you should have gotten from the material presented earlier in the book. I suspect you’re not reading the book and learning the material, but you’re trying to do the assignments you’ve been given without reading the material. You need to change your approach.


  14. July 27th, 2014, 06:26 PM

    #12

    lucas29 is offline


    Member


    Default Re: return outside method

    please tell me how my code should look then. i am not annoyed just confused

    — Update —

    i am not understanding why it is doing what it is doing, but it is not making any sense cause i have one int named bottle and bottleones and bottletens, so i need help to understand why it’s doing this

    — Update —

    btw i did get those errors by compiling, i m in gedit on a windows cp

    — Update —

    i also have them in the same scope


  15. July 27th, 2014, 06:28 PM

    #13

    GregBrannon is offline


    Super Moderator


    Default Re: return outside method

    Answering my own question:

    The variables you named are declared inside the main() method’s scope, OUTSIDE the scope and visibility of the method bottlewordString(). Those variables are unknown outside the main() method.

    Again, these are concepts you should have learned from the book by now. Start over, read/study the book, and learn the material that you’ve skipped to this point.


  16. July 27th, 2014, 08:36 PM

    #14

    lucas29 is offline


    Member


    Default Re: return outside method

    wait… i thought i didn’t close the main till the end!?

    — Update —

    oh… i gues i didn’t

    — Update —

    my mistake

    — Update —

    now i have them in the same scope, and i’m getting a new error, here it is:

    String bottlewordString(int bottleNumber)
    ^
    ‘;’

    String bottlewordString(int bottleNumber)
    ^
    ‘;’

    please help me fix my problems

    — Update —

    please, i really need the help

    — Update —

    DOES ANYONE KNOW HOW TO HELP ME!?

    — Update —

    PLEASE HELP ME!

    — Update —

    anyone? hello?

    — Update —

    i guess either none of you have any idea of how to help me, or the person who does isn’t online

    — Update —

    please

    — Update —

    please someone help me

    — Update —

    please!

    — Update —

    sorry, i’m panicking

    — Update —

    but, seriously i NEED help

    — Update —

    can anyone help me?

    — Update —

    anyone?


  17. July 27th, 2014, 11:53 PM

    #15

    lucas29 is offline


    Member


    Default Re: return outside method

    is there still no one that is helping me?


  18. July 28th, 2014, 12:08 AM

    #16

    Cornix is offline


    Senior Member


    Default Re: return outside method

    I think nobody will ever be able to help you.
    I would suggest you start reading a few books before you try to program something. This thread will probably be closed now.


  19. July 28th, 2014, 01:04 AM

    #17

    Default Re: return outside method

    Myself and others have told you many times that the main
    method scope is different from the scope of any other methods.
    You need to re-organsize the code so the braces begin and end
    where they are logically supposed to.

    You are getting confused as to method scope issues — Greg and
    Cornix have given helpful hints and tips to aid you in this.

    In regards to the new errors — can you post your updated code?
    Not the code with the initial errors — the code with the new errors
    after you made the changes Greg/Cornix suggested.

    Wishes Ada xx

    If to Err is human — then programmers are most human of us all.
    «The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    .«
    Augusta Ada Byron, Lady Lovelace (1851)


  20. July 28th, 2014, 04:41 AM

    #18

    lucas29 is offline


    Member


    Default Re: return outside method

    import java.util.Scanner;
    public class beersong
       {
    	public static void main (String[] args)
    	{
    	int bottle;
    	int bottlenumber;
    	int bottlezero;
    	int i;
    	int bottletens;
    	int bottleones;
    	String bottleword;
     
    	Scanner scannerObject = new Scanner(System.in);
    	System.out.println("how many bottles of beer?");
     
    	bottle = scannerObject.nextInt();
     
    	if ( bottle <= 0 );
    	{
    	bottle = 0;
    	}
     
    	if (bottle > 99);
    	{
    	bottle = 99;
    	}
     
    	bottletens = bottle/10;
    	bottleones = bottle%10;
     
    	for (i = bottle; i > 0; i--)
    	{
     
    System.out.println(i + " bottels of beer on the wall, " + i + " bottles of beer!");
    	System.out.println("you take one down you pass it around " + (i - 1) + " bottles of beer on the wall!");
    	}
     
     
    	System.out.println("the song is done");
     
    	String bottlewordString(int bottleNumber)
    		{
    		if (bottle < 20)
    		{
    		switch (bottleNumber)
    		{
    		case 1:
    			return "one";
    		case 2:
    			return "two";
    		case 3:
    			return "three";
    		case 4:
    			return "four";
    		case 5:
    			return "five";
    		case 6:
    			return "six";
    		case 7:
    			return "seven";
    		case 8:
    			return "eight";
    		case 9:
    			return "nine";
    		case 10:
    			return "ten";
    		case 11:
    			return "eleven";
    		case 12:
    			return "twelve";
    		case 13:
    			return "thirteen";
    		case 14:
    			return "fourteen";
    		case 15:
    			return "fifteen";
    		case 16:
    			return "sixteen";
    		case 17:
    			return "seventeen";
    		case 18:
    			return "eighteen";
    		case 19:
    			return "nineteen";
    		default:
    			System .out .println("please enter a different number, and try again");
    			System.exit(0);
    			return "error"; //to keep the compiler happy
    		}
    		}
    		else
    		{
    		switch (bottletens)
    		{
    		case 2:
    			return "twenty";
    		case 3:
    			return "thirty";
    		case 4:
    			return "fourty";
    		case 5:
    			return "fifty";
    		case 6:
    			return "sixty";
    		case 7:
    			return "seventy";
    		case 8:
    			return "eighty";
    		case 9:
    			return "ninety";		
    default:
    			System .out .println("please enter a different number, and try again");
    			System.exit(0);
    			return "error"; //to keep the compiler happy
    			}
    		switch (bottleones)
    		{
    		case 1:
    			return "one";
    		case 2:
    			return "two";
    		case 3:
    			return "three";
    		case 4:
    			return "four";
    		case 5:
    			return "five";
    		case 6:
    			return "six";
    		case 7:
    			return "seven";
    		case 8:
    			return "eight";
    		case 9:
    			return "nine";
    		default:
    			System .out .println("please enter a different number, and try again");
    			System.exit(0);
    			return "error"; //to keep the compiler happy
    			}
    		}
    		}
    		}
    	}

  21. July 28th, 2014, 05:00 AM

    #19

    GregBrannon is offline


    Super Moderator


    Default Re: return outside method

    Why are you panicking? How much studying have you done in response to my suggestion to read the book?

    What changes have you made in the newly-posted code in response to the suggestions made by Cornix and myself?

    Let me guess: You removed the semicolon and the original close brace of the main() method. That right?

    Why have you progressed no further than that? What help do you need now? If you’re getting errors that you want help with, always post your updated code, the complete and exact text of the error messages you need help with, and ask new questions. (How many times have you been told that?)

    Most everyone has nearly given up on you. Give us some glimmer of hope that future efforts will not be wasted.


  22. July 28th, 2014, 05:48 AM

    #20

    lucas29 is offline


    Member


    Default Re: return outside method

    i needed to finish, that’s why i was panicking.

    — Update —

    btw sorry but i am really thinking that i wont be able to finish. i also say that i did post exact code and error.


  23. July 28th, 2014, 02:00 PM

    #21

    Default Re: return outside method

    I compiled your last posted code and this is what the compiler turned up.

    Error: The public type ‘beersong’ must be defined in it’s own file

    Error: You cannot return from a void function

    Error:

    String bottlewordString(int bottleNumber)

    Multiple markers present at the above line of code
    Syntax error on token ‘(‘. ; expected
    Syntax error on token ‘)’. ; expected

    There were lots of void returning errors, again a problem that should be very
    simple to fix. The first is a bit more cryptic but nothing you should struggle with.

    Have a look at these errors — then compile the code again. Compare your results
    against these and see if any of them match. I can almost assure you they will in
    some form. Fix these — and keep taking advice from Greg, Cornix, myself and others
    and you will be closer to your goal.

    Wishes Ada xx

    If to Err is human — then programmers are most human of us all.
    «The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    .«
    Augusta Ada Byron, Lady Lovelace (1851)


  24. The Following 2 Users Say Thank You to Ada Lovelace For This Useful Post:

    GregBrannon (July 28th, 2014), lucas29 (July 29th, 2014)


  25. July 29th, 2014, 02:44 AM

    #22

    lucas29 is offline


    Member


    Default Re: return outside method

    wow, that is useful, but what are u using?

    — Update —

    i using java 7, i think


  26. July 29th, 2014, 02:51 AM

    #23

    Default Re: return outside method

    Eclipse is my development environment and I use JRE 8.

    The differences between 7 and 8 are not related to this problem.

    Wishes Ada xx

    If to Err is human — then programmers are most human of us all.
    «The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    .«
    Augusta Ada Byron, Lady Lovelace (1851)


  27. July 29th, 2014, 03:46 AM

    #24

    lucas29 is offline


    Member


    Default Re: return outside method

    the syntax errors are the same


  28. July 29th, 2014, 03:53 AM

    #25

    Default Re: return outside method

    Ok — what steps are you going to take to resolve them?

    Wishes Ada xx

    If to Err is human — then programmers are most human of us all.
    «The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    .«
    Augusta Ada Byron, Lady Lovelace (1851)



Форум программистов Vingrad

Модераторы: LSD, AntonSaburov

Поиск:

Ответ в темуСоздание новой темы
Создание опроса
> Подскажите пожалусто где ошибка? 

V

Опции темы

IndigoStill

Новичок

Профиль
Группа: Участник
Сообщений: 11
Регистрация: 21.10.2007

Репутация: нет
Всего: нет

Рассматривал пример из обучения. Скопировал полностью, не работает, в чем причина?

class R {
         private String str; 
         private int size;
         public R (String str1,int size2) 
         {    
        str = str1;
        size = size2; 

                  }
                 public String getstr(); 
                {return str;}

                                public int getsize(); 
                {return size;}
}

    public class П1 
        {
    public static void main(String[] args) 
         {
          Repit[] h = new Repit[2];
          h[0] = new R(«eeeee»,1);
          h[1] = new R(«eeeee1»,2);
          for (int u =0; u<h.length; u++)
          {System.out.println( «»+ h[u].getstr());}
}
}

       Высвечивает ошибку вот здесь:

                 public String getstr(); 
                {return str;}

                                public int getsize(); 
                {return size;}

такого вида:

Created dir: D:1JavaLibrary2buildclasses
Compiling 1 source file to D:1JavaLibrary2buildclasses
D:1JavaLibrary2srcY.java:10: missing method body, or declare abstract
                public String getstr(); 
D:1JavaLibrary2srcY.java:11: return outside method
                {return str;}
D:1JavaLibrary2srcY.java:13: missing method body, or declare abstract
                public int getsize(); 
D:1JavaLibrary2srcY.java:14: return outside method
                {return size;}
4 errors
BUILD FAILED (total time: 0 seconds)

Maka6er

Бывалый
*

Профиль
Группа: Участник
Сообщений: 150
Регистрация: 27.2.2007
Где: Киев

Репутация: нет
Всего: 3

Код

public String getstr();//Здесь
{return str;} 
public int getsize();//и здесь
{return size;}

Точку с запятой надо убрать.

Это сообщение отредактировал(а) Maka6er — 2.3.2008, 13:33

IndigoStill

Новичок

Профиль
Группа: Участник
Сообщений: 11
Регистрация: 21.10.2007

Репутация: нет
Всего: нет

Спасибо.  smile  теперь неделю посыпать себя пеплом.



















Правила форума «Java»
LSD

 
AntonSaburov

powerOn

 
tux

javastic

  • Прежде, чем задать вопрос, прочтите это!
  • Книги по Java собираются здесь.
  • Документация и ресурсы по Java находятся здесь.
  • Используйте теги [code=java][/code] для подсветки
    кода. Используйтe чекбокс «транслит«, если у Вас
    нет русских шрифтов.
  • Помечайте свой вопрос как решённый, если на него получен ответ. Ссылка «Пометить как решённый» находится над первым постом.
  • Действия модераторов можно обсудить здесь.
  • FAQ раздела лежит здесь.

Если Вам помогли, и атмосфера форума Вам понравилась, то заходите
к нам чаще! С уважением,
LSD,
AntonSaburov,
powerOn,
tux, javastic.

0 Пользователей читают эту тему (0 Гостей и 0 Скрытых Пользователей)
0 Пользователей:
« Предыдущая тема | Java: Общие вопросы | Следующая тема »

Понравилась статья? Поделить с друзьями:
  • Ricoh sp 212suw сброс ошибки sc556
  • Return code 1603 hp ошибка
  • Retry was not successful ошибка
  • Ricoh sp 202sn ошибка sc556
  • Retrofit ошибка 400