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