2017-05-25 30 views
0
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

+0

太多的代码。请创建一个[MCVE](https://stackoverflow.com/help/mcve)。 – Turing85

+1

麻烦的是双税= scanner.nextDouble(); - 尝试在此处读入字符串,将字符串打印到System.out以进行可视化检查,然后将Double.parseDouble()值转换为税务变量 – Jan

+0

第一次运行后有人更改了文件Internet_Tax.txt吗?你确定它没有改变? –

回答

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); 
    }      
} 
+0

感谢兄弟,我认为它是另一种出路,并且我已将数据类型更改为字符串及其工作。 –

相关问题