#My ArrayList method returns 0 instead of 6

1 messages · Page 1 of 1 (latest)

earnest garden
#

This is my code, and hopefully the small comments I've left would help describe my thought process for each step. I am unsure why this method returns only 0s @_@

    public static int maxLength(ArrayList<String> list) {
        int counter = 0; //Initialize the counter.
        int tracker = 0; //Saves the highest int count.

        for(int i = 0; i<list.size(); i++){ //Runs through the ArrayList
            String n = list.get(i); //Gets the value of the index
            
            for(int j = 0; j<n.length();j++){ //Counts the amount of characters.
                if (n.charAt(j)!=' '){
                    counter++;
                }
            }

            if (counter > tracker){ //Replaces the value of tracker if counter is higher
                counter = tracker;
            }
            counter = 0;
        }
        return tracker;
    }
wide solsticeBOT
#

<@&987246399047479336> please have a look, thanks.

wide solsticeBOT
# earnest garden This is my code, and hopefully the small comments I've left would help describe ...

Detected code, here are some useful tools:

Formatted code
public static int maxLength(ArrayList<String> list) {
  int counter = 0;
  //Initialize the counter.
  int tracker = 0;
  //Saves the highest int count.
  for (int i = 0; i < list.size(); i++) {
    //Runs through the ArrayList
    String n = list.get(i);
    //Gets the value of the index
    for (int j = 0; j < n.length(); j++) {
      //Counts the amount of characters.
      if (n.charAt(j) != '') {
        counter++;
      }
    }
    if (counter > tracker) {
      //Replaces the value of tracker if counter is higher
      counter = tracker;
    }
    counter = 0;
  }
  return tracker;
}
olive gust
#

not tracker = counter

#

so u replaced ur counter which was for example 1

#

with tracker which was 0

#

that way u constantly reset ur counter back to 0

#

and tracker never changes

earnest garden
#

Many thanks, Zabuzard u.u

#

How do I close this thread?