Results 1 to 7 of 7
Thread: Beginner Loop Question
- 10-12-2011, 01:16 AM #1
Member
- Join Date
- Sep 2011
- Posts
- 54
- Rep Power
- 0
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.
- 10-12-2011, 01:43 AM #2
Senior Member
- Join Date
- Oct 2011
- Posts
- 106
- Rep Power
- 0
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.............
- 10-12-2011, 03:54 AM #3
Member
- Join Date
- Sep 2011
- Posts
- 54
- Rep Power
- 0
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
- 10-12-2011, 04:43 AM #4
Senior Member
- Join Date
- Jan 2011
- Location
- Rizal Province, Philippiines
- Posts
- 167
- Rep Power
- 0
Re: Beginner Loop Question
you did'nt use the s.LowerCase you just use the raw string
you use it in your print statement if you want to show s in lowercase: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); } } }
now it should print all lowercaseJava Code:String t = s.toLowerCase(); System.out.println(t);
- 10-12-2011, 04:50 AM #5
Member
- Join Date
- Sep 2011
- Posts
- 54
- Rep Power
- 0
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
- 10-12-2011, 05:27 AM #6
Senior Member
- Join Date
- Jan 2011
- Location
- Rizal Province, Philippiines
- Posts
- 167
- Rep Power
- 0
Re: Beginner Loop Question
did we help you? Please press the add the reputation
- 10-12-2011, 07:58 AM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
Re: Beginner Loop Question
Last edited by JosAH; 10-12-2011 at 11:04 AM.
When people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
Java Beginner: Can't Figure out Loop Array Issue.
By CMDR Reservoir in forum New To JavaReplies: 4Last Post: 07-19-2011, 02:30 AM -
Beginner question about ArrayList
By kesi in forum New To JavaReplies: 3Last Post: 09-19-2009, 11:30 PM -
Beginner Java question
By DanK in forum New To JavaReplies: 3Last Post: 04-27-2009, 04:29 AM -
Beginner's Problem on Loop/If statement
By obdi in forum New To JavaReplies: 2Last Post: 07-07-2008, 01:41 AM -
[SOLVED] Beginner needs help on Loop,Math-syntax on Graphics
By obdi in forum New To JavaReplies: 13Last Post: 07-06-2008, 09:11 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks