0

So, my code is supposed to look at in input file, the Strings it contains, split them wherever there is a space and out output the Strings separately. I tried using an array to assign those Strings that I split to variables, so that way I can access them when I want to print them out but I keep getting,

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 100
at Coma.main(Coma.java:26)

Can someone please help me? Please excuse my formatting of this question, for this is my first time using StackOverflow.

here's my code

import java.io.File;
import java.util.Scanner;
import java.io.*;
import java.util.*;
import java.lang.ArrayIndexOutOfBoundsException;

public class Coma {

public static void main(String[] args)throws IOException {
    // TODO Auto-generated method stub
    String SENTENCE;
    int NUM_LINES;
    Scanner sc= new Scanner(new File("coma.in"));
    NUM_LINES=sc.nextInt();

    for(int i=0;i<NUM_LINES;i++){
        SENTENCE=sc.nextLine();
        String [] temp;
        String delimiter=" ";
        temp=SENTENCE.split(delimiter);
        String year= temp[0];
        String word=temp[1];

        System.out.println("Nurse: Sir you've been in a coma since " + year     + "\nMe: How's my favorite " + word + " doing?");
    }
}

} 

Here's the input from the file coma.in

3
1495 Constantinople
1962 JFK
1990 USSR
  • Are you sure that `temp` holds more than one element? You should check first that the `split` call actually splits something (and does not return an array of size 1) – UnholySheep Nov 12 '16 at 22:12
  • The problem is with your input data – Vasu Nov 12 '16 at 22:12
  • please provide some lines from your file – Mohammed Housseyn Taleb Nov 12 '16 at 22:15
  • http://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it – Reimeus Nov 12 '16 at 22:16
  • If the number of lines integer is in a line of its own, you might wanna add a `sc.nextLine();` before the loop, as the `sc.nextInt();` does not move the `Scanner` to the next line. Thus the first `sc.nextLine();` inside the loop would just read the (empty) rest of the `NUM_LINES` line. – user2390182 Nov 12 '16 at 22:16
  • @MohammedHousseynTaleb I added the lines from my file. – Daniel Augustine Nov 12 '16 at 22:28

3 Answers3

1

The problem is most likely with your coma.in file format. However, assuming a correct file formatted like so:

data.txt

20 team

10 dog

You could simplify your code to this:

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

public class ReadFile {

    public static void main(String[] args) throws FileNotFoundException {
        Scanner sc = new Scanner(new File("data.txt"));
        // default delimiter is whitespace (Character.isWhitespace)
        while (sc.hasNext()) { // true if another token to read
             System.out.println("Nurse: Sir you've been in a coma since "
                     + sc.next() + "\nMe: How's my favorite "
                     + sc.next() + " doing?");
        }
    }

}
Community
  • 1
  • 1
d.j.brown
  • 1,822
  • 1
  • 12
  • 14
1

Assuming your file format is sth like:

2
1981 x
1982 y

Then

sc.nextInt();  // only moves sc past the next token, NOT beyond the line separator

will only read the 2 and stop right there, and NOT consume the newline! Thus, in order to read the next line (1981 x), you have to add another sc.nextLine() to actually consume the (empty) string after the 2 in order to get to the next line. You are then splitting the empty string which in turn leads to the ArrayIndexOutOfBoundsException as the resulting array is only of length 1:

//...
NUM_LINES=sc.nextInt();
sc.nextLine();  // add this line; 

for(int i=0;i<NUM_LINES;i++){
    SENTENCE=sc.nextLine();
    //...

Because of this behaviour of the nextInt, nextFloat. etc. methods, I tend to prefer using nextLine and the parse... methods:

NUM_LINES=Integer.parseInt(sc.nextLine().strip()); 
user2390182
  • 72,016
  • 6
  • 67
  • 89
1

You can replace :

NUM_LINES=sc.nextInt();

by :

NUM_LINES=Integer.valueOf(sc.nextLine());

And it will work fine.

sofien.lpx
  • 91
  • 1
  • 4