0

The code below is to visualize a printout of data generated in the rest of the program. It takes out the info from the wagens and locos who are inputted into a arraylist.

My main concern here is how to combine them in the right order of this and to get them to print out in the correct sequence.

Thats where my question comes in. I am receiving a

arrayoutofbound exception: 19

on my print integer array.

  1. I have no idea what i am doing wrong.
  2. I am creating and instantiating my array print 0 - 19 places (20 needed).
  3. I am filling up the slots with data in the setDataPrint, which consist of data inputted by 2 other arrays. One with the location of where the data should go and the other with the actual number needed.
  4. I add the final data from setTotalInPrint().

  5. Then i print out the data from schrijfData()

Any help would be greatly appreciated.

import java.util.ArrayList;

/**
 * Verzameling van alle gegevens voor het weergeven van de remmings bulletin
 *
 * @author Pieter-Jan Casteels
 * @version 2017-05-17
 */
public class Bulletin
{
    // Collection of the data
    private int[] print;

    // Totalen
    private int totaalAantal;
    private int totaalAfgezonderdeRem;
    private int totaalLengteInM;
    private int totaalMassa;
    private int totaalRemming;

    // Percentage
    private int percentageRemming;
    private int vereisteMassaRemming;
    private int vereisteRemmingPercentage;

    /**
     * Basic constructor
     */
    public Bulletin()
    {
        System.out.println("Kan niet gegenereerd worden zonder Trein classe");
    }

    /**
     * Full constructor voor de Bulletin classe
     * 
     * @param Trein de huidige trein
     */
    public Bulletin(Trein newTrein)
    {
        setPrint();
        setTotaalAantal(0);
        setAfgezonderdeRem(0);
        setTotaalLengteInM(0);
        setTotaalMassa(0);
        setTotaalRemming(0);
        setPercentageRemming(0);
        setVereisteMassaRemming(0);
        setVereisteRemmingPercentage(50);
        berekenLocoEnWagen(newTrein);
        remmingsPercentage();
        vereisteGeremdeMassa();
        setTotaalInPrint();
        schrijfData(print);
    }

    // Setters en Getters

    /**
     * Roep de classe aan voor het berekenen van de loco en wagen
     * Geef hier de arraylist aan mee
     */
    private void berekenLocoEnWagen(Trein newTrein)
    {
        berekeningLocos(newTrein.getLocos());
        berekeningWagens(newTrein.getWagens());
    }

    /**
     * Setter voor de array met gegevens die geprint moet worden
     */
    private void setPrint()
    {
        print = new int[19];
    }

    /**
     * De totaal optelling van de lengte van de trein
     * 
     * @param intiger de lengte in meter
     */
    private void setTotaalLengteInM(int value)
    {
        if(value >= 0)
        {
            totaalLengteInM += value;
        }
    }

    /**
     * De totaal optelling van de aantallen
     * 
     * @param intiger de aantallen
     */
    private void setTotaalAantal(int value)
    {
        if(value >= 0)
        {
            totaalAantal += value;
        }
    }

    /**
     * Het totaal afgezonderde remmen
     */
    private void setAfgezonderdeRem(int value)
    {
        if(value >= 0)
        {
            totaalAfgezonderdeRem += value;
        }
    }

    /**
     * De totaal optelling van de massas
     * 
     * @param intiger de massas
     */
    private void setTotaalMassa(int value)
    {
        if(value >= 0)
        {
            totaalMassa += value;
        }
    }

    /**
     * De totaal optelling van de remming
     * 
     * @param intiger de Remming
     */
    private void setTotaalRemming(int value)
    {
        if(value >= 0)
        {
            totaalRemming += value;
        }
    }

    /**
     * De weergave voor het percentage remming
     * 
     * @param intiger de Remming
     */
    private void setPercentageRemming(int value)
    {
        if(value >= 0)
        {
            percentageRemming += value;
        }
    }

    /**
     * De vereiste geremde massa
     */
    private void setVereisteMassaRemming(int value)
    {
        if(value >= 0)
        {
            vereisteMassaRemming += value;
        }
    }

