读取文件中的特定行

时间:2014-03-31 00:28:16

标签: java jframe filereader

很难解释这个问题,但是我会尽力而为。基本上我希望程序读取文本文件的第1-6行,然后读取第7-12行,然后是13-18。但现在不是。

我的输出是:

First run

Second run

忽略左上方的数字,这样我就可以在按钮上测试我的锁了。

我不知道该怎么做。任何帮助将不胜感激。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.*;

public class Game extends JFrame
{
JLabel lblQuestion;
JRadioButton btA;
JRadioButton btB;
JRadioButton btC;
JRadioButton btD;
JButton btLock;
JTextField txtQuestion;
int question = 0;

public Game()
{
    getContentPane().setLayout(null);
    setupGUI();
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}  

    btLock.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            btLock.setEnabled(false);
            btLock.setText("Lock In");
            txtQuestion.setText(Integer.toString(question));
            try{
                String[] array = questions();
                lblQuestion.setText("<html>"+array[0]+"</html>");
                btA.setText(array[1]);
                btB.setText(array[2]);
                btC.setText(array[3]);
                btD.setText(array[4]);
            }catch(Exception ex){}



            question++;
        }
    });

    setTitle("Who wants to be a millionare");
    setSize(570,400);
    setVisible(true);
    setResizable(false);
    setLocationRelativeTo(null);


}

public String[] questions() throws IOException{
    File file = new File("questions.txt");
    if (!file.exists()){
        throw new FileNotFoundException("Could not find \"users\" file");
    }
    FileReader fr = new FileReader(file.getAbsoluteFile());
    BufferedReader br = new BufferedReader(fr);

    String[] array = new String[6];

    //I wanted this to skip the lines i dont need but i didnt work
    for(int i = 0; (i*5) < question; i++){
        br.readLine();
    }

    for(int i = 0; i < 5; i++){
        array[i] = br.readLine();
    }

    br.close();
    return array;   
}

public static void main( String args[] )
{
    new Game();
}
}  

1 个答案:

答案 0 :(得分:0)

我是个白痴

for(int i = 0; i < question; i++){
        br.readLine();
        br.readLine();
        br.readLine();
        br.readLine();
        br.readLine();
        br.readLine();
    }

    for(int i = 0; i < 6; i++){
        array[i] = br.readLine();
    }

完成