Reputation: 156
I am attempting to solve a problem where I create a method that counts the number of occurrences of capital and lowercase ("A" or "a") in a certain string. I have been working on this problem for a week now, and the main error that I am receiving is that "char cannot be dereferenced". Can anyone point me in the correct direction on this Java problem? Thank you.
class Main{
public static int countA (String s)
{
String s1 = "a";
String s2 = "A";
int count = 0;
for (int i = 0; i < s.length; i++){
String s3 = s.charAt(i);
if (s3.equals(s1) || s3.equals(s2)){
count += 1;
}
else{
System.out.print("");
}
}
}
//test case below (dont change):
public static void main(String[] args){
System.out.println(countA("aaA")); //3
System.out.println(countA("aaBBdf8k3AAadnklA")); //6
}
}
Upvotes: 6
Views: 2310
Reputation: 786001
Another way of counting characters using Java 8+ String#chars()
method that returns an IntSteam
:
String str = "aaBBdf8k3AAadnklA";
System.out.println(
str
.toLowerCase() // lowercase input String
.chars() // get IntStream of all characters
.filter(c -> c == 'a') // use filter to match only chosen characters
.count()); // get the count of filtered characters
//=> 6
Upvotes: 1
Reputation: 79550
You can use Matcher#results
to get a Stream<MatchResult>
and then use Stream#count
to get the desired result.
Demo:
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
String str = "aaBBdf8k3AAadnklA";
String pattern = "[Aa]";
System.out.println(
Pattern.compile(pattern)
.matcher(str)
.results()
.count());
}
}
Output:
6
[Aa]
is a character class regex pattern and means A
or a
. Learn more about it from Character Classes tutorial.
Upvotes: 2
Reputation: 2465
For counting the number of time 'a'
or 'A'
appears in a String:
public int numberOfA(String s) {
s = s.toLowerCase();
int sum = 0;
for(int i = 0; i < s.length(); i++){
if(s.charAt(i) == 'a')
sum++;
}
return sum;
}
Or just replace everything else and see how long your string is:
int numberOfA = string.replaceAll("[^aA]", "").length();
Upvotes: 4
Reputation: 44854
try a simpler solution
String in = "aaBBdf8k3AAadnklA";
String out = in.replace ("A", "").replace ("a", "");
int lenDiff = in.length () - out.length ();
Also as @chris mentions in his answer, the String could be converted to lowercase first and then only do a single check
Upvotes: 5
Reputation: 673
To find the number of times character a and A appear in string.
int numA = string.replaceAll("[^aA]","").length();
Upvotes: 1
Reputation: 56469
the main error that I am receiving is that "char cannot be dereferenced"
change this:
s.length // this syntax is incorrect
to this:
s.length() // this is how you invoke the length method on a string
also, change this:
String s3 = s.charAt(i); // you cannot assign a char type to string type
to this:
String s3 = Character.toString(s.charAt(i)); // convert the char to string
another solution to accomplishing your task in a simpler manner is by using the Stream#filter
method. Then convert each String
within the Stream
to lowercase prior to comparison, if any Strings match "a"
we keep it, if not we ignore it and at the end, we simply return the count.
public static int countA(String input)
{
return (int)Arrays.stream(input.split("")).filter(s -> s.toLowerCase().equals("a")).count();
}
Upvotes: 4