File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes command line arguments Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "command line arguments" Watch "command line arguments" New topic
Author

command line arguments

Sasikanth Malladi
Ranch Hand

Joined: Nov 04, 2000
Posts: 175
Hi!
I found this Q on the mock test at javaprepare:


<blockquote>
public class test {
public static void main(String args[]) {
Integer intObj=Integer.valueOf(args[args.length-1]);
int i = intObj.intValue();
if(args.length > 1)
System.out.println(i);
if(args.length > 0)
System.out.println(i - 1);
else
System.out.println(i - 2);
}
}
<blockquote>


I think it should print 2 but it prints 1. Can anyone explain why?
Thanks,
Shashi
Kourosh Keshavarzi
Ranch Hand

Joined: Oct 23, 2000
Posts: 66
What arguments are you passing when you run the program?
Sasikanth Malladi
Ranch Hand

Joined: Nov 04, 2000
Posts: 175
oops!! I'm really sorry! I ran the prog as follows:
C:\java java test 2
Thanks,
Shashi
Sasikanth Malladi
Ranch Hand

Joined: Nov 04, 2000
Posts: 175
Mistake again! I mean
c:\java>java test 2
Thanks,
Shashi
Matt DeLacey
Ranch Hand

Joined: Oct 12, 2000
Posts: 318
OK, so there is one command line argument (the number 2). Therefore, the first if statement will fail because the length is not greater than 1, it IS 1. The second will succeed and so it will print i - 1. Since i=2, it will print 1.
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
args.length = 1
Integer intObj=Integer.valueOf(args[args.length-1]); = the value at args[0] = what you passed in which is 2.
int i = intObj.intValue(); so i = 2;
if(args.length > 1)
System.out.println(i); //no- 1 is not > 1
if(args.length > 0)
System.out.println(i - 1); // yes 1 is > 0 so print out i - 1
(which is 2-1=1)
else
System.out.println(i - 2);
} //endif
So 1 is correct.


"JavaRanch, where the deer and the Certified play" - David O'Meara
Sasikanth Malladi
Ranch Hand

Joined: Nov 04, 2000
Posts: 175
Thanks, all!
I was under the impression that test and 2 were both the arguments!! 2 is the only argument and test is the class file. My bad.
Thanks once again.
Shashi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: command line arguments