Results 1 to 10 of 10
  1. #1
    Chackan is offline Member
    Join Date
    Jun 2012
    Posts
    3
    Rep Power
    0

    Default How to use "return"?

    Hey there. Currently very new to Java, took classes but the teacher could be better so...trying to train and learn with some self-study!

    Anyway, I have a Switch with several options. What I want is to return to the initial menu if a wrong option is given!

    Example:

    public class MenuPrincipal {


    public static void main(String[] args) {

    System.out.println("Menu");
    System.out.println("Choose an option:");
    System.out.println("1-First option");
    System.out.println("2-Second option");
    System.out.println("0-Exit");

    Scanner s = new Scanner(System.in);
    int opcao = s.nextInt();

    switch ( opcao ) {
    case 1:
    System.out.println("Option 1");
    break;
    case 2:
    System.out.println("Option 2");
    break;
    case 0:
    System.out.println("Goodbye");
    System.exit(2);
    default:
    System.out.println("Invalid Option. ");
    return;
    I want to return to the bolded part!

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,789
    Rep Power
    25

    Default Re: How to use "return"?

    The return statement is for exiting the currently executing method. I think what you want to do is to loop back to an earlier section of code within a method. If you put the code you want to repeat inside a loop, then at any point inside that loop you can "return" to the beginning of the loop by using the continue statement.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Chackan is offline Member
    Join Date
    Jun 2012
    Posts
    3
    Rep Power
    0

    Default Re: How to use "return"?

    Ahhh ok! So the return statement is only used to exit the executing code, with some sort of answer, is that it?

    By loop, do you mean while, if, etc?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,789
    Rep Power
    25

    Default Re: How to use "return"?

    return exits the currently executing method (possibly with a value) returning to the caller of the method.

    for and while are two kinds of loops. The if statement is not a loop
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default Re: How to use "return"?

    Anyway, I have a Switch with several options. What I want is to return to the initial menu if a wrong option is given!
    I'll give you a little clue just to get idea how it could be done. For example:

    Java Code:
    public static void main(String[] args) {
    
    int opcao = 0;
    
    while(!(opcao == 0)) {
    
    // your code here
    
    opcao = s.nextInt();
    
    // your code here
    }
    
    }

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,789
    Rep Power
    25

    Default Re: How to use "return"?

    @cselic Did you test your code? How many times will the code inside the while() loop execute?
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default Re: How to use "return"?

    ^You're right, my previous code is not good. Here is the better one:

    Java Code:
    public static void main(String[] args) {
     
    int opcao = -1;
     
    while(!(opcao == 0)) {
     
    // your code here
     
    opcao = s.nextInt();
     
    // your code here
    }
     
    }

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,789
    Rep Power
    25

    Default Re: How to use "return"?

    while(!(opcao == 0)) {
    Why use both ! and ==
    why not just use != and make it clearer and easier to understand?
    while(opcao != 0) {
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default Re: How to use "return"?

    ^Norm you're right. That's much better.

    Java Code:
    public static void main(String[] args) {
      
    int opcao = -1;
      
    while((opcao != 0)) {
      
    // your code here
      
    opcao = s.nextInt();
      
    // your code here
    }
      
    }

  10. #10
    Chackan is offline Member
    Join Date
    Jun 2012
    Posts
    3
    Rep Power
    0

    Default Re: How to use "return"?

    Ok, I created the loop! Now I just need to make the loop return to the beggining of the menu! Will try searching for it, if I don't find anything, I will bug you guys a bit more :P

    Thanks mates!

Similar Threads

  1. Replies: 4
    Last Post: 06-07-2012, 10:24 AM
  2. Replies: 17
    Last Post: 05-28-2012, 09:30 PM
  3. Simple "Global" function to return random int
    By mac666 in forum New To Java
    Replies: 2
    Last Post: 01-10-2012, 04:13 PM
  4. Replies: 3
    Last Post: 05-25-2009, 09:50 PM
  5. Replies: 1
    Last Post: 10-20-2008, 08:35 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
  •