2017-08-07 109 views
-2
public class VitalCompare implements Comparator<VitalReportsDetails> { 
    @Override 
    public int compare(VitalReportsDetails vitalReportsDetails, VitalReportsDetails t1) { 
     int n1 = Integer.parseInt((vitalReportsDetails.getValue().equals("") ? "0" : vitalReportsDetails.getValue())); 

     int n2 = Integer.parseInt((t1.getValue().equals("")? "0" : t1.getValue())); 
     if (n1 >= n2) { 
      return 1; 
     } 
     return -1; 

    } 

} 

調用是這樣的:如何解決java.lang.NumberFormatException:無效INT: 「」

int min=Integer.parseInt(Collections.min(listOfData, new VitalCompare()).getValue()); 

logcat的

8-07 11:17:49.604 27972-27972/com.cognistrength.caregiver E/AndroidRuntime: FATAL EXCEPTION: main 
                      Process: com.cognistrength.caregiver, PID: 27972 
                      java.lang.NumberFormatException: Invalid int: "" 
                       at java.lang.Integer.invalidInt(Integer.java:138) 
                       at java.lang.Integer.parseInt(Integer.java:358) 
                       at java.lang.Integer.parseInt(Integer.java:334) 
                       at com.cognistrength.caregiver.adapters.VitalGraphAdapter.onBindViewHolder(VitalGraphAdapter.java:123) 
                       at com.cognistrength.caregiver.adapters.VitalGraphAdapter 
+1

只是拋出這裏......爲什麼'vitalReportsDetails.getValue()'一個'字符串'而不是'整數'? –

+0

比較器用於對數據進行排序,但不會更改它們。 – 2017-08-07 05:34:11

+0

此外,當兩個值相同時,您的「比較器」需要返回「0」。你的不是。 –

回答

1

也許你可以使用:

vitalReportsDetails.getValue().equals("") || vitalReportsDetails.isEmpty() ? "0" : vitalReportsDetails.getValue() 
1

java.lang.NumberFormatException:無效INT: 「」

NumberFormatException

拋出,表明該應用程序試圖將一個 字符串轉換爲數字類型之一,但該字符串沒有 適當的格式。

 int n1 = Integer.parseInt((vitalReportsDetails.getValue().equals("") ? "0" : vitalReportsDetails.getValue())); 
    int n2 = Integer.parseInt((t1.getValue().equals("")? "0" : t1.getValue())); 

問題從n1 & n2到來。 調試兩者。

聲明[Either n1 or n2]將拋出NumberFormatException的,因爲它產生String不能被解析到int

+0

我將字符串轉換到整數 – Adevelopment

+0

@Adevelopment測試用例DEBUG請 –

+0

@Adevelopment你得到'SPACE' –

0

同樣的問題發生在我身上,當字符串是在浮動或喜歡比這個錯誤拋出的數之外的其它形式的形式,如果它是在雙形式或浮動嘗試像下面的方法

這種情況
float floatstring = FLoat.parseFloat("your_string"); 
    //then 
    int int1= (int) Math.round(floatstring); 

double doublestring = Double.parseDouble("100.22"); 
     //then 
     int int2= (int) Math.round(doublestring); 
+0

我越來越字符串,我們必須在int轉換 – Adevelopment

+0

是的,首先將您的字符串轉換爲浮點數或double,然後將其轉換爲int。看到上面的代碼。 –

+0

我已編輯答案現在重新檢查 –

3

你得到 「」,因爲該值在列表中的最小值(你的比較表示)。你可以通過簡單地打電話給 String temp = Collections.min(listOfData, new VitalCompare()).getValue(); int min = Integer.parseInt(temp.equals("") ? "0" : temp);String temp = Collections.min(listOfData, new VitalCompare()).getValue(); int min = Integer.parseInt(temp.equals("") ? "0" : temp);

相關問題