Java

Moderators: zibadian
Number of threads: 7836
Number of posts: 18235

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Why does this code not work ? Posted by pediasure on 13 Dec 2010 at 8:39 PM
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");
}

}

}
Report
This post has been deleted. Posted by kldezhu on 13 Dec 2010 at 11:44 PM
This post has been deleted.
Report
Re: Why does this code not work ? Posted by silveredge52 on 14 Dec 2010 at 5:46 AM
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:
if (S.length>0) ...


Regardes, se52
Report
Re: Why does this code not work ? Posted by pediasure on 14 Dec 2010 at 1:22 PM
You are right, it is 0 and not NULL.
Thanks




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.