1

I'm very new to java. I have to make a program in school, but when I try to run this, I get an error. ArrayIndex....... : 0 at line 139, that wil be where I put two * in front. I'm way to tired to find the error myself. I've been working on this the whole day. Thank you!

PS: I'm using a package called easyIO, in case some of you couldn't understand some of the commands.

edit: I finally found it, and it was a small problem. But now I can't make the damn program find what I search for in the Array.

 void UtskriftArt() {
    In Utskrift = new In();
    In søkefil = new In("Fugler.txt");
    int i= 0;
    int teller = 0;
    String[][]  ArtArray = new String[teller][4];


    for(; i > 0 && !søkefil.endOfFile(); i++){
        søkefil.readLine();
        teller++;
        if(søkefil.endOfFile()){
        søkefil.close();
        }
    } 
    System.out.print("Hvilken art vil du søke opp og skriv ut obeservasjonsdata for? ");
    String ArtSøke = Utskrift.inWord().trim();
    String Art = " ";
    System.out.println("\t\t" + Art);
    for(i = 0; i <= ArtArray.length; i++){
        **if(ArtSøke == ArtArray[i][0]){
        Art = ArtArray[i][0];
        System.out.print(ArtArray[i][1]);
        System.out.print("\t" + ArtArray[i][2]);
        System.out.println("\t" + ArtArray[i][3]);
            }
  • 2 tips: you can not compare a string with == you need to do string.equals(otherString). And don't use special characters in variable names. – Mirco Sep 25 '13 at 16:01
  • I would stay with using English names in programs. Or at least names restricted to the ASCII set... Trust me. National characters are only a pain to maintain. And I'm not English... – ppeterka Sep 25 '13 at 16:01
  • The loop that starts like this: `for(; i > 0 && !søkefil.endOfFile(); i++)` will never execute. The second part says to keep executing the loop as long as `i` is greater than 0 (and we haven't reached the end of the file). However, `i` starts out as 0, so the condition fails right away. – ajb Sep 25 '13 at 16:03

3 Answers3

5

Your array is declared as

int teller = 0;
String[][]  ArtArray = new String[teller][4];

Its first dimension is of size 0. It doesn't have any elements, so you can't access an element at index 0 with

if(ArtSøke == ArtArray[i][0]){
                       ^ i is 0

This wouldn't happen if you used a proper for loop

for(i = 0; i < ArtArray.length; i++){ 

Note that the condition is only the <, not <=. If it is <= you will always go out of bounds since arrays use 0-based indices.

You should declare the actual size you want to array to have

teller = 1; // 1, 2, 3, more?

On a side but serious note, read How do I compare strings in Java?

Community
  • 1
  • 1
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • I want to have a dynamic number, because this is a program a user will use to register observations. I fixed a lot of the problems, but I can't get the damn thing to start from the beginning of the file again to catch all the words. – user2816040 Sep 25 '13 at 17:11
  • @user2816040 I can't comment on that since I don't know your `In` class or your actual problem for that matter. Consider asking another question after identifying the problem. – Sotirios Delimanolis Sep 25 '13 at 17:12
1
int teller = 0;
String[][]  ArtArray = new String[teller][4];

This is causing the problem (well described by Sotirios Delimanolis' answer).

You can solve your problem by initializing AryArray just before second for-loop in which case teller will be greater than 0 after being incremented by first for-loop.

String[][]  ArtArray = new String[teller][4];
for(i = 0; i < ArtArray.length; i++) // improvement here too
Community
  • 1
  • 1
TheKojuEffect
  • 20,103
  • 19
  • 89
  • 125
0

When you have an array, the first element in the array is accessed by index 0. This means that an array with 1 element will only work when you access index 0, all other index values will be out of range.

If you have an array of zero elements, then there is nothing to access at index 0 (or at any other index value).

As mentioned by Sotirios, new String[tellar][4] allocates an array of zero elements, each element being an array of four elements. This is because int tellar = 0; Since the array has zero elements, index 0 is out of range.

DwB
  • 37,124
  • 11
  • 56
  • 82