Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Why does this code not work ?

pediasurepediasure Member Posts: 2
Just started learning java.

Please let me know why the piece of code for String = NULL does not execute when I do not give any command line input. It does not print "No input detected".


import java.lang.*;

public class Echo{

public static void main (String S[]){

if (S!=null){
for (int i=0;i<S.length;i++){
System.out.print(S[i]);
}
}

else{
System.out.println("No input detected");
}

}

}

Comments

  • silveredge52silveredge52 Member Posts: 198
    Hey,
    A guess to the answer to your question is that the input argument string has been instantiated with a length of 0 (therefore it will not equal null). When there is no command line input, it remains that way.
    Thus to determine if any input was submitted on the command line is to use the test:
    [code]if (S.length>0) ...[/code]

    Regardes, se52
  • pediasurepediasure Member Posts: 2
    You are right, it is 0 and not NULL.
    Thanks

Sign In or Register to comment.