2016-12-28 75 views
-4

我的代碼中有JTextfield,我要求用戶輸入他們的ID號。輸入識別號碼後,該代碼將查看輸入的識別號碼是否在grade.txt(我創建的文件)中。如果輸入的ID號碼在文本文件中,它將顯示用戶的等級。當我運行這個程序並輸入不同的ID號時,它顯示第一行 (ID號)的相同等級。 我需要你的幫助,我該如何編碼。Java中的控制結構

@Marcx這裏是完整的代碼:

/* 
* 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 student_grade; 

import java.util.*; 
import java.io.*; 
import java.awt.Component; 
import javax.swing.JOptionPane; 
import javax.swing.JTextField; 
/** 
* 
* @author Christian 
*/ 
public class Student_grade { 
    public static void main(String[] args) throws FileNotFoundException { 


//declaration 
String firstName, lastName, discard; 
double subject1, subject2, subject3, subject4, subject5; 
long userID = 0; 


//get file from the address 
Scanner inFile= new Scanner(new FileReader("D:\\grade.txt")); 
    HashMap<Double, student> map = new HashMap<Double, student>(); 
    while (inFile.hasNext()) 
    { 
     student s = new student(); 
     s.id = inFile.nextDouble();   
     s.subject1 = inFile.nextDouble(); 
     s.subject2 = inFile.nextDouble(); 
     s.subject3 = inFile.nextDouble(); 
     s.subject4 = inFile.nextDouble(); 
     s.subject5 = inFile.nextDouble(); 
     map.put(s.id, s); 
    } 
    student student = map.get(userID); 

//JTextField 
JTextField fullName = new JTextField(); 
JTextField section = new JTextField(); 
JTextField idnumber2 = new JTextField(); 
Object[] message = 
{ 
    "Name:", fullName, 
    "Section:", section, 
    "ID Number:", idnumber2 
}; 
Component parent = null; 
int option = JOptionPane.showConfirmDialog(parent, message, "Check your Grades!", JOptionPane.OK_CANCEL_OPTION); 
//display information 
if (option == JOptionPane.OK_OPTION) 
    { 
    String value1 = fullName.getText(); 
    String value2 = section.getText(); 
    String value3 = idnumber2.getText(); 
      String outputStr= "Name: "+ fullName.getText() + 
       "\nSection: "+ section.getText()+ 
       "\nID Number: "+ student.id + 
       "\nMath: "+ student.subject1 + 
       "\nEnglish: "+ student.subject2 + 
       "\nFilipino: "+ student.subject3 + 
       "\nReligion: "+ student.subject4 + 
       "\nComputer: "+ student.subject5; 

    JOptionPane.showMessageDialog(null, outputStr,"User Information",JOptionPane.INFORMATION_MESSAGE); 
    } 
//closing main arguments 
                     } 
                 }  
+1

您似乎只是抓住文件中的第一組數字,當然它只會顯示第一個學生的成績。 – Carcigenicate

+0

你能幫我解答我該怎麼做?我剛開始學習Java,所以對我來說很難。 –

+0

閱讀文件的內容,通過搜索找到您要查找的名稱,然後獲取它後面的數字。 – Carcigenicate

回答

0

你通過文件需要循環,並尋找學生證,通過用戶輸入的那些相匹配。

您可以每次都讀取和循環遍歷文件,或者您可以使用結構來「存儲」內容供以後使用。

如果你想「存儲」文件的內容,你可以使用HashMap

創建一個Student類,它將用於保存所需的信息。

public class Student{ 
    public Double id; 
    public Double subject1; 
    public Double subject2; 
    public Double subject3; 
    public Double subject4; 
    public Double subject5; 
} 

在你的主類,你可以

Scanner inFile = new Scanner(new FileReader("D:\\grade.txt")); 
    HashMap<Double, Student> map = new HashMap<Double, Student>(); 
    while (inFile.hasNext()) 
    { 
     Student s = new Student(); 
     s.id = inFile.nextDouble();   
     s.subject1 = inFile.nextDouble(); 
     s.subject2 = inFile.nextDouble(); 
     s.subject3 = inFile.nextDouble(); 
     s.subject4 = inFile.nextDouble(); 
     s.subject5 = inFile.nextDouble(); 
     map.put(s.id, s); 
    } 

然後,你需要從HashMap

Student student = map.get(userID); 

獲得輸入的用戶名,並獲得Student對象,然後您可以打印/顯示等級簡單地稱爲student物體

String outputStr= "Name: "+ fullName.getText() + 
       "\nSection: "+ section.getText() + 
       "\nID Number: "+ student.id + 
       "\nMath: "+ student.subject1 + 
       "\nEnglish: "+ student.subject2 + 
       "\nFilipino: "+ student.subject3 + 
       "\nReligion: "+ student.subject4 + 
       "\nComputer: "+ student.subject5; 

您需要將這些片斷調整你的代碼..

如果你不想每次使用HashMap和通讀文件,只需獲得,而之前和循環檢查,如果在ID ID在每行中相同。

+0

@Marchx先生,我試着把'Student student = map.get(userID);'但它說它找不到符號?我該如何解決這個問題?謝謝 –

+0

你需要聲明'long UserID = xxx',其中'xxx'是用戶自己輸入的ID – Marcx

+0

我聲明long UserID = 0;但我得到錯誤'線程中的異常'主要「java.lang.NullPointerException \t at student_grade.Student_grade.main(Student_grade.java:59) C:\ Users \ Christian \ AppData \ Local \ NetBeans \ Cache \ 8.2 \ executor-snippets \ run.xml:53:將Java返回:1 將名稱,部分和ID號碼 –