-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;

    }

}

calling like this :

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
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Adevelopment
  • 265
  • 3
  • 15
  • 1
    Just throwing this out there... why is `vitalReportsDetails.getValue()` a `String` and not an `Integer`? – Joe C Aug 07 '17 at 05:34
  • The comparator is used to sort the data, it does not change them.. –  Aug 07 '17 at 05:34
  • Also, your `Comparator` needs to return `0` when the two values are the same. Yours does not. – Joe C Aug 07 '17 at 05:34
  • how to fix .. this . – Adevelopment Aug 07 '17 at 05:37
  • Start by making `getValue()` return an `int` and not a `String`. Then your fix becomes trivial. – Joe C Aug 07 '17 at 05:38
  • means i dint get ur answer @JoeC – Adevelopment Aug 07 '17 at 05:41
  • 2
    as @IntelliJ Amiya said, it has no value that can be convert to int. for example if the value is "Ali", how can you expect to change that to integer!? – Mehran Zamani Aug 07 '17 at 05:41
  • Post the full exception, the text of the exception for me (Oracle java 8) is java.lang.NumberFormatException: For input string: "", if it is really saying Invalid int: "" then you have a different error than I have seen or a different version of the JVM. You are handling the single invalid string empty string, any non-numeric string will produce the same error, 'Ali' for example as Mehran suggested. If the string has nothing but unprintable characters then the exception message could show an empty string. – Steve Bauer Aug 07 '17 at 05:46
  • https://paste.ofcode.org/zsmL2GMfbaUt3HwmR73nYy – Adevelopment Aug 07 '17 at 05:48

4 Answers4

3

You get "" because that value is the min value in that list (your comparator said that). You can handle that by simply call String temp = Collections.min(listOfData, new VitalCompare()).getValue(); int min = Integer.parseInt(temp.equals("") ? "0" : temp);

1

Maybe you can use:

vitalReportsDetails.getValue().equals("") || vitalReportsDetails.isEmpty() ? "0" : vitalReportsDetails.getValue()
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
Andres Lopez
  • 139
  • 1
  • 4
  • 17
1

java.lang.NumberFormatException: Invalid int: ""

NumberFormatException

Thrown to indicate that the application has attempted to convert a string to one of the numeric types, but that the string does not have the appropriate format.

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

Problem coming from n1 & n2 . Debug both .

The statement [Either n1 or n2] would throw NumberFormatException because it generate String Which cannot be parsed to int.

IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
0

Same problem happened to me , this happens when string is in the form of Float or Double or like other form other than numbers this error throws if it is in form of double or float try like below method

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

for Double

double doublestring = Double.parseDouble("100.22");
        //then
        int int2= (int) Math.round(doublestring );
Omar Dhanish
  • 885
  • 7
  • 18