0

Please help with the java code below.
When I give input, for example, aabbcccd, the output is 99100102d, but it should be a2b2c3d. Can anyone tell what's my mistake in this code? (This code tries to capture input and output how often a specific char has been typed)

import java.util.*;

public class Main {

    public static void main(String args[]) {
        try {
            Scanner scn = new Scanner(System.in);
            String s = scn.nextLine();                     // taking input
            StringBuilder str = new StringBuilder(s);              
            StringBuilder str_new = new StringBuilder();

            int i = 0 ;
            while (i < str.length()) {
                int count = 1; 
                while (i < str.length()-1 && str.charAt(i) == str.charAt(i+1)){
                    count += 1;
                    i++;
                }
                if (count == 1)
                    str_new.append(str.charAt(i));
                else
                    str_new.append(str.charAt(i) + (char)count);
                i++;
            }
            System.out.println(str_new);
        } catch (Exception e) {
            return;
        }
    }
}
1
  • Hi, as you have now answers now, you may think about accepting an answer to reward the one that gives you the most helpful comment. Commented Feb 13, 2021 at 9:48

2 Answers 2

2

The problem comes from str.charAt(i) + (char)count, as they are 2 chars, they are summed up with their int value,


Solve that by using consecutive append() calls

str_new.append(str.charAt(i)).append(count);

You can reduce the code by using an outer for-loop and a ternary operator in the append, and increment only i in the inner while by saving i before

int count;
for (int i = 0; i < str.length(); i++) {
    count = i;
    while (i < str.length() - 1 && str.charAt(i) == str.charAt(i + 1)) {
        i++;
    }
    str_new.append(str.charAt(i)).append((i - count) == 0 ? "" : (i - count + 1));
}
Sign up to request clarification or add additional context in comments.

Comments

0

Your primary issue was the used of the StringBuilder and entering the values which I show in this example. But in this case I am using regular expressions.

  • (.) is a capture block that matches on any character
  • \\1* refers to the first capture block followed by 0 or more of the same character.

The following code constructs the Matcher for the entered text and then continues to find subsequent matches. They could be printed out as found or placed in a StringBuilder as I chose to do.

Scanner scn = new Scanner(System.in);
String text = scn.nextLine();

Matcher m = Pattern.compile("(.)\\1*").matcher(text);

StringBuilder sb = new StringBuilder();
while (m.find()) {
    String s = m.group();
    int count = s.length();
    sb.append(s.charAt(0)).append(count > 1 ? count : "");
}

System.out.println(sb.toString());

for aaabbbbcadb Prints

a3b4cadb

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.