无法将字符串转换为整数。有时得到 NumberFormatException
Not able to convert a string to integer. Sometime getting NumberFormatException
我正在尝试从 Facebook 帐户获取消息数、通知数和好友请求数。从 facebook 获取字符串数据后,我使用 Interger.parseInt(String) 方法将其转换为整数。根据我的代码逻辑,我将从 facebook 获取的数据放入 error、error1 和 error2 中,当它们是字符串时,在将它们转换为整数后,我将它们放入 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 Massage = 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 赋值,因为它没有到达 them.Change 具有以下代码的代码。我为每个创建了单独的 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
我正在尝试从 Facebook 帐户获取消息数、通知数和好友请求数。从 facebook 获取字符串数据后,我使用 Interger.parseInt(String) 方法将其转换为整数。根据我的代码逻辑,我将从 facebook 获取的数据放入 error、error1 和 error2 中,当它们是字符串时,在将它们转换为整数后,我将它们放入 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 Massage = 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 赋值,因为它没有到达 them.Change 具有以下代码的代码。我为每个创建了单独的 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