Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 读取文件时,我试图将每一行存储到相关的LinkedList,但无法读取第一个字符。这是怎么回事?_Java_File Io_Linked List_Charat - Fatal编程技术网

Java 读取文件时,我试图将每一行存储到相关的LinkedList,但无法读取第一个字符。这是怎么回事?

Java 读取文件时,我试图将每一行存储到相关的LinkedList,但无法读取第一个字符。这是怎么回事?,java,file-io,linked-list,charat,Java,File Io,Linked List,Charat,我试图用java做一个琐事游戏,我需要从文本文件中读取信息,然后将数据存储在两个LinkedList(问题或答案)中的一个理论上,任何不是问题或答案(如空格或标题)的内容都将被忽略,不会添加到任何列表中。然而,我从来没有走那么远 当我使用stn.charAt(0)时,它适用于文本文件中的第一行,但不适用于后续行,即使每次迭代while循环后都会重置该值。我收到此错误:java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:0 我做错了什么 代码:

我试图用java做一个琐事游戏,我需要从文本文件中读取信息,然后将数据存储在两个LinkedList(问题或答案)中的一个理论上,任何不是问题或答案(如空格或标题)的内容都将被忽略,不会添加到任何列表中。然而,我从来没有走那么远

当我使用
stn.charAt(0)
时,它适用于文本文件中的第一行,但不适用于后续行,即使每次迭代while循环后都会重置该值。我收到此错误:
java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:0

我做错了什么

代码:


你试着去做了吗?当一行为空时,它有0个字符,所以你不能访问它的第一个字符,因为它没有任何字符。并且您的文件至少有3行空行。学会使用你的调试器:它使类似的事情变得微不足道。或者至少在代码中添加println()以打印变量的值。
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);

        }

    }

}
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