2016-12-28 79 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号码 –