2014-03-31 217 views
0

难以解释问题,但生病尽我所能。基本上我想让程序读取文本文件的第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(); 
} 
} 
+0

首先,确定你的代码一切是毫无关系的问题。然后**从你的帖子中取出**。这是噪音。 –

+0

@SotiriosDelimanolis对不起,没有注意到我真的发了多少垃圾 – Kevin

+0

现在,阅读文件部分实际上与GUI没有任何关系,因此创建一个专门的方法,只是执行文件阅读。使用调试器查看您正在阅读的内容以及发生的情况。 –

回答

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(); 
    } 

完成