Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • 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:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

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: 2907
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!