简体   繁体   中英

I am trying to resize my array but I keep getting an index out of bounds exception

I am trying to create a dictionary out of a .txt file.The problem I think is in my addToDict method. I am trying to resize th array when its full because I am reading from a text file of unknown size but I can only use arrays. I get an out of bounds exception when I am printing the array. I have no idea whats wrong and I have been working on the project for days now. I am also having trouble with my else statement in my addToDict method. It is also and out of bounds exception

import java.io.*;
import java.util.Scanner;
import java.util.regex.*;

public class BuildDict {
    static String dict[] = new String[20];
    static int index = 0;

    public static void main(String args[]) {
        readIn();
    }

    public static void readIn() {
        File inFile = new File("alice.txt");
        try {
            Scanner scan = new Scanner(inFile);
            while (scan.hasNext()) {
                String word = scan.next();
                if (!Character.isUpperCase(word.charAt(0))) {
                    checkRegex(word);
                }
            }
            scan.close();
        } catch (IOException e) {
            System.out.println("Error");
        }
    }

    public static void addToDict(String word) {

        if (index == dict.length) {

           String newAr[] = new String[index * 2];               
           for (int i = 0; i < index; i++) {
                newAr[i] = dict[i];
            }
            newAr[index] = word;
            index++;
            dict = newAr;
            for (int j = 0; j < index; j++) {
                System.out.println(newAr[j]);
            }
        } else {
            dict[index] = word;
            index++;
        }
    }

    public static void checkRegex(String word) {
        String regex = ("[^A-Za-z]");
        Pattern check = Pattern.compile(regex);
        Matcher regexMatcher = check.matcher(word);
        if (!regexMatcher.find()) {
            addToDict(word);
        }
    }
}

You haven't assigned the new array to dict .

if (index == dict.length) {
    for (int i = 0; i < index; i++) {
        newAr[i] = dict[i];
    }
    newAr[index] = word;
    index++;
    for (int j = 0; j < index; j++) {
        System.out.println(newAr[j]);
    }

    // Assign dict to the new array.
    dict = newAr;
} else {
    dict[index] = word;
    index++;
}

The value of index is 0 when the following statement is executed.

String newAr[] = new String[index*2];

Try revisiting your logic. index should be given a positive value before this method is called. That's why you are getting OutOfBounds.

EDIT: Did you mean to write index+2 ?

You have

static int index = 0;

You need to change the value of this variable, based on your file, otherwise you will always have an error in this line

String newAr[] = new String[index*2];

Instead of using a array use a arraylist for when you don't know the size of your array. It will save you a lot of trouble. I find they are much easier to work with in general then normal arrays.

ArrayList<String> dict = new ArrayList<>();
dict.add(word);

//displaying values
for( int i = 0; i < dict.size(); i++ ){
    System.out.println(dict.get(i));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM