-3

I created a list from a text document. I then created a loop to go through it and count every time "strawberry" is in it. But I'm not sure why, it is not working.

I know my list gets created correctly because my print statement at the end returns the correct value. But then it doesn't add up the flavors or the number of times strawberry is present.

 import java.util.List;
 import java.util.ArrayList;

 public class IceCream {
    public static void main(String[] args) {

        int count, strawberry, average, i;

        List<String> flavors = new ArrayList<String>();
        TextIO.readFile("src/icecream.dat");

        while (TextIO.eof() == false) {
            flavors.add(TextIO.getln());
        }

        count = 0;
        strawberry = 0;

        for (i = 0; i < flavors.size(); i++); {
            count++;
            if ( flavors.equals("Strawberry")) {
                strawberry++;
            }
        }

        TextIO.putln(flavors.size());
        average = strawberry / count * 100; 

        TextIO.putln("" + count + " ice cream cones were sold. " 
                  + strawberry + " were strawberry flavored, " 
                  + "which is a total of " + average + "% of the ice cream cones sold.");
    }
}
RubioRic
  • 2,442
  • 4
  • 28
  • 35
Lili V.
  • 51
  • 1
  • 11
  • 6
    Should be `if ( flavors.get(i).equals("Strawberry")) {` – ernest_k May 02 '18 at 06:32
  • What Ernest said. Also be aware that .equals() is case sensitive. – Wep0n May 02 '18 at 06:32
  • @ErnestKiwele Do post as an answer, 10/10 would upvote. – Wep0n May 02 '18 at 06:33
  • If I change it, I get this error message: Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 374, Size: 374 at java.util.ArrayList.rangeCheck(ArrayList.java:657) at java.util.ArrayList.get(ArrayList.java:433) at IceCream.main(IceCream.java:19) – Lili V. May 02 '18 at 06:34

3 Answers3

4

You are wanting to use ArrayList::get and compare the Object using equals

if ( flavors.get(i).equals("Strawberry")) {
    strawberry++;
}

edit

Also remove the last ; on for (i = 0; i < flavors.size(); i++);

Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
  • When I do, I get this answer: Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 374, Size: 374 at java.util.ArrayList.rangeCheck(ArrayList.java:657) at java.util.ArrayList.get(ArrayList.java:433) at IceCream.main(IceCream.java:19) – Lili V. May 02 '18 at 06:36
  • `average` will be 0, because you're using integers. smaller integer devided by bigger integer gives 0. 0 * 100 gives 0. – Max Vollmer May 02 '18 at 06:45
  • 1
    You are going to have a `integer` division problem see http://stackoverflow.com/questions/4685450/why-is-the-result-of-1-3-0 – Scary Wombat May 02 '18 at 06:45
  • Yes, just realized that right now. Thank you so much! – Lili V. May 02 '18 at 06:47
3

Apart from Scary's answer, you have a semicolon after your for-loop. A semicolon is an empty statement, so you're running an empty statement 374 times, while increasing i to be the size of your list. Afterwards you run the block that was supposed to be the loop's body exactly once, but now i is too big for your list.

  1. Remove the semicolon.
  2. Do not declare your variables god knows where, declare them where you need them. That way you would've gotten a compile error, as i would only be available inside and not outside the loop.
Max Vollmer
  • 8,412
  • 9
  • 28
  • 43
1

You are comparing the ArrayList object with the String "Strawberry". What you really want is to compare each element with the String. So the statement should be

if(flavors.get(i).equals("Strawberry")) {
    strawberry++;
}
Ashwin K Kumar
  • 113
  • 1
  • 9