2014-03-31 3 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(); 
    } 

완료

관련 문제