    /**
     * De vereiste percentage voor remming
     */
    private void setVereisteRemmingPercentage(int value)
    {
        if(value >= 0)
        {
            vereisteRemmingPercentage += value;
        }
    }

    // Methodes

    /**
     * De verzameling van gegevens voor de locomotief weergave in de bulletin
     * Verzamel de gegevens per locomotief en plaats ze in de locale variabelen
     * Roep de setters aan voor de optellingen van de totalen
     */
    private void berekeningLocos(ArrayList<Loco> locos)
    {
        if(!locos.isEmpty())
        {
            // Aantal
            int sleepAantal = 0;
            int voertuigAantal = 0;

            // Lengte
            int sleepLengteDM = 0;
            int voertuigLengteDM = 0;
            int sleepInM = 0;
            int voertuigInM = 0;

            // Massa
            int sleepMassa = 0;
            int voertuigMassa = 0;

            // Remming
            int sleepRemming = 0;
            int voertuigRemming = 0;

            for(Loco loco : locos)
            {
                if(!loco.getAlsVoertuig())
                {
                    sleepAantal++;
                    sleepLengteDM += loco.getLengteDM();
                    sleepMassa += loco.getTotalMassa();
                    sleepRemming += loco.getRemming();
                }
                else
                {
                    voertuigAantal++;
                    voertuigLengteDM += loco.getLengteDM();
                    voertuigMassa += loco.getTotalMassa();
                    voertuigRemming += loco.getRemming();
                }

                if(loco.getAfgzRem())
                {
                    setAfgezonderdeRem(1);
                }
            }
            setTotaalAantal(locos.size());
            sleepInM = afrondenLengteInM(sleepLengteDM);
            setTotaalLengteInM(sleepInM);
            voertuigInM = afrondenLengteInM(voertuigLengteDM);
            setTotaalLengteInM(voertuigInM);
            setTotaalMassa(sleepMassa);
            setTotaalMassa(voertuigMassa);
            setTotaalRemming(sleepRemming);
            setTotaalRemming(voertuigRemming);

            int[] loc = {0, 1, 5, 6, 9, 10, 13, 14};
            int[] data = {sleepAantal, voertuigAantal, sleepInM, voertuigInM, sleepMassa, voertuigMassa, sleepRemming, voertuigRemming};
            setDataPrint(loc, data);
        }
        else
        {
            System.out.println("Zonder locomotief geen trein");
        }
    }

    /**
     * Input the data into the print array at location
     * 
     * @param int array loc, this is the location
     * @param int array data, this is the data for the location
     */
    private void setDataPrint(int[] loc, int[] data)
    {
        for(int i = 0; i < loc.length ; i++)
        {
            print[loc[i]] = data[i];
        }
    }

    /**
     * De verzameling van gegevens voor de wagen weergave in de bulletin
     * Verzamel de gegevens per wagen en plaats ze in de locale variabelen
     * Roep de setters aan voor de optellingen van de totalen
     */
    private void berekeningWagens(ArrayList<Wagen> wagens)
    {
        // Aantal
        int wagensAantal = wagens.size();

        // Lengte
        int voertuigLengteDM = 0;
        int voertuigInM = 0;

        // Massa
        int voertuigMassa = 0;

        // Remming
        int voertuigRemming = 0;

        for(Wagen wagen : wagens)
        {
            voertuigLengteDM += wagen.getLengteDM();
            voertuigRemming += wagen.getRemming();
            if(wagen.getAfgzRem())
            {
                setAfgezonderdeRem(1);
            }
        }
        setTotaalAantal(wagensAantal);
        voertuigInM = afrondenLengteInM(voertuigLengteDM);
        setTotaalLengteInM(voertuigInM);
        setTotaalMassa(voertuigMassa);
        setTotaalRemming(voertuigRemming);

        int[] loc = {2, 7, 11, 15};
        int[] data = {wagensAantal, voertuigInM, voertuigMassa, voertuigRemming};
        setDataPrint(loc, data);
    }

    /**
     * Afronden lengtes naar boven lengte naar meters
     */
    private int afrondenLengteInM(int totaalDM)
    {
        return (totaalDM + 9) / 10;
    }

