0

I'm getting the error java.lang.NumberFormatException: Invalid int: "" in my code:

  //breaking up the response into respective parts
  //so we can get values for 'random reviews' string
  try {
    JSONArray responseObject = new JSONArray(response);

    //use StringBuilder, so we can append values
    StringBuilder private_review_ids = new StringBuilder();

    for (int i = 0; i < responseObject.length(); i++) {

      JSONObject obj = responseObject.getJSONObject(i);

      //get the "private_review_ids" part of the response, append
      //each value into a string private_review_ids
      private_review_ids.append(obj.getString("private_review_ids").toString());

      System.out.println("private_review_ids: " + private_review_ids);

  //check that private_review_ids is not 0
  if (private_review_ids.length() != 0){

    //make it all into a single string
    String private_review_ids2 = private_review_ids.toString();

    //we only want the numbers (the review_ids) so get rid of other stuff
    private_review_ids2 = private_review_ids2.replaceAll("[^0-9]+", ",");
    //get rid of the first ","
    private_review_ids2 = private_review_ids2.replaceFirst(",", "");

      System.out.println("private_review_ids2:" + private_review_ids2);

    //a string array, separated by commas
    //because we were getting a stray comma at the start
    String[] just_numbers = private_review_ids2.split(",");

    //We want 3 random numbers from the array just_numbers
    HashSet<Integer> integers = new HashSet<>(3);
    Random random = new Random();

      if (integers != null) {

    //get 3 random numbers
    while (integers.size() < 3) {

      integers.add(Integer.parseInt(just_numbers[random.nextInt(just_numbers.length)]));

    }

    //UNDO THIS System.out.println("1. the integers are " + integers);
    System.out.println("1. the integers are " + integers);

    //make a string from integers
    random_reviews = integers.toString();

    //remove the [ and ] from the string
    random_reviews = random_reviews.substring(1, random_reviews.length() - 1);
    //UNDO THIS System.out.println("2. random_reviews : " + random_reviews);
    System.out.println("2. random_reviews : " + random_reviews);

    Toast.makeText(PopulistoListView.this, "random_reviews : " + random_reviews, Toast.LENGTH_LONG).show();

  }}
  }catch (JSONException e) {
    Log.e("MYAPP", "unexpected JSON exception", e);
    // Do something to recover ... or kill the app.
  }

I've looked at answers here on stackoverflow. As far as I can tell I've heeded advice - using a try catch statement and wrapping it in an if statement but I still get the error on the line

integers.add(Integer.parseInt(just_numbers[random.nextInt(just_numbers.length)]));

Any suggestions on how to solve it please?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
CHarris
  • 2,693
  • 8
  • 45
  • 71
  • Possible duplicate of [What's the best way to check if a String represents an integer in Java?](https://stackoverflow.com/questions/237159/whats-the-best-way-to-check-if-a-string-represents-an-integer-in-java) – RaminS Feb 03 '19 at 00:32
  • Have a look at your input string, most likely you got something like `1,2,3,,5,6`. The double `,,` is the problem here, since the `String` is split but not actually a parsable number. – Glains Feb 03 '19 at 00:34
  • 1
    Actually, I think the exception message is saying that the string being parsed is empty. In general, there are three ways to deal with this: 1) validate the string before you parse it, 2) parse it anyway and then catch and **appropriately** deal with the exception, or 3) if figure out why your code is *mishandling* the input prior to parsing it. (In this case, why your splitting code is giving you an empty string.) – Stephen C Feb 03 '19 at 00:38
  • @StephenC Most of the time response from server will be like 1,7,12, or 3 other random numbers. But in this case yes, there are no numbers coming from the server, so just trying to deal with this situation in my code. – CHarris Feb 03 '19 at 00:40
  • So ... that should be easy. How do you test if a string is empty?? – Stephen C Feb 03 '19 at 00:43
  • @StephenC I'm using `if (private_review_ids.length() != 0){}` now (edited code in question) but error still happens. – CHarris Feb 03 '19 at 01:08
  • That's testing the length of the string before you split it. You need to do things in the right order. Read the javadocs for `trim()` and `split(String)` carefully. Then think it through. – Stephen C Feb 03 '19 at 01:27

1 Answers1

0

I am pretty sure your input String contains empty values, which are then split into empty Strings. Of course, those cannot be parsed to a number.

String test = "1,2,,4";
String[] splitted = test.split(","); // ["1", "2", "", "4"]

And of course, this will throw an exception:

Integer.parseInt(""); // exception
Integer.parseInt(" 1"); // also exception, not trimmed

I recommend to check if you really got an Integer by simply catching the NumberFormatException.

try {
    Integer.parseInt("");
} catch(NumberFormatException e) {
    // not an Integer, proceed accordingly
}
Glains
  • 2,773
  • 3
  • 16
  • 30