Results 1 to 13 of 13
Thread: counting letters in a string
- 09-29-2010, 03:48 AM #1
Member
- Join Date
- Sep 2010
- Posts
- 10
- Rep Power
- 0
counting letters in a string
im working on something that requires me to have a user input a string like:
wEWEWwewewetrtrtrTRTRTRTREEEEEeee
i need to return a statement stating how many w's, e's, t's, and r's and a percentage of how many times each letter is inputted.
i already have my method that scans in the information and i think i understand how to do the necessary loops, but the return is what i am stuck on.
and as far as the upper and lower case i think this works right?
public static boolean isLetter(char c) {
if (c == 'w' || c == 'W') {
return (true);
} else if (c == 'e' || c == 'E') {
return (true);
} else if (c == 't' || c == 'T') {
return (true);
} else if (c == 'r' || c == 'R') {
return (true);
}
return false;
}
- 09-29-2010, 03:59 AM #2
Looks like it could. Compile it and send it some test data to verify.
I'd change the name of the method to be more descriptive of the short list of characters it's testing for.
Most people would think isLetter('A') should return true.Last edited by Norm; 09-29-2010 at 04:02 AM.
- 09-29-2010, 04:04 AM #3
Member
- Join Date
- Sep 2010
- Posts
- 10
- Rep Power
- 0
it wont compile because im missing the parts in between this and im stuck as to what to do nextJava Code:import java.util.Scanner; public class calcString { public static void main(String[] args) { while (true) { Scanner input = new Scanner(System.in); System.out.println("Please input a string (type \"stop\" to end): "); String input= input.nextLine(); if (input.equals ("stop")) break; } } public static boolean isLetter(char c) { if (c == 'w' || c == 'W') { return (true); } else if (c == 'e' || c == 'E') { return (true); } else if (c == 't' || c == 'T') { return (true); } else if (c == 'r' || c == 'R') { return (true); } return false; } }Last edited by Eranga; 09-29-2010 at 04:10 AM. Reason: code tags added
- 09-29-2010, 04:08 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Don't use == to compare characters, it not complete compared two. You may confused with that, so read more about string comparisons.
Regarding your question. Best way to do is sub string one character at a time and compare it without considering the case.
You had to have handle the index properly.Java Code:str.substring(0, 1).equalsIgnoreCase("s");
- 09-29-2010, 04:10 AM #5
Please copy full text of error message and paste it here. Here is a sample:it wont compile because
To test your method, forget about getting input from a user for now. Hard code the values in a short test program that calls the method several times with different valid and invalid letters and prints out the results. You can then look at the output and visually verify that the method is working properly.Java Code:TestSorts.java:138: cannot find symbol symbol : variable var location: class TestSorts var = 2; ^
- 09-29-2010, 04:11 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
@OP, please use code tags when you are posting code segments again. Unformated codes are not clear enough to raed easily.
- 09-29-2010, 04:47 AM #7
Member
- Join Date
- Sep 2010
- Posts
- 10
- Rep Power
- 0
as the way it is, it prints:
Please input a string (type "stop" to end):
wEWEWwewewetrtrtrTRTRTRTREEEEEeee
Please input a string (type "stop" to end):
wEWEWwewewetrtrtrTRTRTRTREEEEEeee
Please input a string (type "stop" to end): stop
so i need to include other nested loops that will pick apart the string. as far are ignoring the input, thats fine but the part that i am struggling with is preventing it from picking apart any string whether it be hard coded or scanned.
i just dont know how to count letters and calculate percentages.
- 09-29-2010, 05:36 AM #8
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
> so i need to include other nested loops that will pick apart the string.
It's far from clear that a bunch of nested loops are needed to pick apart the string. Or a huge chain of (if-elseif)^n for that matter.
Given a string - written on a piece of paper, say - the common sense way of finding letter frequencies would be to work along the string and, one letter at a time, make a tally mark to record the presence of the letter.
String methods exist (to find the length of a string, and the character at each position) to allow the string to be "picked apart" with a single for loop. As for the tallies you might want to consider a simple array of ints where the count for some character ch is stored at location counter[ch].
- 09-29-2010, 06:11 AM #9
Member
- Join Date
- Sep 2010
- Posts
- 10
- Rep Power
- 0
okay i think im almost done but i have (i hope) 1 more question. im trying to exclude all other letters besides the 4 (W,E,T,R)
in other words, if someone inputs: B i will print out a statement saying "The letter B is not a valid part of this string"
now i know i can make 22 else if statements
else if (str.substring(i, i + 1).equalsIgnoreCase("b")){
System.out.println("The letter B is not a valid part of this string.");
there has to be a shorter way to do this...
- 09-29-2010, 09:04 AM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,372
- Blog Entries
- 7
- Rep Power
- 17
- 09-29-2010, 09:38 AM #11
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
"wEWEWwewewetrtrtrTRTRTRTREEEEEeee"?
That's pretty similar to the noise my car's currently making...
- 09-29-2010, 12:27 PM #12
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,372
- Blog Entries
- 7
- Rep Power
- 17
- 09-29-2010, 01:44 PM #13
Yes the is.there has to be a shorter way to do this...
In your loop get the letters one at a time from the String. Several ways to do this. For example the charAt() method. Use your method to test the char. if it char fails, build a string with that char in it to print out your error message.
Do NOT make 22 else if statements
Similar Threads
-
generate random letters inbetween a string
By greg677 in forum New To JavaReplies: 1Last Post: 05-04-2010, 05:06 AM -
increment letters from a string
By iluvjava in forum New To JavaReplies: 24Last Post: 03-22-2010, 05:18 PM -
What does the letters mean?
By mustachMan in forum New To JavaReplies: 3Last Post: 02-11-2010, 09:50 PM -
Need help with counting letters
By mrdestroy in forum New To JavaReplies: 15Last Post: 10-22-2008, 01:33 PM -
validating a string for numbers and letters?
By lockmac in forum New To JavaReplies: 1Last Post: 08-09-2007, 09:17 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks