Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: For Loop Help

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default For Loop Help

    Hi
    I been trying to figure out this problem for last few days but cannot find the answer.
    I am trying to use nested for loops to try and create a 6 character string using letters/numbers from a array. I need it so that it starts off with one character and then adds one more etc.

    Example is
    a
    b
    ..z
    then
    aa
    ab
    ac
    ...az

    or even

    a
    z
    aa
    ba
    za etc

    here is my code so far

    char[] letters = { 
              'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
             's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0','1','2','3','4','5','6','7','8','9',
          };
          boolean found = false;
          char[] guess = new char[7];
          int pos = 0, i = 0;
          String word;
     
      // while (found == false) {
            loop:
     
                for (int a = 0; a<36; a++){
     
                guess[5] = letters[i]; 
     
                 for (int b = 0; b<36; b++){ 
     
                  guess[4] = letters[i]; 
     
                     for (int c = 0; c<36; c++){ 
     
                            guess[3] = letters[i]; 
     
                     for (int d = 0; d<36; d++){
     
                             guess[2] = letters[i]; 
     
                            for (int e = 0; e<36; e++){
     
                                guess[1] = letters[e]; 
     
                                for (int f = 0; f<36; f++){ 
     
       guess[0] = letters[f]; 
     
         word = String.valueOf(guess);

    thanks
    Last edited by nekromanik; February 26th, 2011 at 09:00 PM.


  2. #2
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: For Loop Help

    I'm confused.. what are you trying to do? Your examples don't make a whole lot of sense, but I think I have it but need to be sure..

    It prints aa, than adds one letter to ab, than ac, when it gets to z it restarts ba? Most of your for loops are unnecessary if thats the case, but than you have boolean found.. so I'm confused.

  3. #3
    Member vanDarg's Avatar
    Join Date
    Jan 2011
    Location
    Chicago
    Posts
    65
    My Mood
    Mellow
    Thanks
    1
    Thanked 7 Times in 7 Posts

    Default Re: For Loop Help

    I'm not sure where you are going with this code, and I suspect you don't have much experience with for loops. I recommend reading:

    The for Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)

    It explains the concept of for loops very well. The examples alone should give you a decent explanation.
    "Everything should be made as simple as possible, but not simpler."
    Asking Questions for Dummies | The Java Tutorials | Java Coding Styling Guide

Similar Threads

  1. help using for while loop
    By robertsbd in forum What's Wrong With My Code?
    Replies: 8
    Last Post: October 30th, 2010, 02:36 PM
  2. help with a for loop
    By newbie79 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 3rd, 2010, 02:20 AM
  3. for loop and while loop problems
    By Pulse_Irl in forum Loops & Control Statements
    Replies: 4
    Last Post: May 3rd, 2010, 02:09 AM
  4. hi. i want to rewrite this do loop into a while loop.
    By etidd in forum Loops & Control Statements
    Replies: 3
    Last Post: January 26th, 2010, 05:27 PM
  5. Need help with loop
    By SwEeTAcTioN in forum Loops & Control Statements
    Replies: 8
    Last Post: October 25th, 2009, 05:59 PM