    /**
     * Berekening remmings percentage van de trein
     * Afgerond naar beneden.
     */
    private void remmingsPercentage()
    {
        percentageRemming = (totaalRemming * 100) / totaalMassa;
    }

    /**
     * Vereiste Geremde Remming
     */
    private void vereisteGeremdeMassa()
    {
        vereisteMassaRemming = (vereisteRemmingPercentage * totaalMassa) / 100;
    }

    /**
     * Zet al de totaals in de print array
     */
    private void setTotaalInPrint()
    {
        int[] loc = {3, 4, 8, 12, 16, 17, 18, 19};
        int[] data = {totaalAantal, totaalAfgezonderdeRem, totaalLengteInM, totaalMassa, totaalRemming, percentageRemming, vereisteMassaRemming, vereisteRemmingPercentage};
        setDataPrint(loc, data);
    }

    /**
     * Print de data aan de hand van de print array
     */
    private void schrijfData(int[] print)
    {
        System.out.println("21 AANTAL");
        System.out.println("21.1 Sleeplocomotief(ven): " + print[0]);
        System.out.println("21.2 Loc als voertuig(en): " + print[1]);
        System.out.println("21.3 Andere voertuigen: " + print[2]);
        System.out.println("21.4 TOTAAL TREIN: " + print[3]);
        System.out.println("21.5 Voertuig met afgezonderde rem: " + print[4]); // 4

        // Lengte
        System.out.println("22 LENGTE");
        System.out.println("22.1 Sleeplocomotief(ven): " + print[5]);
        System.out.println("22.2 Loc als voertuig(en): " + print[6]);
        System.out.println("22.3 Andere voertuigen: " + print[7]);
        System.out.println("22.4 TOTAAL TREIN: " + print[8]); // 8

        // Massa
        System.out.println("23 MASSA");
        System.out.println("23.1 Sleeplocomotief(ven): " + print[9]);
        System.out.println("23.2 Loc als voertuig(en): " + print[10]);
        System.out.println("23.3 Andere voertuigen: " + print[11]);
        System.out.println("23.4 TOTAAL TREIN: " + print[12]); // 12

        // Remming
        System.out.println("24 WERKELIJKE GEREMDE MASSA");
        System.out.println("24.1 Sleeplocomotief(ven): " + print[13]);
        System.out.println("24.2 Loc als voertuig(en): " + print[14]);
        System.out.println("24.3 Andere voertuigen: " + print[15]);
        System.out.println("24.4 TOTAAL GEREMDE MASSA");
        System.out.println("24.41 Tonnage: " + print[16]);
        System.out.println("24.42 Percentage: " + print[17]); // 17

        // Vereiste remming
        System.out.println("25 VEREISTE GEREMDE MASSA");
        System.out.println("25.1 Tonnage: " + print[18]);
        System.out.println("25.2 Percentage: " + print[19]); // 19
    }
}
Abdullah Khan
  • 12,010
  • 6
  • 65
  • 78
  • You need to isolate where your error is coming from, its hard to debug an entire program for just one error. – pointerless May 18 '17 at 13:21
  • 1
    YOu say you need 20 entries, but you create an array of length 19: `print = new int[19];`. Thus when you try to access the 20th element at index 19 you're outside the array and hence you get the exception. In general, when dealing with multiple arrays at once (e.g. copying from one to the other) you should make sure they have the same length and if your code can't be sure of that it should check and handle that. – Thomas May 18 '17 at 13:22
  • `print = new int[19]` defines an array index `0..18`. `print[19]` is out of bounds. Try `print = new int[20]` – bradimus May 18 '17 at 13:22

1 Answers1

1

This is the cause, you are creating a print with 19 positions (0-18) you can't use the 19

 private void setPrint()
    {
        print = new int[19];
    }

The solution:

 private void setPrint()
        {
            print = new int[20];
        }
Erick Maia
  • 196
  • 6
  • ok i tried it before but probably forgot to compile. It indeed works. I do however find it a bit strange. As you start your array at value 0 and my end value is 19. – Pieter-Jan Casteels May 18 '17 at 14:07
  • Actually, this is not strange, because if you count the number of positions (0-19) you will get the 20 as you declared in the initialization – Erick Maia May 18 '17 at 14:12