Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the code skip the for-loop

Tags:

java

for-loop

my task was to write a method that should combine letters which occur more then two times in a row. Example: ABBCCCDDDD -> ABBC3D4

I was told to us the library's In.java and Out.java for in 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);
    }
}
like image 817
user3788064 Avatar asked Mar 19 '23 04:03

user3788064


2 Answers

I have corrected the code and gotten your expected result: ABBC3D4

  • All array accesses must be within the array range. When you access [i+2] ensure your i value is always < length-2. (In other words, the index you're accessing (i+2) must always be < length.)

  • With the while loop, it's a little trickier as [i+Zähler] must also be < length but Zähler can be any value. For this I added (i+Zähler) < s.length() as a check. It must be before a[i] == a[i+Zähler] as it needs to be evaluated first.

  • Same goes for your original code: i = i + Zähler. You were overshooting by 1, so I had to add - 1. It ensured print(a[i]) will not fail, by negating the effect of the last Zähler++.

  • println(a[i] + Zähler) will print the numeric sum of a[i] added with Zähler (a char value is an integer at heart). Clearly this is not what you want, so I split it.

  • I also added the check if (Zähler != 0) as you didn't want zeros printed.

  • The print function, instead of println, prints on the same line.

  • Something I didn't correct in the code below: When you're accessing an array in a for loop, the loop parameters should better use the length of that same array. So by right, you should be using a.length, not s.length().

  • Something else: Try rewriting the program using String.charAt().

The working program is on Ideone here (link).

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));
        }
    }
}
like image 78
ADTC Avatar answered Apr 01 '23 19:04

ADTC


This is really a comment, but I need to include formatted output and code.

I think the problem may be a combination of a program failing due to an exception and an output method that is not showing the prior output on exception.

I used System.out.println to output in a program based on the question source code, and got:

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 means that you attempted to access an array using an index outside the range 0 through length-1, using an index that does not exist, in this case 10.

My modified program is:

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);
  }
}
like image 33
Patricia Shanahan Avatar answered Apr 01 '23 17:04

Patricia Shanahan