Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi, I've written a code in java. and need to use goto statement in it. I've checked for the solutions at different forums and came to solutions of using break label: statement but it gives error even I describe the label in my program like.

Java
break label:
..........
.........
label:


The code I'm trying is

Java
import javax.swing.JOptionPane;

/**
 *
 * @author hitman
 */
public class TestApp {
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        int tries = 3;
        String[] loginData;
        loginData = new String[100];
        short loginDataIndex = 0;
        
        do{
            String name;
            name = JOptionPane.showInputDialog("Enter Username");
            String pass = JOptionPane.showInputDialog("Enter Paswword");
            String captcha = JOptionPane.showInputDialog("3 * 6 = ");
            int captchaAnswer = Integer.parseInt(captcha);
            switch(captchaAnswer){
                case 18:
                 JOptionPane.showMessageDialog(null,"You are a Human","Login Sucessfull",JOptionPane.INFORMATION_MESSAGE);
                 break label:  // instead of goto
                 break;
                default:
                     JOptionPane.showMessageDialog(null,"You are not a Human", "Login Failed",JOptionPane.ERROR_MESSAGE);

            }
            loginData[loginDataIndex] = name;
            loginDataIndex++;
            --tries;
            JOptionPane.showMessageDialog(null,"You are Left with "+tries+" tries.","Tries Left",JOptionPane.WARNING_MESSAGE);
            
        }while(tries>0);
        label: //catches the break label statement
       
        JOptionPane.showMessageDialog(null,"Thanks for Visiting","Disabled",JOptionPane.INFORMATION_MESSAGE);
       if(loginData != null){
            short i = 0;
            String failedUsers = "";
            int n=0;
            while(loginData[n]!=null){
                failedUsers = failedUsers +" , "+loginData[n];
                n++;
            }
       
        JOptionPane.showMessageDialog(null,"Users "+failedUsers+" are not Human.","Captcha Error",JOptionPane.ERROR_MESSAGE);
       }
        System.exit(0);
        }
        
    }
Posted
Updated 25-Sep-14 6:44am
v2
Comments
Sergey Alexandrovich Kryukov 25-Sep-14 12:46pm    
Why using goto anywhere? Doesn't seem to be a good sign...
—SA
Fesi Nagetive 28-Sep-14 10:35am    
I know its not good practice to use goto. I've seen some people criticizing unconditional transfer of control. I just wanted to know its implementation for curiosity.

No, you don't need it. If you feel you need it, please try to train yourself to improve your code-writing skills just a bit more. Java lacks "goto" statement:
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/_keywords.html[^].

Not that even though "const" and "goto" are not used, they are reserved. It means that syntax prevents using then even as identifiers.

—SA
 
Share this answer
 
v3
Comments
[no name] 25-Sep-14 13:09pm    
5 from noob. It would be nice, to have an option to get a compiler error and for Java to get a runtime error if somebody uses goto :)
Kind regards, Bruno
Sergey Alexandrovich Kryukov 25-Sep-14 13:33pm    
Thank you, Bruno.
It should come out as compilation error if you try to use goto; just try it to see. This is not a warning, this is the lack of "goto".
—SA
CPallini 25-Sep-14 13:31pm    
5. Very good.
Sergey Alexandrovich Kryukov 25-Sep-14 13:33pm    
Thank you, Carlo.
—SA
Set the label above your do-while loop:
C#
label: //catches the break label statement
        do{
            String name;
            name = JOptionPane.showInputDialog("Enter Username");
            String pass = JOptionPane.showInputDialog("Enter Paswword");
            String captcha = JOptionPane.showInputDialog("3 * 6 = ");
            int captchaAnswer = Integer.parseInt(captcha);
            switch(captchaAnswer){
                case 18:
                 JOptionPane.showMessageDialog(null,"You are a Human","Login Sucessfull",JOptionPane.INFORMATION_MESSAGE);
                 break label;  // instead of goto
                default:
                     JOptionPane.showMessageDialog(null,"You are not a Human", "Login Failed",JOptionPane.ERROR_MESSAGE);

            }
            loginData[loginDataIndex] = name;
            loginDataIndex++;
            --tries;
            JOptionPane.showMessageDialog(null,"You are Left with "+tries+" tries.","Tries Left",JOptionPane.WARNING_MESSAGE);

        }while(tries>0);
 
Share this answer
 
Comments
Fesi Nagetive 28-Sep-14 9:35am    
what if I wanted the label below not above. is there any possibility? I know its not good practice to use goto. I've seen some people criticizing unconditional transfer of control. I just wanted to know its implementation for curiosity.
Xiao Ling 28-Sep-14 19:49pm    
No, you can't make it below. It's explicitly explained in the book - Thinking in Java

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900