0

I have been working on this program and the last thing, I was trying to create an array with "an" number of objects, so it could dynimacally creates objects.

Anyhow, I keep getting the error above and I don't have a clue why? Would appreciate it, if any1 could help!

I wrote a comment of where the error appears...

Thanks :)

    public static void main (String [] args) {


            int an= readInteger ("Mit wie vielen Vektoren möchten Sie rechnen: ");
            //Polynom v = new Polynom (an); // generates "an" number of vectors

            //dies ForLoop ist fuer die Rechnung von v1, v2, usw..
            for (int i =0; i<an;i++) { 
                System.out.println("Vektor v" +(i+1));

                System.out.println("Welche Dimension soll der Vektor" +(i+1) +" besitzen: ");
                int dim = readInteger();

            System.out.println("Wie soll der Vektor gefüllt werden? \n"
                    + "(1) Werte eingeben  \n"
                    + "(2) Vektor mit Zufallszahlen füllen \n"
                    + "(3) Vektor mit Nullen füllen \n");


                Polynom [] vek = new Polynom [an]; // create array of "a" number of elements
                                                   // that holds objects addresses
                // this will create objects in a loop
                for (int  x = 0; x < vek.length; x++) { 
                    vek [x] = new Polynom(x+1); //this will call constructor

                    //Generiert "an" Anzahl von Objekten
                    int a = readInteger(); 
                    System.out.println("Auswahl: "+a +"\n");
                    if (a==1) {
                        System.out.println("Bitte geben Sie die Elemente des Vektors ein: ");
                        vek[dim].eingabe(); //ERROR IS HERE
                        vek[dim].ausgabe(); 
                    }
                    else if (a==2) {
                        System.out.println(dim +" Zufallsvariablen wurden generiert ");
                        vek[dim].Zufallszahlen(); //ERROR ALSO HERE
                        vek[dim].ausgabe();
                    }
                    else if (a==3) {
                        System.out.println("Die " +dim +" Vektoren wurden mit Nullen gefuellt ");
                        vek[dim].Nullen(); //ERROR
                        vek[dim].ausgabe(); 
                    }
                }

            }

    }

}

Rick_C132
  • 1
  • 2
  • Shouldn't it be `vek[x]` instead of `vek[dim]`? – Eran May 15 '18 at 06:27
  • beyond that: read about java naming conventions. The names you are using are violating them all over the place, and beyond them, most of the names are really not communicating much (and that is bad) – GhostCat May 15 '18 at 06:34
  • @GhostCat The name I used are shortcuts for german words. That's why it makes sence here.. However, you're right I should have clarified my question more. I actually already checked the other threads and they didn't solve my problem. Most of the answers are those who didn't get that an array start at 0 and wrote vek.length >= rather than only > to start from 1. But that's unfortunately not my problem and I honestly after lots of search I still didn't find why I get this error. It just doesn't make any sense!!! :/ Thanks anyways bro. :) – Rick_C132 May 15 '18 at 07:39
  • Yes, but again: the convention says to use verbs for method names, like fuelleMitNull(), or fuelleMitZufallszahl(). Zufahlszahl() ist einfach nur schlecht. Beyond that: sorry, but in the end, there are no detours. Actually, arrays arent that complex. They always start at index 0. If you want to always start at 1 (bad idea), then well you have to make sure that the array has n+1 entries. Sorry, this is all basic stuff, and that duplicated question gives you a ton of material to study. there is no point in me explaining again ... what is all written up there. Asking for explanations ... – GhostCat May 15 '18 at 07:54
  • often feels convenient. But in the long run, it slows you down. Because an essential part of programming is to research things yourself. down to the bottom of everything. – GhostCat May 15 '18 at 07:55

1 Answers1

1

dim should range between 0 and vek.length-1, both included. Otherwise, you would get java.lang.ArrayIndexOutOfBoundsException at places like vek[dim]. Check the purpose and return value for int dim = readInteger();

Also, to make the code simpler, readable and debuggable, renaming variables and method name would certainly help.

gargkshitiz
  • 2,130
  • 17
  • 19
  • Thanks for your answer gargkshitiz.. `dim` is already declared in another class and should be initialized by the user.. The idea of writing `vek[dim]` was that each vector (1, 2, 3 etc.) should has a `dim` number of elements **bigger than 0 and equal or less than `an`** (which is the number of vectors initialized, also by the user) and then it would be inititailized in that specific vector. `private int an; //no. of vectors private int dim; private double [] v; Polynom (int d) { dim =d; for (int i=0; i<=an; i++) { v = new double [dim]; } }` – Rick_C132 May 15 '18 at 07:28
  • If I was able to answer your question, I request you to please accept my post as an answer – gargkshitiz May 15 '18 at 07:55
  • It actually still didn't solve the error :(.. I am still trying to figure it out :/ – Rick_C132 May 15 '18 at 08:48