-1

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;
    }
    }
  • 2
    You're reading 2 lines and ignoring the second. I'm not sure what you expected. – shmosel Feb 26 '19 at 23:41
  • any suggestions? i made my code store the second line but now i get an Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0 – error –  Feb 27 '19 at 00:24
  • https://stackoverflow.com/q/13102045/1553851 – shmosel Feb 27 '19 at 00:24

1 Answers1

0

Here's what your main method is doing, line-by-line:

String s = in.next();

Read the first line in and store it in s

in.nextLine();

Read the second line in and don't store it anywhere

String compressString = compress(s);

Compress the first line

System.out.println(compressString);

Print out the compressed first line

}

End without ever having read in the third line.

Hopefully that's enough to point you in the right direction.

Jordan
  • 2,168
  • 7
  • 15
  • hello, so i stored the second and third line and printed them...but now i get an error. Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0 –  Feb 26 '19 at 23:59
  • Please update the code in your post with any changes that you made. – Jordan Feb 27 '19 at 00:39