1

So I'm trying to build a program to take a list of integers (specifically 400) that represents a 20x20 matrix and find the greatest product of four vertically consecutive integers in this list. In this situation, the numbers at the indexes 0, 20, 40, & 60 would be vertically consecutive numbers. For some reason, the java console is spitting out the following error:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 418 out of bounds for length 400 at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64) at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70) at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248) at java.base/java.util.Objects.checkIndex(Objects.java:372) at java.base/java.util.ArrayList.get(ArrayList.java:458) at Main.getDigitsVertical(Main.java:101) at Main.productListVertical(Main.java:59) at Main.main(Main.java:10) exit status 1

Here is my code:

import java.math.BigInteger;
import java.io.*; 
import java.util.*; 

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

    String data = "08022297381500400075040507785212507791084949994017811857608717409843694804566200814931735579142993714067538830034913366552709523046011426924685601325671370236912231167151676389419236542240402866331380244732609903450244753353783684203517125032988128642367102638406759547066183864706726206802621220956394396308409166499421245558056673992697177878968314883489637221362309750076442045351400613397343133957817532822753167159403800462161409535692163905429635314755588824001754243629855786560048357189070544443744602158515417581980816805944769287392138652177704895540045208839735991607975732162626793327986688366887576220720346336746551232639353690442167338253911249472180846293240627636206936417230238834629969826759857404361620733529783190017431497148868116235705540170547183515469169233486143520189196748";  
    //System.out.println(greatestProduct(productList(parseListOfStrings(chopString(data)), 0, 3)));
    System.out.println(greatestProduct(productListVertical(parseListOfStrings(chopString(data)), 0, 3, 20)));

  }

  public static ArrayList<String> chopString(String s) {
      String choppyBoi = new String(s);
      ArrayList<String> result = new ArrayList<>();
      while (choppyBoi.length() > 1) {
        result.add(choppyBoi.substring(0,2));
        choppyBoi = choppyBoi.substring(2);
      }
      //result.add(choppyBoi);
      return result;
    }

    public static ArrayList<Integer> parseListOfStrings(ArrayList<String> s) {
      ArrayList<Integer> result = new ArrayList<>();
      for (String strung : s) {
        result.add(Integer.parseInt(strung));
      }
      return result;
    }
    public static int greatestProduct(ArrayList<Integer> list){
      int biggestNum = 1;
      for(int i = 0; i < list.size(); i++){

        if(list.get(i) > biggestNum){
          biggestNum = list.get(i);
        }
      }
      return biggestNum;

    }

    public static ArrayList<Integer> productListVertical(ArrayList<Integer> myLi, int min, int max, int rowLen){

      ArrayList<Integer> runningList = new   ArrayList<Integer>();
      for(int i = min; i < myLi.size() - max; i++){

        runningList.add(productSummation(getDigitsVertical(myLi, min  + i, max + i, rowLen)));

      }
      return runningList;

  }

    public static int productSummation(ArrayList<Integer> myList){

    int runningResult = 1;
    for(int i = 0; i < myList.size(); i++){

      runningResult *= myList.get(i);

    }
    return runningResult;

  }

  public static ArrayList<Integer> getDigitsVertical(ArrayList<Integer> myList, int min, int max, int rowLen){

    ArrayList<Integer> runningResult = new ArrayList<Integer>();
    int c = 1;

    for(int i = min; i <= max * rowLen; i+= rowLen){

      c = myList.get(i);
      runningResult.add(c);
      if(min == (myList.size() - max)){
      return runningResult;
    }

    }
    return runningResult;

  }

}

Why do I get this error and how can I fix it?

guccifetus
  • 21
  • 2
  • 2
    At some point you try to access index 418. I recommend debugging to see what is causing it to try 418 – Tyler Nov 13 '19 at 21:20
  • If you added some comments, we might understand what you are trying to do. – LowKeyEnergy Nov 13 '19 at 21:38
  • 1
    `System.out.println(greatestProduct(productListVertical(parseListOfStrings(chopString(data)), 0, 3, 20)));` Classic Java – Tyler Nov 13 '19 at 21:41
  • It's not just about comments, but about cramming everything into one long statement.(`productSummation`? It's just called just product.) Anyway, in `getDigitsVertical` you should enforce that you can actually take the desired number of values from your current position. If `max * rowLen >= myList.size()`, you can't. – M Oehm Nov 13 '19 at 21:43
  • Yeah the problem is in this line `c = myList.get(i);`. You are trying to get `i = 418` when `myList.size() = 400`. – Austin Adams Nov 13 '19 at 21:45
  • Possible duplicate of [What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?](https://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it) – Dmitri Borohhov Nov 13 '19 at 22:27

1 Answers1

2

It is clear from the exception that you are attempting to check index 418 in an array containing only 400 elements.

In productListVertical(), variable i runs from 0 to 400-3=397 . Why?

Then you run productSummation() with every value in getDigitsVertical(myLi, min + i, max + i, rowLen), so argument 1 runs from 0 to 397, and argument 2 runs from 3 to 400.

The method getDigitsVertical() then has a loop which runs from i = min to max*rowLen. Your rowLen is 20, so you are running the loop up to 400*20 = 8000 . Then you attempt to get(i) for values of i up to 8000... in an array of size 400.

Of course your shit is going to get ruined.

Lessons to consider: Design your algorithm on paper. If the algorithm doesn't make sense to you, then don't expect it to make sense to a machine.

Comment your code so other people can help you.

LowKeyEnergy
  • 652
  • 4
  • 11