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 7 of 7

Thread: Looping questions

  1. #1
    Member
    Join Date
    Sep 2012
    Posts
    56
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Looping questions

    I am trying to build up the alphabet character by character. Like:
    a
    ab
    abc
    abcd

    Well as of now I can only print out the alphabet with my loop like abcdefg...z. Can anyone help me out?

    So here is the code I am trying to use:
    public void loop4()
        {
            char alph = 'a';
                while(alph <= 'z')
                {
     
                    System.out.println(alph);
                    alph++;
     
                }
        }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Looping questions

    What is the logic for the printout?
    Is it something like this:
    print 1 char on first line
    print 2 chars on second line
    print 3 chars on third line
    etc
    print 26 chars on the 26th line

    Do you see the pattern there?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: Looping questions

    It's either you make a nested loop for printing array from a-next.

    Or you make a string variable and concatenate the the characters in that string and print it, that way you're not going to need the nested loop

  4. #4
    Member
    Join Date
    Sep 2012
    Posts
    56
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Looping questions

    Still confused... how can a nested loop help? The trouble I am having is that everytime I loop through once, I loose the previous letter. What would the loop within this loop be doing? Or do I need my current loop inside another loop?

  5. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Looping questions

    You should consider the StringBuilder class to help with this exercise.

  6. #6
    Member
    Join Date
    Feb 2014
    Posts
    180
    Thanks
    0
    Thanked 48 Times in 45 Posts

    Default Re: Looping questions

    Using a StringBuilder to concatenate characters together is probably the easier way. The basic idea is as you loop through from 'a' to 'z', you concatenate/append the next letter in the alphabet to the StringBuilder, and then print out the StringBuilder.

    Here's something to help you get started using bits of what you already have:

    char alph = 'a';
    StringBuilder strBuilder = new StringBuilder(); // <= Initialise the StringBuilder here
     
    while (alph <= 'z') {
        // Write code to append alph to the StringBuilder
        // Write code to print out the contents of the StringBuilder
     
        alph++;
    }

    See StringBuilder (Java Platform SE 7 ) for the API documentation for StringBuilder to find out the code you need to write above.

    The nested-loop method can be a bit mind-bending for the novice. The basic idea is to use 2 variables to keep track of the iterations in each of the loops, with the inner loop's iterations dependent on the progress of the outer loop. The following is an example using nested 'while' loops:

    char outerAlph = 'a';
    while (outerAlph <= 'z') {
        char innerAlph = 'a';
        while (innerAlph <= outerAlph) {
            // Write code here
     
            innerAlph++;
        }
        // Write code here
     
        outerAlph++;
    }

  7. The Following User Says Thank You to jashburn For This Useful Post:

    vysero (March 10th, 2014)

  8. #7
    Member
    Join Date
    Sep 2012
    Posts
    56
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Looping questions

    I understand now thank you!

Similar Threads

  1. List of my Java3D Questions, and Proguard questions
    By Zachary1234 in forum Java SE APIs
    Replies: 0
    Last Post: November 16th, 2012, 09:40 PM
  2. Looping Over and Over Again?
    By avalanche72 in forum Loops & Control Statements
    Replies: 1
    Last Post: February 1st, 2012, 05:11 AM
  3. Need help with looping!
    By crsoccerplayer6 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 29th, 2011, 04:09 PM
  4. Help in looping
    By endframe in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 28th, 2010, 03:24 PM
  5. [SOLVED] looping, for,while,do-while.
    By chronoz13 in forum Loops & Control Statements
    Replies: 4
    Last Post: August 6th, 2009, 01:32 PM