0

I am trying to split a .txt file into an array so I can access individual elements from it. However I get the following error, Index 1 out of bounds for length 1 at babySort.main(babySort.java:21).

I am unsure where I am going wrong because I used the same code on a test string earlier and it splits into the appropriate amount of elements.

I suspect it has something to do with the while loop, but I can't seem to wrap my mind around it, any help would be appreciated.

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Scanner;


public class babySort {
    public static void main(String[] args) throws FileNotFoundException {
        File inputFile = new File("src/babynames.txt");
        Scanner in = new Scanner(inputFile);


        String test = "Why wont this work?";
        String[] test2 = test.split("\\s+");
        System.out.println(test2[2]);

        while (in.hasNext()) {
            String input = in.next();
            String[] inputSplit = input.split("\\s+");
            //System.out.println(Arrays.toString(inputSplit));
            System.out.println(inputSplit[1]);
        }

            }
        }
  • Check the contents of `"src/babynames.txt"`; in your `while` loop, what does `input` print? – sushant Oct 24 '21 at 14:41
  • input prints out the contents of the babynames.txt one at a time like so, 7 Andrew 272600 1.3277 Brittany 190779 0.9720 and when I print inputSplit it does the same except all the contents have brackets. – Dustin McDonnell Oct 24 '21 at 14:44
  • .next returns a token that is probably already split. Did you mean to use "readLine"? – matt Oct 24 '21 at 15:00

1 Answers1

2

From the documentation:

A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace. The resulting tokens may then be converted into values of different types using the various next methods.

My understanding is that you want to read the input line-by-line. You can use FileReader instead of Scanner to read the file line-by-line. You can also use BufferedReader like so:

try (BufferedReader br = new BufferedReader(new FileReader(inputFile, StandardCharsets.UTF_8))) {
    String input;
    while ((input = br.readLine()) != null) {
       // process the line
       String[] inputSplit = input.split("\\s+");
      //System.out.println(Arrays.toString(inputSplit));
      System.out.println(inputSplit[1]);
    }
}
sushant
  • 1,101
  • 2
  • 9
  • 15
  • You should never create a `FileReader` (or anything else that converts binary data into `String`/`char[]`/`CharBuffer`) without specifying a character encoding. – Joachim Sauer Oct 24 '21 at 14:54
  • That worked! Thank you very much, I'll read more into the documentation of FileReader and BufferedReader. @JoachimSauer I'm sorry but I don't understand what you mean by character encoding. – Dustin McDonnell Oct 24 '21 at 14:56
  • 1
    @JoachimSauer Noted, I have modified the method. – sushant Oct 24 '21 at 14:59
  • @DustinMcDonnell Thanks! Glad it worked. Please consider upvoting or marking this as the answer if it helped. – sushant Oct 24 '21 at 15:00