2017-08-07 105 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);

相关问题