-6

This is in Eclipse. I wrote this code that is supposed to sum each String element within the ArrayList and then return the sum cubed. The problem is that it is returning 0.0 every time the method is run with strings added to it.

I don't understand why on line 6 the loop is not computing the length of each string element and storing the value to sum1.

public static double q1(ArrayList<String> input1) {

    ArrayList<String> array1 = new ArrayList<String>();
    double sum1 = 0;
    for (String s1: array1) {
         sum1 += s1.length(); //Line 6
    }
    return Math.pow(sum1, 3);
}
Andrew Li
  • 55,805
  • 14
  • 125
  • 143
Damo
  • 3
  • 4
  • 2
    `array1` is empty...I don't understand why you're using it – MadProgrammer Feb 18 '18 at 00:33
  • 1
    [What does your step debugger tell you?](http://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) I can tell you what it will tell you, you are using an empty arraylist and not the one passed in as an argument. –  Feb 18 '18 at 00:34
  • 1
    did you mean `for (String s1 : input1){ ...}`. – Ousmane D. Feb 18 '18 at 00:35
  • Did you mean to iterate through `input1` not create an empty ArrayList? – Andrew Li Feb 18 '18 at 00:35
  • remove `array1`, replace `array1` in loop `for` on `input1` – TigerTV.ru Feb 18 '18 at 00:38
  • 1
    Yes, apologies. I should have stated that this array list was supposed to not have anything added to it by me. A different program is adding strings to the array list but I see now that I am having issues with creating the correct code for allowing the program to add the strings. – Damo Feb 18 '18 at 00:45
  • Delete `array1` (that entire line), then rename `array1` to `input1` in your `for` loop. That should solve your problem, but make sure you go back through with a debugger to understand why. – Jules Dupont Feb 18 '18 at 00:49

2 Answers2

2

From the code provided and the info you gave us, this is what I think is happening. You are not populating the ArrayList array1 object with string data to loop through. The default constructor makes an empty list by default, you can test this in the debugger in eclipse by stopping and looking at

int test = array1.size(); 

this will return an int of the size of the array. All the in depth info about array list can be found here on the arrayList java doc. https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html

fix it try this:

 ArrayList<String> array1 = new ArrayList<String>();
 array1.add("put string 1 here");
 array1.add("put string2 here");

and so on then you can do this part:

    double sum1 = 0;
    for (String s1: array1){
         sum1 += s1.length(); //Line 6
    }
    return Math.pow(sum1, 3);
1

You created a new empty list and iterated by its elements (there are none). If this method must calculate what you want, use this:

public static double q1(ArrayList<String> input1){
    double sum1 = 0;
    for (String s1: input1){
         sum1 += s1.length(); //Line 6
    }
    return Math.pow(sum1, 3);
}
excelsiorious
  • 422
  • 9
  • 18