I am always surprised at the added knowledge i can get from others when i give my opinion on a topic.
I think we may be at too early a stage even to suggest things like Scanner.Paul Ngom wrote: . . . You could use the Scanner class for that.
Deron Brown wrote:For the program I know how to have the user type in a word but the problem is how I would count the occurrences of each letter in the word and display it.
I am always surprised at the added knowledge i can get from others when i give my opinion on a topic.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Deron Brown wrote:char a = Input.getChar("Please type in a word");
Joanne
Paul Ngom wrote:use .split method on the string so as
to save it in an array. You could then scan
the array
Paul Ngom wrote:You can
save the alphabet letters in a two dimensional
array with a column for the letters and
another for the counts.
Joanne
Or use a single dimension int array and calculate the index of the element to increment using the character value.
Letter A count = xx
Letter B count = xx
....
Letter Z count = xx
I am always surprised at the added knowledge i can get from others when i give my opinion on a topic.
Paul Ngom wrote:
Or use a single dimension int array and calculate the index of the element to increment using the character value.
The letters run from A to Z.
Joanne
That's why I said calculate the index.
'A' - 'A' = 0
'B' - 'A' = 1
'C' - 'A' = 2
'D' - 'A' = 3
'E' - 'A' = 4
etc
I am always surprised at the added knowledge i can get from others when i give my opinion on a topic.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Rico Felix wrote:What if I told you that you only need one if statement to solve your problem by using the Character class...
You got an array of 26 int slots to keep a running count of letter occurrences...
You loop through the input string and test each character checking if its a letter... if(here is the one if) its a letter increment the correct slot in the array
Print the information after the loop statement
Rico Felix wrote:Its like putting pieces of puzzles together... The String class has a method charAt(int) that return a char... The Character class has a method isLetter(char) that return boolean...
Put these to classes to collaborate and you get:
This is done to eliminate a slew of if-else branches and to make sure its a letter and not a digit
Rico Felix wrote:If you take into consideration that a character (char) is really an integer encoded within a character set to represent characters then it will become clear... take for instance the character 'a' has the integer value of 97...
With this information it can be seen that if you perform the expression 'a' - 'a' you will get 0 since it will be promoted to type int in the expression... therefore array['a' - 'a']++ == array[0]++
Rico Felix wrote:Hey pal, I'm back at ya... I see that you have done a great deal of figuring out the technique for reducing the slew of if-else constructs
However your code will never compile if you use an identifier that was not declared in your code so, if (Character.isLetter(string.charAt(k)) will not work because string is the culprit... its suppose to be str
Another mistake that you are making is differentiating between character and string literals... a character is enclosed in single quotes 'a' while a string is within double quotes "a", they a not the same... you cannot minus a string from a string...
To clear up now... the idea is that a character when used in an expression, is promoted to type int so you can perform arithmetic on them... since there are 26 letters in our alphabet the logic is simple which is to transform the character to either capital or common case and minus to end up in the correct index within the array to increment...
If we know that 'a' = 97 then if the character was 'B' which is equal to 66, transform it to 'b' which will be equal to 98 and subtract it from 'a' to end up in index 1 and so forth for each letter found...
Or you can go the other was around knowing that 'A' = 65 then if the character was 'c' which is equal to 99 transform it to 'C' which will be equal to 67 and subtract it from 'A' to end up in index 3 since (67 - 65 = 3) == ('C' - 'A' = 3)
Rico Felix wrote:One important skill you must also acquire while programming is understanding the compilers error messages... what it says is that there are unmatched parentheses in your code... you need to add another ) to close the if statement condition
Rico Felix wrote:I've deliberately left you to run it to see the error message... what you are seeing is array access error... the expression that you have placed for the subscript is working out to a negative number since 'a' = 67 and 'k' = 107 if you do the maths that is equal to -10 which is an unacceptable subscript for an array
No matter how many women are assigned to the project, a pregnancy takes nine months. Much longer than this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
|