如何解决java.lang.NumberFormatException:代码中的int无效:“”错误?

时间:2019-02-03 00:28:00

标签: java try-catch numberformatexception

我的代码中出现错误java.lang.NumberFormatException: Invalid int: ""

  //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.
  }

我在这里查看了关于stackoverflow的答案。据我所知,我已经听取了建议-使用try catch语句并将其包装在if语句中,但是我仍然在网上遇到错误

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

请问有什么解决方法的建议吗?

1 个答案:

答案 0 :(得分:0)

我非常确定您的输入String包含空值,然后将其拆分为空字符串。当然,这些不能解析为数字。

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

当然,这将引发异常:

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

我建议您只需抓住Integer来检查您是否真的有NumberFormatException

try {
    Integer.parseInt("");
} catch(NumberFormatException e) {
    // not an Integer, proceed accordingly
}