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

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,772
  • 1
  • 13
  • 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
  • 67,685
  • 6
  • 55
  • 77
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