I am trying to make a trivia game in java, and I am required to read in information from a text file, then store the data in one of two LinkedLists (Either questions or answers) In theory, anything that is not a question nor answer (like a blank space or a title heading) will be ignored, and not added to any List. However, I never made it that far.
When I use stn.charAt(0) it works for the first line in the text file, but no subsequent lines, even though the value is reset after each iteration of the while loop. I am receiving this error: java.lang.StringIndexOutOfBoundsException: String index out of range: 0
What am I doing wrong?
code:
import java.io.*;
import java.util.*;
public class Game
{
public static void main(String[] args)
{
File pack = new File("SP21.txt");
FileReader f;
JFrame j = new JFrame();
LinkedList<String> ans = new LinkedList<String>();
LinkedList<String> qus = new LinkedList<String>();
try
{
f = new FileReader(pack);
BufferedReader br = new BufferedReader(f);
String st;
String stn;
while((st = br.readLine()) != null)
{
//This line removes all non-ascii characters, which is ridiculously beneficial to reading Q's and A's
stn = st.replaceAll("[^\\p{ASCII}]" , "");
char c = stn.charAt(0);
}
br.close();
} catch (FileNotFoundException e) {
JOptionPane.showMessageDialog(j , "File not found" + e , "Error" , JOptionPane.ERROR_MESSAGE);
} catch (Exception e) {
JOptionPane.showMessageDialog(j , "Whoops! Something went wrong! " + e , "Error" , JOptionPane.ERROR_MESSAGE);
}
}
}
And the attached file is:
40-POINT SNAPSTART TO ROUND ONE
1.Name the last Plantagenet and Yorkist King of England.
A. RICHARD III
2. Of Denmark, Sweden or Finland, which country has no territory north of the Arctic Circle?
A. DENMARK
3. With what type of emergency should I associate the Heimlich Manoeuvre?
A. CHOKING (ON FOOD)
4. In what city is the headquarters of the Canadian Wheat Board?
A. WINNIPEG
30-POINT OPEN QUESTION - WRITERS
5. Which French author wrote The “Three Musketeers” and “The Count of Monte Cristo"?
A. ALEXANDRE DUMAS
6. Which Scottish novelist wrote his first historical romance in prose, entitled “Ivanhoe”?
A. SIR WALTER SCOTT
7. Which American writer is famous for “John Brown’s Body” and “The Devil and Daniel Webster”?
A. STEPHEN BENET