2021年1月21日星期四

How can you check to see if the first two letters in the ArrayList is equal to something and then increase the sum in java?

I made a program to do the above, but it keeps printing the sum as 0. The goal is if the string's index (0,2) is "ab" then the sum is increased by 1. So far this is what I have but it prints 0. Should my if statement be written differently to fix it?

class Main {  public static void main(String[] args) {        /* declare list */            System.out.println(checkAB(list));  }    public static int checkAB(ArrayList<String> list) {      int sum  = 0;      for(int i = 0; i < list.size(); i++) {          if(list.subList(0,2).equals("ab")) {              sum++          }      }      return sum;  }  }  
https://stackoverflow.com/questions/65836850/how-can-you-check-to-see-if-the-first-two-letters-in-the-arraylist-is-equal-to-s January 22, 2021 at 06:42AM

没有评论:

发表评论