Search code examples
javastringexceptionif-statementnumberformatexception

Not able to convert a string to integer. Sometime getting NumberFormatException


I am trying to get massage count, notification count and friend request count from a facebook account. After getting the string data from facebook i am converting it into interger by using Interger.parseInt(String) method. As per my code logic i am putting the data i am getting from facebook inside error, error1 and error2 when they are a string and after converting them into interger i am putting them inside actualfriend, actualmassage and actualnotification. At the end i am comparing the actual data with some expected data.

The problem is i am getting correct results for massage count but for friend request and notification i am getting correct data in error1 and error2 but once i convert them into an integer from string them i gets actualfriend=0 and 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

As you can see i am getting- Readable Massage = 1(getting from error variable) actualmassage = 1(getting from actualmassage after converting it to integer from string). Total Notification = 1(getting from error1 variable) actualnotification = 0(getting from actualmassage after converting it to integer from string) which is wrong. Total FriendRequest = 1(getting from error2 variable) actualfriend = 0(getting from actualfriend after converting it to integer from string) which is wrong.

changed the code from-

      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

But after doing this i got-

 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)

Solution

  • It looks like the first if(error.length() != 0) && (error1.length() != 0) && (error2.length() != 0) is failing to assign values to actualnotification and actualfriend because it is not reaching them.Change the code with below code. I created separate if block for each and it worked.

         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