so i'm having a bit of a problem with my code. suppose the output is:
aaaaabcc1111111111
xyz
abbbc
my code will only read the first line "aaaaabcc1111111111" and not the rest. i've been try to mess around with the for loops but i can't seem to put my finger on what to do. any help would be much appreciated!
public class Compress {
public static void main(String[] args) {
java.util.Scanner in = new java.util.Scanner(System.in);
String s = in.next();
in.nextLine();
String compressString = compress(s);
System.out.println(compressString);
}
public static String compress (String text){
String string = "";
int count = 0;
char currentChar = text.charAt(0);
for(int i=0; i<text.length(); i++){
if (currentChar == text.charAt(i)) {
currentChar = text.charAt(i);
count++;
} else {
if (count >= 3) {
if (Character.isDigit(currentChar)) {
//if character is a digit, print #c#n
string += "#" + count + "#" + currentChar;
} else {
//else it is then a letter, so print #cn
string += "#" + count + currentChar;
}
} else if (count == 2) {
string += currentChar;
string += currentChar;
} else if (count == 1) {
string += currentChar;
}
currentChar = text.charAt(i);
count = 1;
}
}
//if count is greater than 3
if (count >= 3) {
//if character is a digit, print #c#n
if (Character.isDigit(currentChar)) {
string += "#" + count + "#" + currentChar;
} else {
//else it IS then a letter, so print #cn
string += "#" + count + currentChar;
}
} else if (count == 2) {
string = string + currentChar;
string = string + currentChar;
} else if (count == 1) {
string += currentChar;
}
return string;
}
}