How can I count the number of times
a character repeated in the String (ignore case).:confused::confused:
Printable View
How can I count the number of times
a character repeated in the String (ignore case).:confused::confused:
Have you even bothered reading about the String class in the API? Read first, then code, then ask specific questions about your code.
Hey, pls don't be so harsh on the new guy. might be he missed out the research part.
@mrdestroy: You actually do not need to know the String class to write a similar logic urself. basic procedural programming ll do. but as ojn said about the String class, yes.. there is a related method.
Do you have a single letter to look for or are you making a count for all the letters in the String?
There are several ways to get to the individual characters/letters in a String. Look at the API doc for the string class. substring, indexOf and charAt for example.
I mean the number of 1 letter repeated in a String.
Ex: javaforum
There are 2 letter a repeated.
I'm reading the API, but pls give some ideas if you have.
Do you want a count for all the letters or for just a single letter that you have chosen?
Which method do you think you want to use? Can you code a couple of lines of code to show us how you plan to use it?
public class LetterCounting {
/**This is the problem.
* Write a program to ask the user to enter a String which consists of only alphabet
letters a-z or A Z and a character. Then your program will count the number of times
this character repeated in the String (ignore case). ‘A’ is considered different with ‘a’.
Note: You no need to validate the user’s input for this exercise.
*/
public static void main(String[] args) {
String input, check;
int l, times = 0;
input = JOptionPane.showInputDialog("Please enter a string: ");
l = input.length();
check = JOptionPane.showInputDialog("Please enter a letter to check: ");
for (int i = 0; i < l ; i++){
JOptionPane.showMessageDialog(null, "Letter " + check + " repeats " + times + " in string " + input );
//times here is the number of repetition of a letter. How can I count this?
}
}
}
//this is a few codes I just plan to do. But I'm on stuck. How can I do it in while loop?
Part of the instructions seem contradicting:
Either it's case sensitive or 'A' is considered that same as 'a'Quote:
... (ignore case). ‘A’ is considered different with ‘a’.
Anway.. This is what I would do:
- Read/get entire text
- In the loop compare each text character to the letter you are looking for (hint: do not use ==)
- When a the same letter is found, increment a letter counter
- When finished looping through the text, print the letter counter variable
CJSL
Of... use the split function to split on that letter and use the length of the array it returns -1. ;)
Forget about all the I/O stuff. Write a simple program that has a string defined in it, like: String data = "Scan this for an a":
and some code to scan that String for the character and count its occurances.
Then merge it in with the I/O code you've shown.
@Supamagier:
Let the OP learn to program first, then show him the shortcuts.
He must learn loops.
public static void main(String[] args) {
String input;
int l, times = 0;
char c1, c2;
input = JOptionPane.showInputDialog("Please enter a string: ");
l = input.length();
c1 = JOptionPane.showInputDialog("Please enter a letter to check: ").charAt(0);
for (int i = 0; (i <= l - 1) ; i++){
c2 = input.charAt(i);
if (c1 == c2)
times++;
}
JOptionPane.showMessageDialog(null, "Letter " + c1 + " repeats " + times + " time(s) in string " + input );
}
}
Is there any ways using while or do while pls?
What you mean using do-while loop? Looking to write the same code using do-while.
Yes, the same code but using do-while or while.
Do this in two or more steps to be able to test what the user entered before assuming it is valid and assigning it to c1.Quote:
c1 = JOptionPane.showInputDialog("Please enter a letter to check: ").charAt(0);
The for loop is more natural when doing a scan. It sets the index, tests for the end and increments it all in one spot. You can do the incrementing of the index manually in a while loop and use the same end of loop condition test as in the for loop.
int count=0;
int i;
for (i = 0; i < input.length(); i++)
{
if (input.charAt(i)== c1)
{
count++;
}
}
//For loop is the way to go for counting the letters. I think this is how I did it, correct me if I'm wrong :)
You should know the way you used. What happen when you run the application with this code segment.