Why does the code skip the for-loop

My task was to write a method that should combine letters that occur more than two times in a row. Example: ABBCCCDDDD → ABBC3D4

They told me the In.java and Out.java library for input and output. My problem is that the method skips the for loop. I hope you can help me. If you have any questions about the code, just ask me please.

public class Aufgabe9_4 {
    static void codieren(String s){
        int Zähler = 0;
        char[] a = s.toUpperCase().toCharArray();
        for (int i = 0; i<s.length()-1; i++){
            if (a[i] == a[i+1] && a[i] ==a[i+2]){
                Zähler = 3;
                while (a[i] == a[i+Zähler]) Zähler++;   
                i = i + Zähler; 
            }
            Out.println(a[i] + Zähler);
        }
    }
    public static void main(String args[]) {
        Out.println("Geben sie eine Reihenfolge von Buchstaben ein!");
        String s = In.readString();
        Out.println("Die Codierung lautet:");
        codieren(s);
    }
}
+4
source share
6 answers

I adjusted the code and got the expected result: ABBC3D4

  • . [i+2], , i < length-2. ( , , (i+2), < length.)

  • while , [i+Zähler] < length, Zähler . (i+Zähler) < s.length() . a[i] == a[i+Zähler], .

  • : i = i + Zähler. 1, - 1. , print(a[i]) , last Zähler++.

  • println(a[i] + Zähler) a[i], Zähler ( char - ). , , , .

  • if (Zähler != 0), .

  • print println .

  • -, : for, . a.length, s.length().

  • - : , String.charAt().

Ideone ().

static void codieren(String s){
    int Zähler = 0;
    char[] a = s.toUpperCase().toCharArray();
    for (int i = 0; i<s.length()-2; i++){
        if (a[i] == a[i+1] && a[i] ==a[i+2]){
            Zähler = 3;
            while ((i+Zähler) < s.length() && a[i] == a[i+Zähler]) {
                Zähler++;
            }
            i = i + Zähler - 1;
        }
        System.out.print(a[i]);
        if (Zähler != 0) {
            System.out.print(String.valueOf(Zähler));
        }
    }
}
+2

, .

, - , .

System.out.println :

65
66
66
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
    at Test.codieren(Test.java:8)
    at Test.main(Test.java:18)
71

ArrayIndexOutOfBoundsException , 0 -1, , , 10.

:

public class Test {
  static void codieren(String s) {
    int Zähler = 0;
    char[] a = s.toUpperCase().toCharArray();
    for (int i = 0; i <= s.length() - 1; i++) {
      if (a[i] == a[i + 1] && a[i] == a[i + 2]) {
        Zähler = 3;
        while (a[i] == a[i + Zähler])
          Zähler++;
        i = i + Zähler;
      }
      System.out.println(a[i] + Zähler);
    }
  }

  public static void main(String args[]) {
    String s = "abbcccdddd";
    codieren(s);
  }
}
+1

char , . :

  • [i] , .
  • Zaeler 1.

, :

static void codieren(String s){
    char[] a = s.toUpperCase().toCharArray();
    for (int i = 0; i<s.length()-1; i++){
        int Zähler = 1;        
        Out.print(a[i]);
        if (a[i] == a[i+1] && a[i] ==a[i+2]){
            Zähler = 3;
            while (a[i] == a[i+Zähler]) Zähler++;   
            i = i + Zähler; 
        }
        Out.println("(" + Zähler + ")");
    }
}
+1

,

private static int countOccur(char[] arr, int st, int end) {
  final char ch = arr[st]; // 'st' is start. 
  int count = 1;
  for (; count < end - st; count++) { // <-- don't exceed the array boundary
    if (ch != arr[st + count]) { // <-- break if not the same character
      break;
    }
  }
  return count; // <-- return the count
}

codieren(String), String ( void, - , ),

static String codieren(String s) {
  if (s == null) { // <-- handle corner cases.
    return null;
  }
  StringBuilder sb = new StringBuilder(); // <-- for building the return String.
  char[] a = s.toUpperCase().toCharArray();
  for (int i = 0; i < a.length;) { // <-- we will increment by occurrences.
    int occur = countOccur(a, i, a.length); // <-- get the occurrences count.
    if (occur > 2) { // <-- 3 or more are LETTER then COUNT.
      sb.append(a[i]).append(occur);
    } else { // <-- Must be 2 or fewer.
      for (int t = 0; t < occur; t++) {
        sb.append(a[i]);
      }
    }
    i += occur; // <-- increment 'i'.
  }
  return sb.toString(); // <-- return the result.
}
+1

, " ", ( ).

:

  • , "", , aviillable

This divides the work into two parts: "scanning for edges" and "presentation of the last sequence", which can be encoded in a modular way.

public static String getCollapsed(String inString) {
    char[] chars = inString.toUpperCase().toCharArray();
    StringBuilder sb = new StringBuilder(inString.length());
    char curChar = ' ';
    char lastChar = ' ';
    int lastCount = 0;
    for (int i = 0; i < chars.length; i++) {
        curChar = chars[i];
        if (i != 0) {
            if (curChar != lastChar) {
                sb.append(getRepresentation(lastChar, lastCount));
                lastCount = 0;
            }
        }
        lastChar = curChar;
        lastCount++;
    }
    sb.append(getRepresentation(lastChar, lastCount));
    return sb.toString();
}

private static String getRepresentation(char c, int count) {
    String s = "";
    if (count == 1) s = "" + c;
    else if (count == 2) s = "" + c + c;
    else s = "" + c + count;
    return s;
}

public static void main(String[] args) {
    System.out.println(getCollapsed("AAAABCCDDDD"));

}
+1
source

how to use two whiles

i = 0;
counter = 0;
currentchar=s[0]
stringbuffer outputstr;

while i < s.length
  {
   while s[i]==currentchar and i < s.length
     {
      counter +=1;
      i+=1;
     }

   if (counter== 1) 
      { outputstr.append(currentchar); }
   elseif (counter==2)
      { outputstr.append(currentchar);
        outputstr.append(currentchar);
      }
   else
      { outputstr.append(currentchar);
        outputstr.append(counter.toString);
      }

   currentchar = s[i];
   counter = 0;
  }
+1
source

Source: https://habr.com/ru/post/1546487/


All Articles