Hello experts,
actually i have a situation like this.
i have a drop down menu value:{2,3,4,5,6}. there is a common operation need to be done if any number in menu list chosen.each of the number must have different number of layers. if user choose 2 means 2 layers,if 3 means 3 layers and so on. this is my plan.
comb[0] is layer

for(int i=0;i<comb[0];i++)
 for(int j=0;j<comb[1];j++)
  { if(value_int==2)  goto A:
     else for(int k=0;k<comb[2];k++)
           { if(value_int==3)  goto A:
             else for(int l=0;k<comb[3];k++)
                   { if(value_int==4)  goto A:
                     else for(int m=0;k<comb[4];k++)
                           { if(value_int==5)  goto A:
                             else for(int n=0;k<comb[5];k++)
                                  goto A:
                            }
                   }
            }
   }

A: //do A

so, my question is,can i use goto? or it is not available in java?
or mayb u have any suggestion to my code?i knew its not a good way to be done.
really need help for this guys!!!

Thanks for advance.

Recommended Answers

All 16 Replies

There is no goto keyword in java that will take you to a certain line. According to wikipedia the goto keyword is reserved in java but not actually used.

So, goto is not a choice.. changing the code should be though. i'll leave that to someone a little more equipped when it comes to java though :P

There is no goto keyword in java that will take you to a certain line. According to wikipedia the goto keyword is reserved in java but not actually used.

So, goto is not a choice.. changing the code should be though. i'll leave that to someone a little more equipped when it comes to java though :P

any suggestion to my program?

no it's not in JAVA

Use methods. Your example and explanation are convoluted and I can't give more specific advice, but to "do something" under certain conditions (i.e. in a control structure such as a for loop, if statement, etc) you can call a method.

Use methods. Your example and explanation are convoluted and I can't give more specific advice, but to "do something" under certain conditions (i.e. in a control structure such as a for loop, if statement, etc) you can call a method.

i understand u.but can i use 'for' more than 2 times?

Hello experts,
actually i have a situation like this.
i have a drop down menu value:{2,3,4,5,6}. there is a common operation need to be done if any number in menu list chosen.each of the number must have different number of layers. if user choose 2 means 2 layers,if 3 means 3 layers and so on. this is my plan.
comb[0] is layer

for(int i=0;i<comb[0];i++)
 for(int j=0;j<comb[1];j++)
  { if(value_int==2)  goto A:
     else for(int k=0;k<comb[2];k++)
           { if(value_int==3)  goto A:
             else for(int l=0;k<comb[3];k++)
                   { if(value_int==4)  goto A:
                     else for(int m=0;k<comb[4];k++)
                           { if(value_int==5)  goto A:
                             else for(int n=0;k<comb[5];k++)
                                  goto A:
                            }
                   }
            }
   }

A: //do A

so, my question is,can i use goto? or it is not available in java?
or mayb u have any suggestion to my code?i knew its not a good way to be done.
really need help for this guys!!!

Thanks for advance.

actually its like more than double array..i dont know how to tell it.
example when user choose 2 from dropdown menu.
array a[0]= 0,1,1 array b[0]=0,1,2
a[1]= 1,0,1 b[1]=0,1,2
a[2]= 1,1,0 b[2]=0,1,2


for a[0][2], it must list all in b[2]
a[0][1], it must list all in b[1]
a[0][0], randomly choose data in b[0]
for a[1][2], it must list all in b[2]
a[1][1], randomly choose data in b[1]
a[1][0], it must list all in b[0]
for a[2][2], randomly choose data in b[2]
a[2][1], it must list all in b[1]
a[2][0], it must list all in b[0]

and so on with value=3,4,5,6 the different is the size of both a and b array. for array a, value=number of '1' and b must be more than value

no,,, use loop is just the same as go to but it has a condition...

no,,, use loop is just the same as go to but it has a condition...

could u please elaborate more?please...

here a sample:
I don't know if this is what you need
I use "goto" when I'm using/putting a choice in my program
just like the below example....

hope this help...

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class sample {
	public static void main( String[] args ) {
		try {
			char choi;
			BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
			do {
				System.out.print("Enter your name: ");
				String name = input.readLine();
				System.out.println(name);
				System.out.print("Try again?Y/N");
				choi = (char) input.read();
			} while(choi=='Y' || choi=='y');
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
}

or how about use a function,,,,

public static void A() {
     //code here
}


///////////////////////

for(int i=0;i<comb[0];i++)
 for(int j=0;j<comb[1];j++)
  { if(value_int==2)  goto A:
     else for(int k=0;k<comb[2];k++)
           { if(value_int==3)  A();
             else for(int l=0;k<comb[3];k++)
                   { if(value_int==4)  A();
                     else for(int m=0;k<comb[4];k++)
                           { if(value_int==5) A();
                             else for(int n=0;k<comb[5];k++)
                                 A();
                            }
                   }

try this code

break A:

you can throw some Exception and handle it instead of

goto A:

but goto is deprecated in java. its not recommended to use goto

commented: utterliy and completely wrong -2

but goto is deprecated in java. its not recommended to use goto

Wrong. So utterly wrong I can't begin to describe it.
Goto is NOT deprecated.
It is a reserved word that was never implemented.

This was done quite deliberately because there's never a need for goto.
Any programmer who thinks they need it have serious design flaws and need to rethink their architecture, start to learn programming Java rather than Basic.

Loops, methods, etc. are where it's at, not random jumping around in code.

ok i might be wrong.
but still if you want to implement it.

break A; //semicolon at the end

Thanks guys...so much.
I think i prefer use method...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.