2017-05-25 65 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

感謝兄弟,我認爲它是另一種出路,並且我已將數據類型更改爲字符串及其工作。 –

相關問題