2015-04-02 112 views
-1

尝试从.txt文件读取到数组列表中,以便将其进一步导入到GUI中。 .txt文件中的数据由“|”分隔数据由单词和定义组成。 GUI将显示定义,用户将在jText字段中输入他们的答案,然后程序将根据.txt文件中的实际单词检查用户答案,并记录最后显示的分数。我们对代码的错误感到非常困惑,因为GUI中没有显示任何内容。感谢您的任何答复或建议。尝试从.txt文件读取到数组列表中

package sat.vocab.practice; 

/** 
* 
* @author Brandon Tavares 
* 
*/ 

import java.io.File; 
import java.io.FileNotFoundException; 
import java.util.ArrayList; 
import java.util.Scanner; 

public class SATVocabPractice { 
    public static void main(String[] args) { 
    File f = new File("C:/Users/Brandon/Documents/NetBeansProjects/SAT Vocab Practice/src/sat/vocab/practice/Words.txt"); 
     try{ 
      ArrayList<String> lines = get_arraylist_from_file(f); 
      for(int x = 0; x < lines.size(); x++){ 
       System.out.println(lines.get(x)); 
      } 
      System.out.println("Reading"); 
     } 
     catch(Exception e){ 
      e.printStackTrace(); 
     } 


    } 
    public static ArrayList<String> get_arraylist_from_file(File f) 
     throws FileNotFoundException { 
     Scanner s; 
     ArrayList<String> list = new ArrayList<String>(); 
     s = new Scanner(f); 
     while (s.hasNext()) { 
      System.out.println(s.next()); 
      list.add(s.next()); 
     } 
     s.close(); 
     return list; 
    } 
} 

这里是GUI代码。

/* 
* To change this license header, choose License Headers in Project Properties. 
* To change this template file, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package sat.vocab.practice; 

/** 
* 
* @author Brandon 
*/ 
public class MainView extends javax.swing.JFrame { 

    /** 
    * Creates new form MainView 
    */ 
    public MainView() { 
     initComponents(); 
    } 

    /** 
    * This method is called from within the constructor to initialize the form. 
    * WARNING: Do NOT modify this code. The content of this method is always 
    * regenerated by the Form Editor. 
    */ 
    @SuppressWarnings("unchecked") 
    // <editor-fold defaultstate="collapsed" desc="Generated Code">       
    private void initComponents() { 

     defintionHeader = new javax.swing.JLabel(); 
     definitionLabel = new javax.swing.JLabel(); 
     jLabel1 = new javax.swing.JLabel(); 
     jTextField1 = new javax.swing.JTextField(); 
     jToggleButton1 = new javax.swing.JToggleButton(); 
     score = new javax.swing.JLabel(); 
     jLabel3 = new javax.swing.JLabel(); 

     setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 
     setBackground(new java.awt.Color(0, 255, 204)); 
     setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR)); 
     setPreferredSize(new java.awt.Dimension(1920, 1080)); 

     defintionHeader.setText("Definition:"); 

     definitionLabel.setText("Definition Here"); 

     jLabel1.setText("Your Answer:"); 

     jTextField1.setText("jTextField1"); 

     jToggleButton1.setText("Submit Answer"); 

     score.setText("0"); 

     jLabel3.setText("Score:"); 

     javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
     getContentPane().setLayout(layout); 
     layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
       .addGap(63, 63, 63) 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
        .addGroup(layout.createSequentialGroup() 
         .addComponent(jLabel3) 
         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
         .addComponent(score)) 
        .addGroup(layout.createSequentialGroup() 
         .addComponent(defintionHeader) 
         .addGap(18, 18, 18) 
         .addComponent(definitionLabel)) 
        .addGroup(layout.createSequentialGroup() 
         .addComponent(jLabel1) 
         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) 
         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
          .addComponent(jToggleButton1) 
          .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE)))) 
       .addContainerGap(130, Short.MAX_VALUE)) 
     ); 
     layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
       .addGap(72, 72, 72) 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
        .addComponent(defintionHeader) 
        .addComponent(definitionLabel)) 
       .addGap(29, 29, 29) 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
        .addComponent(jLabel1) 
        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) 
       .addGap(45, 45, 45) 
       .addComponent(jToggleButton1) 
       .addGap(38, 38, 38) 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
        .addComponent(jLabel3) 
        .addComponent(score)) 
       .addContainerGap(45, Short.MAX_VALUE)) 
     ); 

     pack(); 
    }// </editor-fold>       

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String args[]) { 
     /* Set the Nimbus look and feel */ 
     //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> 
     /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. 
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */ 
     try { 
      for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { 
       if ("Nimbus".equals(info.getName())) { 
        javax.swing.UIManager.setLookAndFeel(info.getClassName()); 
        break; 
       } 
      } 
     } catch (ClassNotFoundException ex) { 
      java.util.logging.Logger.getLogger(MainView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (InstantiationException ex) { 
      java.util.logging.Logger.getLogger(MainView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (IllegalAccessException ex) { 
      java.util.logging.Logger.getLogger(MainView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
      java.util.logging.Logger.getLogger(MainView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } 
     //</editor-fold> 

     /* Create and display the form */ 
     java.awt.EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       new MainView().setVisible(true); 
      } 
     }); 
    } 

    // Variables declaration - do not modify      
    private javax.swing.JLabel definitionLabel; 
    private javax.swing.JLabel defintionHeader; 
    private javax.swing.JLabel jLabel1; 
    private javax.swing.JLabel jLabel3; 
    private javax.swing.JTextField jTextField1; 
    private javax.swing.JToggleButton jToggleButton1; 
    private javax.swing.JLabel score; 
    // End of variables declaration     
} 
+0

你只是在阅读线条,但不考虑标记'|'在线。你可以把你的GUI代码和你如何在那里发送解析的数据? – 2015-04-02 21:07:38

+0

我已经添加了GUI代码。 – 2015-04-02 21:29:53

回答

0

您的代码有例外。尝试这个。

public static ArrayList<String> get_arraylist_from_file(File f) 
    throws FileNotFoundException { 
    Scanner s; 
    ArrayList<String> list = new ArrayList<String>(); 
    s = new Scanner(f); 
    while (s.hasNext()) { 
String temp=s.next(); 
     System.out.println(temp); 
     list.add(temp); 
    } 
    s.close(); 
    return list; 
    } 
} 
0

阅读使用附加方法然后转动的StringBuilder到对象阵列分割对整个文本文件转换成一个StringBuilder对象的“|”然后遍历对象数组并将对象数组的每个部分添加到数组列表中。

相关问题