微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

空字段和整数值的输入验证

如何解决空字段和整数值的输入验证

我正在尝试将数据保存在 Students.txt 中(在一个 JFrame 中),然后在另一个框架中的 JTable(根据百分比 - 从高到低)中打开它。

这是我编写文本文件代码(它确实可以) 我还想补充的唯一一件事是,如果其中一个 txt 输入为空或者 txtInputMark 不是整数值,它会显示一条错误消息。

    package hpxs200.pkg1;
    import javax.swing.JTextField;
    import java.io.File;
    import javax.swing.JOptionPane;
    import java.io.FileWriter;
    import java.io.IOException;

   public class frmRegistration extends javax.swing.JFrame {
   String strInput1;
    String strInput2;
    String strInput3;
    Integer intInput1;
   File Textfile = new File("Student.txt");

   private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {
     strInput1 = txtInputFName.getText();
    strInput2 = txtInputLName.getText();
    strInput3 = txtInputMark.getText();
      try
      {
          Textfile.createNewFile();
          lblOutput1.setText("Student information has been saved");
      }
      catch (Exception Ex)
      {
          JOptionPane.showMessageDialog(null,Ex);
      }

     try 
     {
         String strfilename = "Student.txt";
         FileWriter fw = new FileWriter(strfilename,true);
         fw.write(strInput1 + ";");
         fw.write(strInput2 + ";");
         fw.write(strInput3 + "\n");
         fw.close();
     }
     catch (IOException | NumberFormatException Ex)
     {
         JOptionPane.showMessageDialog( null,Ex);
     }
}                                       

private void txtInputMarkActionPerformed(java.awt.event.ActionEvent evt) {
    // Todo add your handling code here:
}                                            

private void btnClearactionPerformed(java.awt.event.ActionEvent evt) {
    txtInputFName.setText("");
    txtInputLName.setText("");
    txtInputMark.setText("");'''

这就是我检索数据的全部内容,我不知道该怎么做。

我希望将文本文件中的数据显示在表格中,从最高标记到最低标记,请记住数据文件是在另一个框架中创建的。

private void btndisplayStudentInfoActionPerformed(ActionEvent evt) {
    String strInput4;
    BufferedReader brReader = null;
    InputStreamReader isReader = null;
   try{
        try (FileInputStream fisFin = new FileInputStream("Students.txt"); 
           InputStreamReader isrReader = new InputStreamReader(fisFin))
        {
            brReader = newBufferedReader(isrReader);
            while((strInput4 = brReader.readLine()) !=null)
            {
                lblOutput.setText("content has been read");
            }
            brReader.close();
        }
   }
   catch (Exception Ex)
   {
       JOptionPane.showMessageDialog(null,Ex);
   }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。