Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Why can't I add 1 + 1?  RSS feed

 
Tim Dee
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I�m VERY new to java and am trying to write a simple �Add two integers together�. I want to be able to call the class from a DOS box and give it two numbers which it then adds and returns the result.
I have written the following:



When I type:
Java Tadd 1 1
I would expect the two strings to be passed to the prog, converted to integers then added together and the result printed out.

However it returns the following
Exception in thread �main� java.lang.ArrayIndexOutOfBoundsException: 2 at Tadd.main(Tadd.java:4)

Why is this?

MNany thanks for any help.
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java arrays have zero based indexes. So your two arguments are args[0] and args[1].
 
Tim Dee
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perfect. Thank you
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



Couldn't resist

Bu.
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From dictionary.com
in‧dex  /ˈɪndɛks/ plural -dex‧es, -di‧ces /-dəˌsiz/
�noun
1.(in a nonfiction book, monograph, etc.) a more or less detailed alphabetical listing ...



[ September 20, 2006: Message edited by: Garrett Rowe ]
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
indexes == indices
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!