jeudi 12 décembre 2019

Java: How can I add numbers in an If Statement (explanation in the post)

I am doing some homework for university.

I have to iterate a string over an int array to decode matching signs.

I need your help with the following problem:

This is my Code right now:

public static void main(String[] args) {
            String s = "| || |  ||||  |||| || || | ||| || ||";

            int result = 0;
            int[] num = {0, 1, 2, 4, 7};
            String over = "";
            String[] parts = s.split("(?<=\\G.{6})");
            for(int i=0; i<parts.length; i++) {
              parts[i] = parts[i].substring(0, 5);
              over = parts[i];
              for(int j = 0; j < num.length; j++) {
                  if(over.charAt(j) == ' ') {
                      result = num[j];

                  }                   
              }
              System.out.println(result);
}



}

Output is: 711424 but I need it to be 811424.

for(int j = 0; j < num.length; j++) {
                  if(over.charAt(j) == ' ') {
                      result = num[j];

Here is my problem I think. My Variable "result" does not add the first result an the next one. I hope you see what I mean. I've tried various things, but cannot seem to get it done without screwing the rest of the numbers up.

Thank you

For clarification: This is the math behind the decoding:

Its in German, sorry for that

Aucun commentaire:

Enregistrer un commentaire