• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help me this code step by step especially at static boolen convert  RSS feed

 
Ranch Hand
Posts: 481
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class Test {
public static void main(String[] args){
if( args.length > 0 ||
convert( args[0] )) {
System.out.print("Good ");
}
else {
System.out.print("Bad ");
}
System.out.println( number );
}

static float number = 0 ;
static boolean convert( String s ){
try {
number = Float.parseFloat( s );
return true ;
}catch (NumberFormatException e ){
number = Float.NaN ;
return false ;
}
}
}

Help me this code step by step especially at static boolen convert
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
dear Karthik Rajashekaran

the static method boolean convert is meant to check the validity of the argument passed right!!! but unfortunately it will not work as you have used a short circuit OR operator( || ). recall that a short circuit operator need not execute both its operands , if the first is true then it ignores the second and returns/evaluates to true.Since this validation is done in the second part just the passing of a command argument is enough to evaluate the if statement to true.so the convert call is never called!!!
so the output is always Good number if a cmd argument is given(any argument) orelse a NullPointerException if no amd args are specified!!!
Regards
Simon


 
Karu Raj
Ranch Hand
Posts: 481
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ho yes
thanks a lot.

I need to be more alert while doing it
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!