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

Thread: Iterating a String

  1. #1
    Member
    Join Date
    Jun 2011
    Posts
    43
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Iterating a String

    String String1 = new String();

    String1 = "bubble";


    My method : DisplayLetter(char aChar);



    If I execute
    DisplayLetter(String1.charAt(0));
    DisplayLetter(String1.charAt(1));
    DisplayLetter(String1.charAt(2));
    DisplayLetter(String1.charAt(3));
    DisplayLetter(String1.charAt(4));
    DisplayLetter(String1.charAt(5));

    it will display
    b
    u
    b
    b
    l
    e
    I would like to be able to achieve the above by using a loop:


    for (int count = 0; count < String1.length; count++)
    DisplayLetter(String1.charAt(?));


    Can you advise one correct format.

    rgds.
    Last edited by av8; July 5th, 2011 at 05:04 AM.


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

    Default Re: Iterating a String

    In the code for a loop that you posted, what variable is going to start at 0 and increment by one every time thru the loop?

    Does the code you posted compile? length is a method not a variable in the String class

    Java naming conventions: variable names start with lowercase letters: string1 vs String1

    Also I'd recommend that you put a { at the end of the if statement and a } following the last line that you want to be in the loop. Align the ending } with the start of the if statement. Indent the code within the {}s.

  3. #3
    Member
    Join Date
    Jun 2011
    Posts
    43
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Iterating a String

    I'm getting there slowly. I can have the loop counter as an actual parameter to itererate through a string object

    DisplayLetter(String1.charAt(for (int count= 0; count < aString.length; count++));

    but i'm getting illegal start of expression. What is my error , can any top java guy help on my code above so it runs ok.

    Cheers.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Iterating a String

    That's an interesting construction of a statement.
    Your previous post was almost there, you just had to put the loop index into the method call.
    Now what have you done?
    What does the API doc for the charAt() method say it takes for an argument?
    Certainly not a for loop.
    You could use an expression of some kind as the argument for charAt() as long as it returned the correct type of value. for loops are not expressions that return values.

  5. #5
    Member
    Join Date
    Jun 2011
    Posts
    43
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Iterating a String

    Can you advise on the syntax how to put the loop index into the method call.

    Thank you.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Iterating a String

    Replace the ? with count

  7. #7
    Member
    Join Date
    Jun 2011
    Posts
    43
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Iterating a String

    Thanks Norm I've tried this and it displays only the last letter e.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Iterating a String

    I've tried this and it displays only the last letter
    What did you try? I don't see any code.

  9. #9
    Member
    Join Date
    Jun 2011
    Posts
    43
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Iterating a String

    for(int count = 0; count < aString.length(); count++)
    DisplayLetter(aString.charAt(count));

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Iterating a String

    This is painful. Do I have to ask you for the source line by line?
    You don't show what the value of aString is or the complete output from your program.

  11. #11
    Member
    Join Date
    Jun 2011
    Posts
    43
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Iterating a String

    Sorry,

    String aString = new String();



    aString = "bubble";

    for(int count = 0; count < aString.length(); count++)
    DisplayLetter(aString.charAt(count));

    the out put is e

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Iterating a String

    For the next lines of code I need to see: The DisplayLetter method.

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Iterating a String

    Execute this code and see what is printed out:
          String aString = "bubble";
          for(int count = 0; count < aString.length(); count++) {
              System.out.println(count + " " + aString.charAt(count)); 
          } // end for(count)

  14. #14
    Member
    Join Date
    Jun 2011
    Posts
    43
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Iterating a String

    Another scenario

    String String1 = new String();

    String1 = "bubble";

    String1.charAt(0);
    String1.charAt(1);
    String1.charAt(2);
    String1.charAt(3);
    String1.charAt(4);
    String1.charAt(5);

    when I execute the above 6 lines individually the output corresponds to the index value.

    I would like to output all the individual characters using the code below but I do not know what I am missing.... their is no output.

    for (int count = 0; count < String1.length(); count++)
    String1.charAt(count);

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Iterating a String

    for (int count = 0; count < String1.length(); count++) 
      String1.charAt(count);
    vs
    for(int count = 0; count < aString.length(); count++)
       DisplayLetter(aString.charAt(count));
    Why did you remove the call to the DisplayLetter() method?

    The following line returns a char, but what does the compiler do with that returned value?????
    String1.charAt(count);

  16. #16
    Member
    Join Date
    Jun 2011
    Posts
    43
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Iterating a String

    Thank you Norm for your patience

    for(int count = 0; count < aString.length(); count++)
    {
    DisplayLetter(aString.charAt(count));
    }
    I had missed out the curlys, program works now. thanks again, Goodnight.

  17. #17
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Iterating a String

    Adding the braces made zero difference. Your code should have still worked without them. If it didn't then something else in your code was wrong and you fixed it without realising.
    Improving the world one idiot at a time!

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Iterating a String

    I'm afraid there is a lot of magic going on here.

  19. #19
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Iterating a String

    Did I miss the DisplayLetter method code posting, or is it Top Secret?

Similar Threads

  1. While loop not iterating correctly
    By Rhyssa6 in forum What's Wrong With My Code?
    Replies: 15
    Last Post: May 18th, 2011, 09:13 PM
  2. Replies: 18
    Last Post: March 2nd, 2011, 10:52 AM
  3. [SOLVED] String Matcher finding only char not a whole string
    By Kakashi in forum What's Wrong With My Code?
    Replies: 11
    Last Post: February 18th, 2011, 09:58 AM
  4. Replies: 2
    Last Post: December 22nd, 2010, 09:21 AM
  5. [HELP] Iterating through a jagged edged array
    By AlexBeer in forum Collections and Generics
    Replies: 2
    Last Post: April 12th, 2010, 06:33 AM