-1

im trying to read a csv with dummy data into java arraylists. I don't know what happened, but I get the described error message above the next day I started the program again.

Here's my Code. I hope you don't get irritated by the german variables. I think the structure is important.

      package Aufgabe2;
    
    import java.io.BufferedReader;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    
    import java.text.ParseException;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Scanner;
    
    import Aufgabe2.Models.Einlagekonto;
    import Aufgabe2.Models.Girokonto;
    import Aufgabe2.Models.Kunde;

public class Kundenreport {

    public static void main(String[] args) throws ParseException, FileNotFoundException {

        Scanner scanner = new Scanner(System.in);

        List<Kunde> kunde = new ArrayList<Kunde>();

        String pathKunden = "/Users/testuser/OneDrive/03_Privat/05_Code/01_Java/university/project8/src/Aufgabe2/Data/Kunden.csv";
       
        String line = null;

        try {
            BufferedReader kundenReader = new BufferedReader(new FileReader(pathKunden));
            kundenReader.readLine();

            while ((line = kundenReader.readLine()) != null) {
                String[] valuesKunden = line.split(";");
                kunde.add(new Kunde(valuesKunden[0], valuesKunden[1], valuesKunden[2], valuesKunden[3]));
            }

            kundenReader.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

Here is the "Kunde" (engl. Customer) Class which inherits from the abstract class "Konto"

    package Aufgabe2.Models;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;


    import Aufgabe2.Abstract.Konto;
    
    public class Kunde extends Konto {
    
        public Kunde() {
    
        }
    
        // Kunde ohne Konten
        public Kunde(String kundenNr, String name, String vorname, String kundeSeit) {
            super(kundenNr, name, vorname, kundeSeit);
        }

And the "Konto" (engl. account) class possesses the attributes, abstract methods and getters/setter methods.

    package Aufgabe2.Abstract;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Scanner;

import Aufgabe2.Models.Kunde;

public abstract class Konto {

    public final static Date today = new Date();
    public Scanner scanner = new Scanner(System.in);
    public static SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy", Locale.GERMAN);
    private String kundenNr, name, vorname, kundeSeit;
    private double kontoStdGiro, kontoStdEinlage;
    private String anlagedatum, faelligkeitsdatum;

    public Konto() {

    }

    // Nur Kunde ohne Konten
    public Konto(String kundenNr, String name, String vorname, String kundeSeit) {
        this.kundenNr = kundenNr;
        this.name = name;
        this.vorname = vorname;
        this.kundeSeit = kundeSeit;
    }

I was searching for a while and thankful for every advise from you.

Thanks!

By the way, here is the folder structure of the project: Screenshot of repository

AbdoCherry
  • 23
  • 6

1 Answers1

0

Most languages - such as Java or Python are zero-indexed, meaning they start from 0 instead of 1 like we normally do when counting. So when accessing a particular index we have to go one less than its number, for example, if we want the second element in array arr, we would do arr[1]. So in this case an array of length one only has one index, meaning arr[0]. For further reading on zero index here's a link: https://softwareengineering.stackexchange.com/questions/110804/why-are-zero-based-arrays-the-norm

Jawand S.
  • 148
  • 10
  • Thanks for your comment, but as you can see in the code snippets, the index starts at 0 to access the first string of the csv and ends at 3. I do have 4 string elements per line and my object, stored in an arraylist has four attributes (Kundennummer; Name, Vorname (First Name) and KundeSeit (Customer since). But the error still returns out of bound…. Does the string array valuesKunde is the problem here? – AbdoCherry Jan 03 '22 at 03:25
  • After splitting a line into `valuesKunden` you have no checks on the size of the resulting array. It is possible that faulty input data will result in fewer than 4 elements., and since there is no explicit check, an index out of bounds will be the result. Two things to note: firstly the exception message will tell you the exact line number, and secondly, it's ArrayIndexOutOfBounds, not IndexOutOfBounds, the latter being what is documented for ArrayList - so it's probably the basic array type. – passer-by Jan 03 '22 at 03:43
  • If you want to simply access the last element in a java array you can do arr[arr.length - 1] this way it never goes out of bounds (unless it's length 0 I suppose) – Jawand S. Jan 03 '22 at 17:51