Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By JosAH

Thread: Beginner Loop Question

  1. #1
    katiebear128 is offline Member
    Join Date
    Sep 2011
    Posts
    54
    Rep Power
    0

    Default Beginner Loop Question

    The Question: Suppose s is any string. Write a code fragment, including a loop, that counts the number of r's (upper or lower case in s), and then prints out that count

    My solution:
    public class tester{
    public static void main(String[] args){
    String s="Roar";
    int total=0;
    s.toLowerCase();
    for(int j=0; j<=s.length()-1;j++)
    {if (s.charAt(j)=='r')
    total++;
    System.out.println(total);
    }
    }
    }


    this gives me an output of
    0001.

    How do I make the program print out just 1 number at the end and count the upper case R? Thanks for any help in advance.

  2. #2
    mwr1976 is offline Senior Member
    Join Date
    Oct 2011
    Posts
    106
    Rep Power
    0

    Default Re: Beginner Loop Question

    Your println is executing each iteration of the loop so if you want to print it one time after the iterations are done you would.............

  3. #3
    katiebear128 is offline Member
    Join Date
    Sep 2011
    Posts
    54
    Rep Power
    0

    Default Re: Beginner Loop Question

    got it. I move the println out of the loop statement...& I put in another if statement for the other r. I just don't get why the s.toLowerCase still print out Roar rather than roar

  4. #4
    RichersooN is offline Senior Member
    Join Date
    Jan 2011
    Location
    Rizal Province, Philippiines
    Posts
    167
    Rep Power
    0

    Default Re: Beginner Loop Question

    you did'nt use the s.LowerCase you just use the raw string
    Java Code:
    public class tester{
    public static void main(String[] args){
    String s="Roar";
    int total=0;
    s.toLowerCase(); ///////////////you did not use me
    for(int j=0; j<=s.length()-1;j++)
    {if (s.charAt(j)=='r')
    total++;
    System.out.println(total);
    }
    }
    }
    you use it in your print statement if you want to show s in lowercase:
    Java Code:
    String t = s.toLowerCase(); 
    System.out.println(t);
    now it should print all lowercase

  5. #5
    katiebear128 is offline Member
    Join Date
    Sep 2011
    Posts
    54
    Rep Power
    0

    Default Re: Beginner Loop Question

    ok, thanks so much for pointing that out! So basically my original statement did nothing. Thank you for clearing that up

  6. #6
    RichersooN is offline Senior Member
    Join Date
    Jan 2011
    Location
    Rizal Province, Philippiines
    Posts
    167
    Rep Power
    0

    Default Re: Beginner Loop Question

    did we help you? Please press the add the reputation

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,354
    Blog Entries
    7
    Rep Power
    20

    Default Re: Beginner Loop Question

    Quote Originally Posted by RichersooN View Post
    did we help you? Please press the add the reputation
    No, you didn't help the op; Strings are immutable and a simple s.toLowerCase() doesn't do anything; please check your facts before you post a (non) solution and begging for rep points is sort of childish too.

    Jos
    Last edited by JosAH; 10-12-2011 at 11:04 AM.
    mine0926 likes this.
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Java Beginner: Can't Figure out Loop Array Issue.
    By CMDR Reservoir in forum New To Java
    Replies: 4
    Last Post: 07-19-2011, 02:30 AM
  2. Beginner question about ArrayList
    By kesi in forum New To Java
    Replies: 3
    Last Post: 09-19-2009, 11:30 PM
  3. Beginner Java question
    By DanK in forum New To Java
    Replies: 3
    Last Post: 04-27-2009, 04:29 AM
  4. Beginner's Problem on Loop/If statement
    By obdi in forum New To Java
    Replies: 2
    Last Post: 07-07-2008, 01:41 AM
  5. Replies: 13
    Last Post: 07-06-2008, 09:11 AM

Posting Permissions

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