Results 1 to 12 of 12
  1. #1
    Mr.abe90 is offline Member
    Join Date
    May 2011
    Posts
    18
    Rep Power
    0

    Arrow 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. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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. #3
    Mr.abe90 is offline Member
    Join Date
    May 2011
    Posts
    18
    Rep Power
    0

    Default

    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. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,658
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Mr.abe90 View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Mr.abe90 is offline Member
    Join Date
    May 2011
    Posts
    18
    Rep Power
    0

    Default

    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. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,658
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Mr.abe90 View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    Mr.abe90 is offline Member
    Join Date
    May 2011
    Posts
    18
    Rep Power
    0

    Default

    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++){
    ^
    Password.java:3: <identifier> expected
    for(y=1;y<10;y++){
         ^
    Password.java:3: ';' expected
    for(y=1;y<10;y++){
          ^
    Password.java:3: illegal start of type
    for(y=1;y<10;y++){
              ^
    Password.java:3: <identifier> expected
    for(y=1;y<10;y++){
                ^
    Password.java:3: ';' expected
    for(y=1;y<10;y++){
                 ^
    Password.java:3: illegal start of type
    for(y=1;y<10;y++){
                  ^
    Password.java:3: <identifier> expected
    for(y=1;y<10;y++){
                    ^
    Password.java:3: ';' expected
    for(y=1;y<10;y++){
                     ^
    Password.java:4: illegal start of type
       for(int x=1;x<=5;x++){
       ^
    Password.java:4: <identifier> expected
       for(int x=1;x<=5;x++){
          ^
    Password.java:4: ';' expected
       for(int x=1;x<=5;x++){
           ^
    Password.java:4: <identifier> expected
       for(int x=1;x<=5;x++){
                ^
    Password.java:4: <identifier> expected
       for(int x=1;x<=5;x++){
                    ^
    Password.java:4: illegal start of type
       for(int x=1;x<=5;x++){
                      ^
    Password.java:4: <identifier> expected
       for(int x=1;x<=5;x++){
                       ^
    Password.java:4: ';' expected
       for(int x=1;x<=5;x++){
                        ^
    Password.java:4: illegal start of type
       for(int x=1;x<=5;x++){
                         ^
    Password.java:4: <identifier> expected
       for(int x=1;x<=5;x++){
                           ^
    Password.java:4: ';' expected
       for(int x=1;x<=5;x++){
                            ^
    Password.java:5: <identifier> expected
          System.out.print("z");
                          ^
    Password.java:5: illegal start of type
          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. #8
    Mr.abe90 is offline Member
    Join Date
    May 2011
    Posts
    18
    Rep Power
    0

    Default

    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. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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. #10
    Mr.abe90 is offline Member
    Join Date
    May 2011
    Posts
    18
    Rep Power
    0

    Default

    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. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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. #12
    Mr.abe90 is offline Member
    Join Date
    May 2011
    Posts
    18
    Rep Power
    0

    Default

    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.

Similar Threads

  1. Firt letter with T
    By globo in forum New To Java
    Replies: 4
    Last Post: 02-25-2011, 09:04 PM
  2. OpenFaces 2.0: Open New Possibilities for your Web Applications
    By TeamDev in forum Reviews / Advertising
    Replies: 1
    Last Post: 04-01-2010, 05:09 PM
  3. Replies: 10
    Last Post: 02-20-2010, 08:25 PM
  4. Letter with Letters
    By elgatoboricua in forum New To Java
    Replies: 7
    Last Post: 09-16-2008, 02:59 PM
  5. Replies: 20
    Last Post: 05-14-2008, 09:42 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •