2013-03-13 37 views
0

我使用java和flex爲我的web應用程序將用於研究所。 如果我的結果是標記,那麼少於60將是Re出現RA。對於年級系統,U會重新出現。但是如果結果是分級系統,我正面臨NumberFormatException。因此我附上我的代碼。請儘快告知。數字格式發生異常

XMLProcess xp = new XMLProcess(); 
try { 
    Document doc = xp.processXml(xml); 
    Element el = doc.getDocumentElement(); 
    NodeList nl = el.getElementsByTagName("batch"); 

    if (nl != null && nl.getLength() > 0) { 
     for (int i = 0; i < nl.getLength(); i++) { 

      Element el1 = (Element) nl.item(i); 
      NodeList nl1 = el1.getElementsByTagName("student"); 
      String schKey = el1.getAttribute("schkey"); 
      if (nl1 != null && nl1.getLength() > 0) { 
       for (int j = 0; j < nl1.getLength(); j++) { 
        String reAppear = ""; 
        String sms = ""; 
        Element el2 = (Element) nl1.item(j); 
        String studentName = el2.getAttribute("name"); 
        String studentId = el2.getAttribute("id"); 
        String studentKey = el2.getAttribute("key"); 
        sms += "" + studentName + "\n"; 
        sms += "Sem " 
          + sem.substring(sem.indexOf("-") + 1, sem.length()) 
          + " " 
          + course 
          + "\n"; 
        sms += "" + exam + "\n"; 

        sms += (el2.getAttribute("mrg").equalsIgnoreCase("G") 
          ? "Sub: Grade" 
          : "Sub: Marks") 
          + "\n"; 
        for (int k = 0; 
          k < el2.getAttributes().getLength(); 
          k++) { 
         String aName = 
           el2.getAttributes().item(k).getNodeName(); 
         if (aName.equalsIgnoreCase("key") 
           || aName.equalsIgnoreCase("key") 
           || aName.equalsIgnoreCase("id") 
           || aName.equalsIgnoreCase("name") 
           || aName.equalsIgnoreCase("status") 
           || aName.equalsIgnoreCase("total") 
           || aName.equalsIgnoreCase("percentage") 
           || aName.equalsIgnoreCase("mrg")) { 
         } else { 


          sms += el2.getAttributes().item(k).getNodeName().substring(
            1) 
            + ": " 
            + (el2.getAttribute("mrg").equalsIgnoreCase("G") 
            ? (el2.getAttributes().item(k).getNodeValue()) 
            : (Integer.parseInt(
            el2.getAttributes().item(k).getNodeValue()) 
            < 60 
            ? "RA" 
            : el2.getAttributes().item(k).getNodeValue())) 
            + "\n"; 

/***Here exception occurs***/    if (Integer.parseInt(
            el2.getAttributes().item(k).getNodeValue()) 
            <= 60) { 
           reAppear = "SM"; 

          } 
          if (el2.getAttributes().item(k).getNodeValue() 
            == "U") { 
           reAppear = "SG"; 

          } 

         } 
        } 
        if (reAppear.equalsIgnoreCase("SM")) { 
         sms += "RA : FAIL LESS 60 marks. "; 
         sms += "Meet Principal/Director/HOD"; 
        } 
        if (reAppear.equalsIgnoreCase("SG")) { 
         sms += "U - Fail Re Appear. "; 
         sms += "Meet Principal/Director/HOD"; 
        } 
+5

可以共享堆棧跟蹤和線路 – 2013-03-13 06:35:05

回答

4

Integer.parseInt(..)當它收到的字符串表達式爲空或不是有效整數時產生此異常。你有2個地方在你使用這個電話。它看起來像你對XML節點中包含的數據的假設是不正確的。該節點不存在,或者其值不是整數。

0
if (Integer.parseInt(el2.getAttributes().item(k).getNodeValue())<= 60) 

此行爲您創建問題。 你需要編寫代碼就像

if (el2.getAttributes().item(k).getNodeValue() 
            == "U") { 
           reAppear = "SG"; 

}else if (Integer.parseInt(
       el2.getAttributes().item(k).getNodeValue()) 
            <= 60) { 
       reAppear = "SM"; 

     } 
+0

我仍然得到了錯誤。對於標記只有我已經將字符串轉換爲int。當我傳遞A,B,C作爲評分時,我必須做什麼? – 2013-03-13 06:59:59

+0

可以通過示例XML並告訴我'K'值爲錯誤 – 2013-03-13 09:47:19