1

I am having one problem that is preventing my entire code from working. It is having an array index out of bounds error, but it matches the file array perfectly, so I'm not sure what the problem is..

public void Menu() {
    prompt.welcomeMsg();
    prompt.nGramOptionMsg();
    String userInput = input.next();
    while (userInput.charAt(0) != 's' || userInput.charAt(0) != 'S') {
        if (userInput.charAt(0) == 'n' || userInput.charAt(0) == 'N') {
            prompt.nGramLengthMsg();
            int userIntut = input.nextInt();
            nGram = new NGram(userIntut);
            prompt.fileUpload();
            String userFilePut = input.next();
            FileOpener file = new FileOpener(userFilePut);
            String[] fileArray = file.openFile();
            for (int i = 0; i < fileArray.length; i++) {
                String[] splitedFileArray = fileArray[i].split("\\s+");
                list.add(splitedFileArray[i]);
            }
            String[] listToStringArray = (String[]) list.toArray(new String[0]);
            String[] nGrams = nGram.arrayToNGram(fileArray);
            for (int i = 0; i < nGrams.length; i++) {
                Word word;
                if (!hashMap.containsKey(nGrams[i])) {
                    word = new Word(nGrams[i], 1);
                    hashMap.put(word.getNGram(), word);
                } else {
                    Word tempWord = hashMap.remove(nGrams[i]);
                    tempWord.increaseAbsoluteFrequency();
                    hashMap.put(tempWord.getNGram(), tempWord);
                }

            }

            HashMapFiller fill = new HashMapFiller();
            fill.hashMap(hashMap);
            fill.print();

            prompt.goAgain();
        }

}

The problem occurs when the list.add is trying to add the splitedFileArray. I tried doing fileArray.length-1 but it had a similar error, except -1.

3
  • You should iterate the new array 'splitedFileArra' and then you can assign all part of splited array into list. This line is wrong: list.add(splitedFileArray[i]); 'i' not splited array index. Commented Oct 8, 2018 at 3:55
  • Possible duplicate of What is a debugger and how can it help me diagnose problems? Commented Oct 8, 2018 at 3:55
  • @dave is right. When the program compiles and runs, but givens runtime exceptions or unexpected results, it's time for some debugging. Help yourself to these free debugging tips if you need some suggestions on how to get started. And if you haven't yet familiarized yourself with your IDE's debugger, now is the perfect opportunity. Commented Oct 8, 2018 at 4:04

1 Answer 1

1

The root cause for this problem is that you are trying to access the array in following line. What actually happening in behind the scenes is that you actually try to access unknown sized array which is returned from the split() method. returned array size might be less than the defined index (in your case i).

list.add(splitedFileArray[i]);

You can resolve this problem as follows..

for (int i = 0; i < fileArray.length; i++) {
    String[] splitedFileArray = fileArray[i].split("\\s+");
    list.addAll(Arrays.asList(splitedFileArray));
}

Hope this answer will help you to resolve your problem...

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.