• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 481
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ho yes
thanks a lot.

I need to be more alert while doing it
 
Men call me Jim. Women look past me to this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic