BuddaBob
BuddaBob

Reputation: 11

What does this error mean in my project?

Hello I am working on a project and I continue to get an error message and I can't figure out why. Can someone please help?

import java.util.Scanner;
import java.util.ArrayList;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.File;
import java.io.BufferedReader;
import java.io.FileReader;
import java.lang.StringIndexOutOfBoundsException;


public class Authorship{

     public static void main(String []args){
        Scanner scanner = new Scanner(System.in);
        System.out.println("Name of input file:");
        String correctAnswers = scanner.next();
        File file =new File(scanner.next());
        if(!file.exists()){
            System.out.println(" This file does not exist");
        } else {
            BufferedReader reader = null;
            ArrayList<String> list = new ArrayList<String>();
            try {
                reader = new BufferedReader(new FileReader(file));
                String text = null;
                int count[] = new int[13];
                while ((text = reader.readLine()) != null) {
                    String[] line = text.split(" ");
                    for(int i=0;i<line.length;i++){
                        int wordCount = line[i].length();
                        count[wordCount-1]++;
                        totalWordCount++;
                    }
                }
                for(int i = 0;i < 13;i++){
                  float percentage =count[i]*100/totalWordCount;
                  if(i != 12) {
                        System.out.printf("Proportion of "+(i+1)+"-letter  words: %.2f%%(%d words)", percentage, count[i]);
             } else {
                        System.out.printf("Proportion of 13- (or more)letter words: %.2f%%(%d words)", percentage, count[i]);
                        System.out.println("\n");
                    }
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (reader != null) {
                        reader.close();
                    }
                 } catch (IOException e) {
               } 
            }
        }
     }
  }
}

I receive the error:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
    at Authorship.main(Authorship.java:36)

Upvotes: 0

Views: 74

Answers (2)

Chazz
Chazz

Reputation: 31

I believe I fixed your code. It should run fine now. You needed to change totalWordCount++; to wordCount and *100/totalWordCount; to i

import java.util.Scanner;
import java.util.ArrayList;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.File;
import java.io.BufferedReader;
import java.io.FileReader;
import java.lang.StringIndexOutOfBoundsException;


public class Authorship{

     public static void main(String []args){
        Scanner scanner = new Scanner(System.in);
        System.out.println("Name of input file:");
        String correctAnswers = scanner.next();
        File file =new File(scanner.next());
        if(!file.exists()){
            System.out.println(" This file does not exist");
        } else {
            BufferedReader reader = null;
            ArrayList<String> list = new ArrayList<String>();
            try {
                reader = new BufferedReader(new FileReader(file));
                String text = null;
                int count[] = new int[13];
                while ((text = reader.readLine()) != null) {
                    String[] line = text.split(" ");
                    for(int i=0;i<line.length;i++){
                        int wordCount = line[i].length();
                        count[wordCount-1]++;
                        wordCount++;
                    }
                }
                for(int i = 0;i < 13;i++){
                  float percentage =count[i]*100/i;
                  if(i != 12) {
                        System.out.printf("Proportion of "+(i+1)+"-letter  words: %.2f%%(%d words)", percentage, count[i]);
             } else {
                        System.out.printf("Proportion of 13- (or more)letter words: %.2f%%(%d words)", percentage, count[i]);
                        System.out.println("\n");
                    }
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (reader != null) {
                        reader.close();
                    }
                 } catch (IOException e) {
               } 
            }
        }
     }
  }

Upvotes: 0

Madhawa Priyashantha
Madhawa Priyashantha

Reputation: 9862

if wordCount is 0 then [wordCount - 1] is -1

so you are getting ArrayIndexOutOfBoundsException: -1 exception when access -1 index . count[wordCount - 1]//error occured

to avoid this check wordcount length first.access wordCount - 1 only when wordCount > 0

while ((text = reader.readLine()) != null) {
    String[] line = text.split(" ");
    for (int i = 0; i < line.length; i++) {
        int wordCount = line[i].length();
        if (wordCount > 0) {
            count[wordCount - 1]++;
            totalWordCount++;
        }
    }
}

Upvotes: 4

Related Questions