有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在第一次尝试保存数据后,在关闭程序后无法打开

public class internetCalculator extends javax.swing.JFrame {

    ArrayList<User> list = new ArrayList<User>();

    public internetCalculator() throws IOException {
        initComponents();
        loadDataFromFile();
        jPanel1.hide();
        mainMenuJ.show(true);
    }


    public void saveDataToFile() throws IOException {
        //Below is Internet Plan, D for DIGI, M for Maxis....
        double D = 0.05, M = 0.10, C = 0.02, R1 = 0.12;
        double total = 0, tax = 0; //Plan Tax Rate Per MB
        double normal = 30.0, pro = 45.0, superPro = 65.0, ultra = 90;//Package

        try {
            String name = nameTF.getText();
            String phNo = phNoTF.getText();
            String usage = usageTF.getText();
            String bPlan = planCB.getSelectedItem().toString();
            String bPackage = packageCB.getSelectedItem().toString();

            double internetUsage = Double.parseDouble(usage);

            //First Calculation ***PLAN***
            if (planCB.getSelectedItem().toString().equals("Digi Plan")) {
                total = internetUsage * D;
            } else if (planCB.getSelectedItem().toString().equals("Maxis Plan")) {
                total = internetUsage * M;
            } else if (planCB.getSelectedItem().toString().equals("Celcom Plan")) {
                total = internetUsage * C;
            } else if (planCB.getSelectedItem().toString().equals("Red1 Plan")) {
                total = internetUsage * R1;
            }
            if (packageCB.getSelectedItem().toString().equals("Normal")) {
                tax = total + normal;
            } else if (packageCB.getSelectedItem().toString().equals("Pro")) {
                tax = total + pro;
            } else if (packageCB.getSelectedItem().toString().equals("SuperPro")) {
                tax = total + superPro;
            } else if (packageCB.getSelectedItem().toString().equals("UltraHighSpeed")) {
                tax = total + ultra;
            }

            User users = new User(name, phNo, bPlan, bPackage, tax);
            list.add(users); //add object s to array
            File outFile = new File("Internet_Tax.txt");
            FileWriter outFileStream = new FileWriter(outFile, true);
            PrintWriter outStream = new PrintWriter(outFileStream);
            outStream.println(name);
            outStream.println(phNo);
            outStream.println(bPlan);
            outStream.println(bPackage);
            outStream.println(tax);
            outStream.close();

            //If User Didnt Choose any Plan and Package , Display Error
            if ((packageCB.getSelectedItem().toString().equals("Select"))
                || (planCB.getSelectedItem().toString().equals("Select"))) {
                throw new Exception("Please Select PLAN or PACKAGE to Perform Calculation !");
            }

            if (!name.matches("[a-zA-Z]+")) {
                throw new Exception("Name With Letter with A - Z ONLY !");
            }// name with only Letter
            if (!phNo.matches("[0-9]+")) {
                throw new Exception("Phone Number with DIGIT number ONLY! ");
            }//Phone number only DIGIT
            if (!usage.matches("[0-9]+")) {
                throw new Exception("Internet Usage with DIGIT number ONLY! ");
            }//Internet Usage only DIGIT

        } catch (Exception e) {
            outputTA.setText(e.getMessage());
        }
    }//End Save Data To File

    public void loadDataFromFile() throws FileNotFoundException, IOException {
        File inFile = new File("Internet_Tax.txt");

        if (inFile.exists()) {
            FileReader fileReader = new FileReader(inFile);
            Scanner scanner = new Scanner(inFile);
            list.clear();
            DefaultTableModel stable = new DefaultTableModel(0, 0);
            String header[] = new String[]{"Name", "Phone", "Plan","Package","Total_Tax"};
            stable.setColumnIdentifiers(header);
            tableT.setModel(stable);

            while (scanner.hasNextLine()) {
                String name = scanner.nextLine();
                String phNo = scanner.nextLine();
                String bPlan = scanner.nextLine();
                String bPackage = scanner.nextLine();
                double tax = scanner.nextDouble();
                User users = new User(name, phNo, bPlan, bPackage, tax);
                stable.addRow(new Object[]{name, phNo, bPlan, bPackage, tax});
                list.add(users);

            }

            scanner.close();
            fileReader.close();
            nameTF.setText(""); // name
            phNoTF.setText(""); // matric
            usageTF.setText("");  // Phone
            planCB.setSelectedItem("Select");
            packageCB.setSelectedItem("Select");
        } else {
            DefaultTableModel stable = new DefaultTableModel(0, 0);
            String header[] = new String[]{"Name", "Phone", "Plan","Package","Total_Tax"};
            stable.setColumnIdentifiers(header);
            tableT.setModel(stable);
        }
}

在第一次尝试运行该程序时,该程序似乎运行良好,并在表中显示数据。关闭程序并再次运行程序后,它会显示错误。我不知道我的代码中有什么错误。异常错误显示在loadDataFromFile()上:

Here is the Error message


共 (1) 个答案

  1. # 1 楼答案

    我想这样可以告诉你问题出在哪里

    将扫描代码放入try catch并在获得双倍值之前添加一个条件

    例如:

     while (scanner.hasNextLine()) {
          try {
            String name = scanner.nextLine();
            String phNo = scanner.nextLine();
            String bPlan = scanner.nextLine();
            String bPackage = scanner.nextLine();
    
             if(scanner.hasNextDouble()){
               double tax = scanner.nextDouble();
             }else{System.out.println("Value is not Double!")}
    
        } catch (Exception e) {
                System.out.println("WARNING : " + e.getMessage());
        }finally {
            scanner.close();
            User users = new User(name, phNo, bPlan, bPackage, tax);
            stable.addRow(new Object[]{name, phNo, bPlan, bPackage, tax});
            list.add(users);
       }                    
    }