File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

K&B Self Test Chap 5 Question 4 - arguments

 
Andreas Auer
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

can someone please explain why there is/should be a different output using:

$java Input
and
$java Input 0

Why is the 2nd argument considered? I can only see 'args[0]' in code.
(As I don't know if I'm allowed to copy the whole question, I'll skip this until someone tells me anything different.

Thanks in Advance,
Andreas
_____________
Update:

class Input {
public static void main(String[] args) {
String s = "-";
try {
doMath(args[0]);
s += "t "; // line 6
}
finally { System.out.println(s += "f "); }
}

public static void doMath(String a) {
int y = 7 / Integer.parseInt(a);
}
}

command-line invocations:
java Input
java Input 0

Answer is "Each invocation produces a different exception."

Why???
[ July 18, 2008: Message edited by: Andreas Auer ]
 
Sagar Rohankar
Ranch Hand
Posts: 2905
1
Java Spring Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
$java Input
and
$java Input 0

Why is the 2nd argument considered? I can only see 'args[0]' in code.


You can see the 'args[0]' , that means you are displaying the first command line argument !
What you want to convey us ? Pl elaborate it .

Post your code here .
[ July 18, 2008: Message edited by: Sagar Rohankar ]
 
Raphael Rabadan
Ranch Hand
Posts: 141
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Andreas Auer:
Hi guys,

can someone please explain why there is/should be a different output using:

$java Input
and
$java Input 0

Why is the 2nd argument considered? I can only see 'args[0]' in code.
(As I don't know if I'm allowed to copy the whole question, I'll skip this until someone tells me anything different.

Thanks in Advance,
Andreas
_____________
Update:

class Input {
public static void main(String[] args) {
String s = "-";
try {
doMath(args[0]);
s += "t "; // line 6
}
finally { System.out.println(s += "f "); }
}

public static void doMath(String a) {
int y = 7 / Integer.parseInt(a);
}
}

command-line invocations:
java Input
java Input 0

Answer is "Each invocation produces a different exception."

Why???

[ July 18, 2008: Message edited by: Andreas Auer ]


Hello,

The 1st one will try to use the args[0] but it does not exists. So you'll get an ArrayIndexOutOfBoundsException.

In the 2nd case you'll get an ArithmeticException saying divison by 0 is not permited.

Raphael Rabadan.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic