Reputation: 301
I'm a new java programmer. I'm trying to do a search from a user inputed string to find how many occurrences of a particular character there are.
It doesn't quite do what I want it to do as it seems to be displaying the number of characters from the point of the first occurrence of the character. For this project I need to use .indexOf()
This is my code.
import java.util.Scanner;
public class SearchingStrings {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Please enter the string to test > ");
String stringToTest = input.nextLine();
System.out.println("Please enter the letter to count >");
String letterToCount = input.nextLine();
// first position found
int positionOfLetter = stringToTest.indexOf(letterToCount);;
if (positionOfLetter != -1) {
int countNumberOfLetters = 1;
for (int i = positionOfLetter + 1; i < stringToTest.length() - 1; i++) {
positionOfLetter = stringToTest.substring(positionOfLetter, stringToTest.length()).indexOf(letterToCount);
if (positionOfLetter != -1) {
countNumberOfLetters++;
}
}
System.out.println("Number of letters found: " + countNumberOfLetters);
} else {
System.out.println("Your letter " + letterToCount + " was not found in the string!");
}
}
}
Upvotes: 2
Views: 5398
Reputation: 892
I always liked using split.
longString.split(subStringToCount).length - 1
So in the case of
String longString = "test string with spaces";
String subStringToCount = " ";
return longString.split(subStringToCount).length - 1;
You'll get 3.
Upvotes: 0
Reputation: 1
You can simply do this trick:
int count = stringToTest.replaceAll(letterToCount, "##").length() - stringToTest.length();
Upvotes: 0
Reputation: 140328
A cheeky way to count the number of occurrences is to remove all of the occurrences, and compare the length of the resulting string to the original:
String removed = stringToTest.replace(letterToCount, "");
int numOccurrences =
(stringToTest.length() - removed.length()) / letterToCount.length();
Upvotes: 1
Reputation: 22224
Using substring
to create a new String to search into is quite confusing here apart from inefficient. indexOf
has a version that accepts an index to start searching from. That's more efficient as it doesn't make copies of part of the string and simplifies your code quite a lot. E.g.
int positionOfLetter = stringToTest.indexOf(letterToCount);
int countNumberOfLetters = 0;
while (positionOfLetter != -1) {
countNumberOfLetters++;
positionOfLetter = stringToTest.indexOf(letterToCount, positionOfLetter + 1);
}
System.out.println("Number of letters found: " + countNumberOfLetters);
The second parameter of indexOf
is the start index. Note the +1
otherwise it will find the same char again and never break out of the loop.
Upvotes: 3
Reputation: 11
Seems like a homework question since you wrote you need to use indexOf but you don't. If your purpose is to count a specific letter you can do the following.
int counter=0;
for(int i=0; i<stringToTest.length();i++){
if(stringToTest.charAt(i).equals(letterToCount)){
System.out.println(letterToCount+" appears in "+ i +");
counter++;
}
}
System.out.println(letterToCount+" appeared "+counter+" times");
This code simply moves through each character of the stringToTest and checks if it is equal to the letter you want. indexOf() is unnecessary
Upvotes: 0
Reputation: 4555
Matcher m = Pattern.compile(letterToCount).matcher(input);
int count = 0;
while (m.find()) {
count++;
}
should also do the trick.
Upvotes: 1
Reputation: 30819
You are almost there, instead of for
loop, you can use do..while
loop to achieve this, e.g.:
String s = "test test1 test2";
String letter = "t";
int index = -1, count = 0;
do{
index = s.indexOf(letter);
if(index != -1){
count++;
s = s.substring(index + 1);
}
}while(index != -1);
System.out.println(count);
Upvotes: 3