• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

K&B Self Test Chap 5 Question 4 - arguments  RSS feed

 
Greenhorn
Posts: 4
  • 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 ]
 
Ranch Hand
Posts: 2908
1
Java Spring Ubuntu
  • 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 ]
 
Ranch Hand
Posts: 141
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!