Reputation: 1081
I want to print only 3 same consecutive characters from the given String as below if input is:
"aasssfddddvvv"
then I should get output as output:
sss
vvv
count=2
This is my requirement, please help me in this.
my code:
import java.util.Scanner;
class Test {
public static void main(String args[]) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter the String: ");
char s[] = scan.nextLine().toCharArray();
for (int i = 0; i < s.length - 1; i++) {
if (s[i] == s[i + 1] && s[i] == s[i + 2] && s[i] == s[i + 3]) {
System.out.println(s[i]);
}
}
scan.close();
}
}
Upvotes: 2
Views: 2244
Reputation: 578
It seems you want only a strict count of 3 consecutive character. If that's the case, even after fixing the issues in your code (without changing the overall logic), it will print the ddd
twice. Here is the output of the fixed code:
sss
ddd
ddd
vvv
count=4
What I meant by the fixed code is in handling these issues
&& s[i] == s[i + 3]
.i < s.length - 2
Here is the fixed code (remember this is still not producing the right output you want)
int occurence_count = 0;
for (int i = 0; i < s.length - 2; i++) {
if (s[i] == s[i + 1] && s[i] == s[i + 2]) {
occurence_count++;
System.out.println(s[i]);
}
}
System.out.println("count=" + occurence_count);
One logic change is to have a separate counter that counts the consecutive characters and a variable that remembers what is the last character.
int char_count = 0;
char last_char = '!';
The basic idea is to go through our string and increment the char_count
by one if it is the same and reset if it is different. We also need to initialize it with a character that you're not planing to use as input, such as !
.
Since we're going through the entire string, the for loop condition needs to go to the very end using i < s.length
.
When we're resetting (when a different character is detected), we want to also check whether the previous one has char_count=3
or not. If it is a match, then increment the occurence_count
. We also need to do this one more time after the for loop because the check might not happens for consecutive that happens at the end of the input string. Here is the code:
int occurence_count = 0;
int char_count = 0;
char last_char = '!';
for (int i = 0; i < s.length; i++) {
if (s[i] != last_char) {
if (char_count == 3) {
occurence_count++;
System.out.println(""+last_char+last_char+last_char);
}
last_char = s[i];
char_count = 0;
}
char_count++;
}
if (char_count == 3) {
occurence_count++;
System.out.println(""+last_char+last_char+last_char);
}
System.out.println("count=" + occurence_count);
Upvotes: 0
Reputation: 1625
You cant check.cells i + 2 and i + 3 because you will cross the array bounds when you will reach i = length -2, You should count the sequence and check if the length is sufficient, if so print, also, in your example you are comparing 4 cells so the length will be 4 and not 3, start the seq in 1 because when you are reading a single char its a sequence of 1:
import java.util.Scanner;
class Test {
public static void main(String args[]) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter the String: ");
char s[] = scan.nextLine().toCharArray();
int seqLen = 3;
int cnt = 1;
for (int i = 1; i < s.length; i++) {
if (s[i] == s[i - 1]) {
cnt++;
}
if((cnt==seqLen)&&((s[i] !=s[i - 1] || i == s.length)){
if(cnt == seqLen)
{
for(int j= 0; j<seqLen; j++)
{
System.out.println(s[i - 1]);
}
}
cnt=1;
}
}
scan.close();
}
}
Upvotes: 0
Reputation: 522331
This answer is a departure from the more manual method being used in the OP. But, this is fairly straightforward to handle uses a regex pattern matcher in Java. We can match on the pattern (.)\\2*
, to capture all groups of similar letters. Then, just print out the groups which occur as 3 only.
String line = "aasssfddddvvv";
String pattern = "((.)\\2*)";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(line);
while (m.find()) {
if (m.group(1).length() == 3) {
System.out.println(m.group(1));
}
}
sss
vvv
Upvotes: 3
Reputation: 4917
import java.util.Scanner;
class Test {
public static void main(String args[]) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter the String: ");
char s[] = scan.nextLine().toCharArray();
int count=0;
for (int i = 0; i < s.length - 3; i++) {
if (s[i] == s[i + 1] && s[i] == s[i + 2] && s[i]!=s[i + 3]) {
System.out.println(s[i]+s[i]+s[i]);
count++;
}
}
if(s[s.length - 3] == s[s.length - 2] && s[s.length - 3] == s[s.length - 1]){
System.out.println(s[s.length]+s[s.length]+s[s.length]);
count++;
}
scan.close();
System.out.println(count);
}
}
Upvotes: 0
Reputation: 599
Replace the main logic of your code with this. Here we are checking if there are only 3 repeating characters. by making sure that the fourth character is different.
int count=0;
for (int i = 0; i < s.length - 3; i++) {
if (s[i] == s[i + 1] && s[i] == s[i + 2] && s[i] != s[i + 3]) {
System.out.println(s[i]+s[i]+s[i]);
count++;
}
}
System.out.println("count="+count);
Upvotes: 0
Reputation: 8806
First Iterate through your char[]
array. Then, in your if
statement,
s[i]
with the next char(s[i+1]
) and the char after next(s[i+2]
)Replace this part of your code,
for (int i = 0; i < s.length; i++) {
if (i < s.length - 2 && s[i] == s[i + 1] && s[i] == s[i + 2]) {
System.out.println(s[i]);
}
}
Upvotes: 0
Reputation: 109
First problem is that, you try to reach lenght+2th index of array in for which is not possible.
Assume String lenght is 5
0: 0 1 2
1: 1 2 3
2: 2 3 4
3: 3 4 (5)
4: 4 **(5) (6)**
As you can see, you try to reach 5 which is not part of string. So try
for (int i = 0; i < s.length - 2; i++)
instead of
for (int i = 0; i < s.length - 1; i++)
Tip: You can get this from error.
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 13
It says you try to reach index which you can't.
Second problem you check 4 char in if condition. Use:
if (s[i] == s[i + 1] && s[i] == s[i + 2])
instead of
if (s[i] == s[i + 1] && s[i] == s[i + 2] && s[i] == s[i + 3])
Upvotes: 2