-1
package com.Text.Scanner.java;

import java.util.ArrayList;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;


public class TextScanner {
    public static void main(String ...args) throws IOException{
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter names for parsing");
        String input = sc.nextLine();
        ArrayList<String> names = new ArrayList<String>();
        for (int i = 0;i<=input.length();i++) {
            names.add(input.substring(0, input.indexOf(",")));
            input = input.substring(input.indexOf(",")+1); 
        }

I had issues here but I didn't think much of it, could be this

        System.out.println(names);
        // handles the string import to arraylist

        BufferedReader reader;
        try {
            reader = new BufferedReader(new FileReader("file location"));
            //finds file
            String line = reader.readLine();
            //reads line
            while (line != null) {
                for (int i = 0; i <= line.length(); i++) {
                    if (line.contains(names.get(i))) {
                        //gets name from array to scan line for
                        System.out.println(line.substring(4, line.indexOf(names.get(i)) + names.get(i).length()));
                        //controls length
                        line = reader.readLine();
                        }
                    }
                }
            reader.close();
           }
                catch (IOException e) {
             e.printStackTrace();
        }
    }
}

It returns this:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 4 out of bounds for length 4
    at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
    at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
    at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248)
    at java.base/java.util.Objects.checkIndex(Objects.java:373)
    at java.base/java.util.ArrayList.get(ArrayList.java:426)
    at com.Text.Scanner.java.TextScanner.main(TextScanner.java:32)

My goal is to setup a program that will scan the text file for a list of names (first and last) and then return the data associated with them.

Boken
  • 4,825
  • 10
  • 32
  • 42
Dan
  • 34
  • 3
  • 1
    `i <= line.length()` should be `i < line.length()` - the Exception tells you that you are reading index 4 of a List that has length 4, when the maximum index is 3. – Hulk Jul 09 '20 at 06:24
  • related: https://stackoverflow.com/questions/18410035/ways-to-iterate-over-a-list-in-java – Hulk Jul 09 '20 at 06:30

1 Answers1

0

The problem was here:

                for (int i = 0; i < line.length(); i++) {
                if (line.contains(names.get(i))) {
                    //gets name from array to scan line for
                    System.out.println(line.substring(4, line.indexOf(names.get(i)) + names.get(i).length()));
                    //controls length
                    line = reader.readLine();
                    }
                }

The reason why it didn't work is because the length of line is bigger than the arraylist, the solution is to do this instead:

                for (int i = 0; i < names.size(); i++) {
                if (line.contains(names.get(i))) {
                    //gets name from array to scan line for
                    System.out.println(line.substring(4, line.indexOf(names.get(i)) + names.get(i).length()));
                    //controls length
                    line = reader.readLine();
                    }
                }
Dan
  • 34
  • 3