Results 1 to 13 of 13
  1. #1
    beandip408 is offline Member
    Join Date
    Sep 2010
    Posts
    10
    Rep Power
    0

    Default 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;
    }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,815
    Rep Power
    25

    Default

    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 05:02 AM.

  3. #3
    beandip408 is offline Member
    Join Date
    Sep 2010
    Posts
    10
    Rep Power
    0

    Default

    Java 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;
    		
    	
    	}
    	
    	}
    it wont compile because im missing the parts in between this and im stuck as to what to do next
    Last edited by Eranga; 09-29-2010 at 05:10 AM. Reason: code tags added

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    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.

    Java Code:
    str.substring(0, 1).equalsIgnoreCase("s");
    You had to have handle the index properly.

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,815
    Rep Power
    25

    Default

    it wont compile because
    Please copy full text of error message and paste it here. Here is a sample:
    Java Code:
    TestSorts.java:138: cannot find symbol
    symbol  : variable var
    location: class TestSorts
             var = 2;
             ^
    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.

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    @OP, please use code tags when you are posting code segments again. Unformated codes are not clear enough to raed easily.

  7. #7
    beandip408 is offline Member
    Join Date
    Sep 2010
    Posts
    10
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Please copy full text of error message and paste it here. Here is a sample:
    Java Code:
    TestSorts.java:138: cannot find symbol
    symbol  : variable var
    location: class TestSorts
             var = 2;
             ^
    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.
    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.

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    > 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].

  9. #9
    beandip408 is offline Member
    Join Date
    Sep 2010
    Posts
    10
    Rep Power
    0

    Default

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

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

    Default

    Quote Originally Posted by Eranga View Post
    Don't use == to compare characters
    Yes, do use == to compare characters; chars are a primitive type and the comparison does what you want it to do; there's no need for String fiddling.

    kind regards,

    Jos

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,182
    Rep Power
    20

    Default

    "wEWEWwewewetrtrtrTRTRTRTREEEEEeee"?

    That's pretty similar to the noise my car's currently making...

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

    Default

    Quote Originally Posted by Tolls View Post
    "wEWEWwewewetrtrtrTRTRTRTREEEEEeee"?

    That's pretty similar to the noise my car's currently making...
    "wEWEWwewewetrtrtrTRTRTRTREEEEEeee"? That sounds if you might've hit a toddler and it must've got stuck between the bottom of your car and the exhaust pipe; have you checked?

    kind regards,

    Jos (<--- mechanic ;-)
    Last edited by JosAH; 09-29-2010 at 02:50 PM.

  13. #13
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,815
    Rep Power
    25

    Default

    there has to be a shorter way to do this...
    Yes the is.
    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

  1. generate random letters inbetween a string
    By greg677 in forum New To Java
    Replies: 1
    Last Post: 05-04-2010, 06:06 AM
  2. increment letters from a string
    By iluvjava in forum New To Java
    Replies: 24
    Last Post: 03-22-2010, 06:18 PM
  3. What does the letters mean?
    By mustachMan in forum New To Java
    Replies: 3
    Last Post: 02-11-2010, 10:50 PM
  4. Need help with counting letters
    By mrdestroy in forum New To Java
    Replies: 15
    Last Post: 10-22-2008, 02:33 PM
  5. validating a string for numbers and letters?
    By lockmac in forum New To Java
    Replies: 1
    Last Post: 08-09-2007, 10:17 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
  •