• Post Reply Bookmark Topic Watch Topic
  • New Topic

command line array args  RSS feed

 
kennith stomps
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This code is from a tutorial online, why is it that while you are adding the args array, it adds args[0] + args[2] ?? Shouldn't it be args[0]  + args[1]???

 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
args[1] is the operator. See line 15.
 
kennith stomps
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No what is being added is arg[0] and arg[2]

case '+': result = Integer.parseInt(args[0]) +
                         Integer.parseInt(args[2]);
 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kennith stomps wrote:No what is being added is arg[0] and arg[2]

case '+': result = Integer.parseInt(args[0]) +
                         Integer.parseInt(args[2]);


Correct. [0] is the first operand, and [2] is the second operand, and [1] is the operator.
So if you enter
programname 10 + 20
then arg[0] is "10"
arg[1] is "+"
arg[2] is "20"

or more precisely
java Calculator 10 + 20
 
kennith stomps
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That makes more sense, thank you.
 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kennith stomps wrote:That makes more sense, thank you.

Glad to be of help.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!