Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 1 of 2 12 LastLast
Results 1 to 25 of 44

Thread: return outside method

  1. #1
    Member
    Join Date
    Jul 2014
    Posts
    48
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default return outside method

    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. #2
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: return outside method

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

  3. #3
    Member
    Join Date
    Jul 2014
    Posts
    48
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: return outside method

    so... make a new method?

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    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.

  5. #5
    Member
    Join Date
    Jul 2014
    Posts
    48
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    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

  6. #6
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    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

  7. #7
    Member
    Join Date
    Jul 2014
    Posts
    48
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: return outside method

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

    return "nine";
    ^
    error: return outside of methood

  8. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    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.

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

    lucas29 (July 27th, 2014)

  10. #9
    Member
    Join Date
    Jul 2014
    Posts
    48
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    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

  11. #10
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    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.

  12. #11
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    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.

  13. #12
    Member
    Join Date
    Jul 2014
    Posts
    48
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    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

  14. #13
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    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.

  15. #14
    Member
    Join Date
    Jul 2014
    Posts
    48
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    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?

  16. #15
    Member
    Join Date
    Jul 2014
    Posts
    48
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: return outside method

    is there still no one that is helping me?

  17. #16
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    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.

  18. #17
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    414
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    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)

  19. #18
    Member
    Join Date
    Jul 2014
    Posts
    48
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    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
    			}
    		}
    		}
    		}
    	}

  20. #19
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    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.

  21. #20
    Member
    Join Date
    Jul 2014
    Posts
    48
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    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.

  22. #21
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    414
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    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)

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

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

  24. #22
    Member
    Join Date
    Jul 2014
    Posts
    48
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: return outside method

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

    --- Update ---

    i using java 7, i think

  25. #23
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    414
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    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)

  26. #24
    Member
    Join Date
    Jul 2014
    Posts
    48
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: return outside method

    the syntax errors are the same

  27. #25
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    414
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    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)

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 1
    Last Post: May 27th, 2014, 07:39 PM
  2. Please help with method return statement!
    By brobertson300 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: April 3rd, 2014, 07:10 AM
  3. can i return two values from a return method?
    By tonu in forum Object Oriented Programming
    Replies: 4
    Last Post: January 1st, 2014, 11:02 AM
  4. return from a method
    By MHS in forum Java Theory & Questions
    Replies: 1
    Last Post: June 15th, 2013, 01:06 PM
  5. Method return value(s)
    By mwr76 in forum Loops & Control Statements
    Replies: 3
    Last Post: October 2nd, 2011, 02:38 PM