0

I am trying to make a method that gets the int value assigned to characters in a String, adds them up and returns a total.

It seems like the nested loops are not working properly, for some reason.

public static int tap_counter(String type) {

    String[] type_array = type.split("");

    String[] example_letters = new String[] {"A","B"}; 

    int total = 0;

    for(int i=0; i < type_array.length;i++){  
        for(int j=0; type_array[i] != standard_letters[j]; j++) 
        {
          if(type_array[i] == "A") total += 1;
          else if(type_array[i] == "B") total += 2;
        }
     }
    return total;
}

It works for a single-character String, but I get an OutOfBounds exception for multi-element Strings.

So, for instance, for the input "AB":

  • expected output is 3;

  • real output: java.lang.ArrayIndexOutOfBoundsException: Index 2 out of bounds for length 2.

azro
  • 35,213
  • 7
  • 25
  • 55
GCarletto
  • 1
  • 2
  • Please don't say the loop don't work properly, they are used by millions of people ... – azro Oct 31 '19 at 09:18
  • 1
    Welcome to Stack Overflow. What is `standard_letters` here? What is your input? If you could provide a [mcve], along with the steps you've taken to diagnose the problem, that would really help. Additionally, you should read https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java (I'd also advise you to start following Java naming conventions, using `camelCase` instead of `snake_case`. But that's a separate matter.) – Jon Skeet Oct 31 '19 at 09:19
  • 1
    Your inner loop only increases **j**, but never checks if j goes beyond the end of the array. – GhostCat Oct 31 '19 at 09:19
  • Hint: how do you expect the inner loop to exit, and what do you think will happen if `type_array` contains a string that isn't in `standard_letters`? – Jon Skeet Oct 31 '19 at 09:19

1 Answers1

0

Hope this helps:

public static int tap_counter(String type) {

String[] type_array = type.split("");

String[] example_letters = new String[] {"A","B"}; 

int total = 0;

for(int i=0; i < type_array.length;i++){  
    for(int j=0; j < example_letters.length; j++) 
    {
        System.out.println("Trying to match " + type_array[i] + " and " + example_letters[j]);
        if(type_array[i] == example_letters[j] {
              total += (j+1); //j index is one less than the value you wish to assign
              System.out.println("Match found. New total is:" + total)
              break; //exit innermost for loop
        }
    }
 }
return total;

}

Berge
  • 1
  • 3
  • I've now corrected that, but now (using the exact same code gently provided by Berge), rather than OutOfBounds, I get output: 0 for input: "AB", instead of: '3'... Any extra help would be very much appreciated. – GCarletto Oct 31 '19 at 12:47
  • Often a good idea to temporarily add some console logging to help with the debugging.(see the updated code). Should give you a better idea what happens when you run the code. – Berge Nov 01 '19 at 09:53