Java 无法将字符串转换为整数。有时会收到NumberFormatException

Java 无法将字符串转换为整数。有时会收到NumberFormatException,java,string,exception,if-statement,numberformatexception,Java,String,Exception,If Statement,Numberformatexception,我正在尝试从facebook帐户获取按摩次数、通知次数和好友请求次数。从facebook获取字符串数据后,我使用integer.parseInt(string)方法将其转换为integer。根据我的代码逻辑,我将从facebook获取的数据放入error、error1和error2中,当它们是字符串时,将它们转换为integer后,我将它们放入actualfriend、actualmassage和actualnotification中。最后,我将实际数据与一些预期数据进行比较 问题是我得到了正确

我正在尝试从facebook帐户获取按摩次数、通知次数和好友请求次数。从facebook获取字符串数据后,我使用integer.parseInt(string)方法将其转换为integer。根据我的代码逻辑,我将从facebook获取的数据放入error、error1和error2中,当它们是字符串时,将它们转换为integer后,我将它们放入actualfriend、actualmassage和actualnotification中。最后,我将实际数据与一些预期数据进行比较

问题是我得到了正确的按摩计数结果,但对于朋友请求和通知,我在error1和error2中得到了正确的数据,但一旦我将它们从字符串转换成整数,我得到了actualfriend=0和actualnotification=0

 public  void operation() throws InterruptedException, AWTException ,StaleElementReferenceException,NoSuchElementException,NumberFormatException {
      int i=1;
      int j=1;
      while(i==1){
      String error = "1";
      String error1= "1";
      String error2= "1";
      System.out.println(error);
      if((error.length() != 0) && (error1.length() != 0) && (error2.length() != 0)){ 
      try{
       actualmassage = Integer.parseInt(error);
        }catch (NumberFormatException e) {
          error="5";
           try{ 
            actualnotification=Integer.parseInt(error1);
             }catch(NumberFormatException ee){
              error1="5";
               try{ 
                actualfriend=Integer.parseInt(error2);
                 }catch(NumberFormatException eee){
                   error2="5";
                 }//catch3
               }//catch2
           }//catch3
      }//if for massage 
      else if((error.length() !=0) && (error1.length() !=0) && (error2.length() !=0)){
          actualmassage = Integer.parseInt(error);
          actualnotification = Integer.parseInt(error1);
          actualfriend = Integer.parseInt(error2);
      }//else if
      else{

      }//else
      int expectedmassage =1;
      int expectednotification=0;
      int expectedfriend=0;
      if(expectedmassage == actualmassage ){
          Thread.sleep(4000);
          System.out.println("Readable Massage = "+error +" actualmassage = "+actualmassage);
      }else{
          System.outprintln("Does't Match");
          i++;
      }//else
      if(expectednotification == actualnotification){
          Thread.sleep(4000);
          System.out.println("Total Notification = "+error1 +" actualnotification = "+actualnotification);
      }else{
          System.outprintln("Does't Match");
          i++;
      }//else
      if(expectedfriend == actualfriend ){
          Thread.sleep(4000);
          System.out.println("Total FriendRequest = "+error2 +" actualfriend = "+actualfriend);
      }else{
          System.outprintln("Does't Match");
          i++;
      }//else

    //Counting number of runs
      System.out.println("Number of run = " + j);
      j++;
      }//while
      System.out.println("Stop");
      i=(i+2);  
 }//operation
public static void main(String[] args) throws StaleElementReferenceException, InterruptedException, AWTException,NoSuchElementException,NumberFormatException{
    fb c = new fb();
   }//main
  }//calss

Output for this-
1
Readable Massage = 1 actualmassage = 1
Total Notification = 1 actualnotification = 0
Total FriendRequest = 1 actualfriend = 0
Number of run = 1
1
Readable Massage = 1 actualmassage = 1
Total Notification = 1 actualnotification = 0
Total FriendRequest = 1 actualfriend = 0
Number of run = 2
如您所见,我得到了-Readable message=1(从错误变量获取)actualmassage=1(从字符串转换为整数后从actualmassage获取)。Total Notification=1(从error1变量获取)actualnotification=0(从字符串转换为整数后从actualmassage获取),这是错误的。Total FriendRequest=1(从error2变量获取)actualfriend=0(从字符串转换为整数后从actualfriend获取),这是错误的

将代码从-

      int expectedmassage =1;
      int expectednotification=0;
      int expectedfriend=0;
      if(expectedmassage == actualmassage ){
          Thread.sleep(4000);
          System.out.println("Readable Massage = "+error +" actualmassage = "+actualmassage);
      }else{
          System.out.print("Does't match");
          i++;
      }//else
      if(expectednotification == Integer.parseInt(error1)){
          Thread.sleep(4000);
          System.out.println("Total Notification = "+error1 +" actualnotification = "+actualnotification);
      }else{
          System.out.print("Does't match");
          i++;
      }//else
      if(expectedfriend == Integer.parseInt(error2)){
          Thread.sleep(4000);
          System.out.println("Total FriendRequest = "+error2 +" actualfriend = "+actualfriend);
      }else{
          System.out.print("Does't match");
          i++;
      }//else
但是做了这件事之后,我得到了-

 1
java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at com.selenium.fb.main1(fb.java:103)
at com.selenium.fb.main(fb.java:123)
第一个if(error.length()!=0)&&&&(error1.length()!=0)&&(error2.length()!=0)无法为actualnotification和actualfriend赋值,因为它们没有到达。请使用以下代码更改代码。我为每一个创建了单独的if块,它成功了

     try{
       actualmassage = Integer.parseInt(error);
        }catch (NumberFormatException e) {
          error="0";
        }//catch1
      }//if1
         if((error1.length() != 0)){
           try{ 
            actualnotification=Integer.parseInt(error1);
             }catch(NumberFormatException ee){
              error1="0";
               }//catch2
           }//if2   
        if((error2.length() != 0)){ 
              try{
                   actualfriend=Integer.parseInt(error2);
                 }catch(NumberFormatException eee){
                   error2="0";
                 }//catch3
                }//if3

您的问题看起来好像是由某个文本生成器创建的。你能做的最起码的事情就是检查你的问题,问问自己它是否可读,以及你能做些什么来改进它。提示:从清楚地解释问题开始。删除所有不必要的代码-使用可以显示如何重现问题的最小代码示例等@alfasin I更改了问题Sir的格式第
fb.java:103行中发生了什么?看起来您正在尝试解析一个空字符串。