# Thread: Loop through all letter and number possibilities?

1. Member
Join Date
May 2011
Posts
18
Rep Power
0

## Loop through all letter and number possibilities?

What I mean is how do i make a loop in java that cycles through all possible combinations of letters and numbers (with a limit of course). Like have it start with 0 and next have it be 00, 000, 0000... And then after like 5 digits of zeros have a 1 added (00001) And continue on adding letters.

I realize it would take a very long time to list them all but i still want to know what the code would be like.
Last edited by JosAH; 05-29-2011 at 11:02 AM. Reason: removed the big font

2. We don't really write code for people, instead we help people out. Have you tried this yet? If so, why not post your code(with [code] tags [/code]) and any problems you are having.

3. Member
Join Date
May 2011
Posts
18
Rep Power
0
I have this so far.
Java Code:
```int z=0;
for(y=1;y<10;y++){

for(int x=1;x<=5;x++){
System.out.print("z");
}
z++}```
Te error is ';' expected in "z++}"
it's probably a simple answer but I have not been coding in java for to long.

4. Originally Posted by Mr.abe90
I have this so far.
Java Code:
```int z=0;
for(y=1;y<10;y++){

for(int x=1;x<=5;x++){
System.out.print("z");
}
z++}```
Te error is ';' expected in "z++}"
it's probably a simple answer but I have not been coding in java for to long.
Not long enough: in Java statements are terminated by a semi colon; you forgot one and the compiler started to whine about it.

kind regards,

Jos

5. Member
Join Date
May 2011
Posts
18
Rep Power
0
Not long enough: in Java statements are terminated by a semi colon; you forgot one and the compiler started to whine about it.

kind regards,

Jos
Well am I suppost to put the semi colon before the "}", after z++} or get rid of the "}"?

6. Originally Posted by Mr.abe90
Well am I suppost to put the semi colon before the "}", after z++} or get rid of the "}"?
Those curly brackets have to match; b.t.w. don't put that curly bracket there; put it on its own line like this:

Java Code:
```int z=0;
for(y=1;y<10;y++){
for(int x=1;x<=5;x++){
System.out.print("z");
}
z++;
}```
kind regards,

Jos

7. Member
Join Date
May 2011
Posts
18
Rep Power
0
Those curly brackets have to match; b.t.w. don't put that curly bracket there; put it on its own line like this:

Java Code:
```int z=0;
for(y=1;y<10;y++){
for(int x=1;x<=5;x++){
System.out.print("z");
}
z++;
}```
kind regards,

Jos
These are the errors I get when i type that exact code.
Java Code:
```Password.java:3: illegal start of type
for(y=1;y<10;y++){
^
for(y=1;y<10;y++){
^
for(y=1;y<10;y++){
^
for(y=1;y<10;y++){
^
for(y=1;y<10;y++){
^
for(y=1;y<10;y++){
^
for(y=1;y<10;y++){
^
for(y=1;y<10;y++){
^
for(y=1;y<10;y++){
^
for(int x=1;x<=5;x++){
^
for(int x=1;x<=5;x++){
^
for(int x=1;x<=5;x++){
^
for(int x=1;x<=5;x++){
^
for(int x=1;x<=5;x++){
^
for(int x=1;x<=5;x++){
^
for(int x=1;x<=5;x++){
^
for(int x=1;x<=5;x++){
^
for(int x=1;x<=5;x++){
^
for(int x=1;x<=5;x++){
^
for(int x=1;x<=5;x++){
^
System.out.print("z");
^
System.out.print("z");
^
Password.java:7: class, interface, or enum expected
z++;
^
Password.java:8: class, interface, or enum expected
}
^
24 errors```
I have no idea what to do.

8. Member
Join Date
May 2011
Posts
18
Rep Power
0
Ok, so i changed some things around and this is my complete code.
Java Code:
```public class Project1 {
public static void main(String[] args) {
int z=0;
for(y=1;y<10;y++){
for(int x=1;x<=5;x++){
System.out.print("z");
}
z++;
}
}
}```

The errors are:
Java Code:
```cannot find symbol
symbol  : variable y
for(y=1;y<10;y++){
^
for(y=1;y<10;y++){
^
for(y=1;y<10;y++){
^```

9. The solution is already in your code: Look at the difference between the the x for loop that has no errors and the y for loop that has the errors.

10. Member
Join Date
May 2011
Posts
18
Rep Power
0
I got a different code from someone else and it works great.
Java Code:
```int i, j, k, m, n;
String d1, d2, d3, d4, d5;
String digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

// First print 1-digit "numbers"
for(i = 0; i < 36; i++){
d1 = digits.substring(i, i+1 );
System.out.println(d1);
}

System.out.println();

// Next, print 2-digit "numbers"
for(i = 0; i < 36; i++){
d1 = digits.substring(i, i+1 );
for( j = 0; j < 36; j++ ) {
d2 = d1 + digits.substring(j, j+1 );
System.out.println(d2);
}
}

System.out.println();

// Now print 3-digit "numbers"
for(i = 0; i < 36; i++){
d1 = digits.substring(i, i+1 );
for( j = 0; j < 36; j++ ) {
d2 = d1 + digits.substring(j, j+1 );
for( k = 0; k < 36; k++ ) {
d3 = d2 + digits.substring(k, k+1 );
System.out.println(d3);
}
}
}

System.out.println();

// Now print 4-digit "numbers"
for(i = 0; i < 36; i++){
d1 = digits.substring(i, i+1 );
for( j = 0; j < 36; j++ ) {
d2 = d1 + digits.substring(j, j+1 );
for( k = 0; k < 36; k++ ) {
d3 = d2 + digits.substring(k, k+1 );
for( m = 0; m < 36; m++) {
d4 = d3 + digits.substring( m, m+1 );
System.out.println(d4);
}
}
}
}

System.out.println();

// Finally, print 5-digit "numbers"
for(i = 0; i < 36; i++){
d1 = digits.substring(i, i+1 );
for( j = 0; j < 36; j++ ) {
d2 = d1 + digits.substring(j, j+1 );
for( k = 0; k < 36; k++ ) {
d3 = d2 + digits.substring(k, k+1 );
for( m = 0; m < 36; m++) {
d4 = d3 + digits.substring( m, m+1 );
for( n = 0; n < 36; n++) {
d5 = d4 + digits.substring( n, n+1 );
System.out.println(d5);
}
}
}
}
}```
But Thanks For All Your Help!

11. Oh for the love of everything beautiful, at least figure out what you did wrong!

Again, what is the difference between this:
Java Code:
`for (y=1;y<10;y++){ // causes errors`
and this:
Java Code:
`for ([b][color="red"]int[/color][/b] x=1;x<=5;x++){  // no errors`

12. Member
Join Date
May 2011
Posts
18
Rep Power
0
Oh for the love of everything beautiful, at least figure out what you did wrong!

Again, what is the difference between this:
Java Code:
`for (y=1;y<10;y++){ // causes errors`
and this:
Java Code:
`for (int x=1;x<=5;x++){  // no errors`
I know I did eventually figure to do just that.
Works fine now.