Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

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
 
Whoever got anywhere by being normal? Just ask this exceptional tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic