This week's book giveaway is in the Agile forum.
We're giving away four copies of Head First Agile and have Andrew Stellman & Jennifer Greene on-line!
See this thread for details.
Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

ERROR-java.lang.ArrayIndexOutOfBoundsException  RSS feed

 
Calvin Wong
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want write a small app to generate random numbers in specified numbers, the code as follow:



can compile it and run, when running it raised an error: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException, I scratch out all my hair and Googled for hours, no solutions I can find.

Anyone can help me ? Thanks
 
Dylan Black
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An ArrayIndexOutOfBoundsException is when you reference an index of an array that is out of bounds. For example:



In your code, you are referencing lottery's index out of bounds. I'll show you where, and let you figure out how to debug it. HINT: use the .length() method. If that shows error, get rid of () and vice versa.

 
Ganesh Patekar
Bartender
Posts: 696
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to CodeRanch!   Thank you for posting code in code tag.

  • Remember array index always starts from 0 so minimum index is 0 and maximum index is the number of elements in array - 1.
  • ArrayIndexOutOfBoundsException occurs when you try to access an index of an array which is either less than 0 Or greater than number of elements in array Or can say size of array -1.
  • In your example size of lottery is lottery.length = 2 means minimum and maximum index of lottery array aka bounds of that array is 0 to 1. If you try to access element at index 2 like lottery[2] then it give runtime exception saying ArrayIndexOutOfBoundsException.


  • In below loop value of i can be 0, 1, 2
  • But you know index range of lottery can only be 0 to 1 as we know max index of lotterry array is lottery.length - 1 i.e. 2 - 1 means min 0 and max 1 bounds only. When i becomes 2 and try to assign randomNum at below line It is nothing but lottery[2] = randomNum; where index 2 of lottery doesn't exists which goes out of max bound of lottery i.e. 1 so give ArrayIndexOutOfBoundsException at run time.

  • In below code at line no 15 and 21, you are creating an object of Random each time loop is executed, better create It once out side of loop and use that reference variable of Random to generate numbers.


  • I don't see any use of array arr more than just passing value of size of array arr i.e. 5 to nextInt(int bound) method of Random.
  • You can replace It with  

  • I assume you know what this code generates
  • It is nothing but below line of code which generates int value between 0 (inclusive) and the specified value (exclusive) i.e. int value between 0 to 4

  • Don't write that much code in main method, you can create a separate method named getRandomNumbers() or anything you think more expressive and put this code in that method. Just invoke that method from main method.
  • Worth reading click here -->Main Is A Pain and here also --> Java Programming Style Guide
  • Class name l explains nothing you can rename It like RandomNumber etc. Class name Always starts with capital letter and followed by CamelCase.
  • I'm a bit skeptical, there may be another problem in code will let you know once you make changes I think you need to
  •  
    Calvin Wong
    Greenhorn
    Posts: 2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks both

    I got it and solved, I will always evaluate this lesson.

    BTW, I want learn one language from scratch, C# or JAVA, which is better for me ?

    My final goal is to fetch data from web.

    Also, where I can find good resources ?

    Thanks again
     
    Ganesh Patekar
    Bartender
    Posts: 696
    23
    Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
  • The value of int bound in nextInt(int bound) method of Random should be equal to the number of times i loop executes else your intenal x loop will execute infinite time.
  • How?
  • Suppose your outer loop i executes 5 times means It is and you have value of int bound less than 5, assume 4  i.e.  .nextInt(4); means this method can generate values 0, 1, 2, 3 so total 4 unique numbers But you want to generate 5 unique values (No duplicates) whose code you wrote in inner x loop so It is not possible as already mentioned nextInt(4) generates only 0, 1, 2, 3 so inner x loop will execute infinite times just to get next 5th unique number which is not possible.

    Regarding your next question, I think other members of this forum may answer you better than me and yes, you are welcome   
     
    Knute Snortum
    Sheriff
    Posts: 4091
    112
    Chrome Eclipse IDE Java Postgres Database VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Calvin Wong wrote:
    BTW, I want learn one language from scratch, C# or JAVA, which is better for me ?

    My final goal is to fetch data from web.

    Also, where I can find good resources ?

    We could probably start a new thread with these questions, but I would say Java is better just because it will run in a lot of different environments.  If you're just receiving data from a web service then there's probably not much difference between C# and Java, but if you're writing a web app, Java has the edge.

    The Oracle Tutorials are probably a good start.  Then there's books like Head First Java.  It's old, but it gives you a good start if you don't mind the teaching method.  You could then try Learning Java, then Java 8 in Action.
     
    Dylan Black
    Greenhorn
    Posts: 5
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Calvin Wong wrote:
    BTW, I want learn one language from scratch, C# or JAVA, which is better for me ?

    My final goal is to fetch data from web.


    If you want to fetch data from the internet, scratch in my opinion would not be a wise choice, as the programming language is very limited. When it comes to C# or Java, I would say learn the fundamentals first in java, then learn C#, C++, C, etc.

    Calvin Wong wrote:
    Also, where I can find good resources?


    There are many resources on the internet such as oracles tutorials. You can also buy books on java or possibly borrow them from your local library. Good Luck!
     
    Paul Clapham
    Sheriff
    Posts: 22531
    43
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Dylan Black wrote:There are many resources on the internet such as oracles tutorials. You can also buy books on java or possibly borrow them from your local library.


    Very true. But about the library idea: My city's public library, which is very well stocked, does have books about a wide variety of computer topics. BUT... they do tend to be 8 or 10 years old. That makes them pretty useless, since most computer topics seem to have a 5-year turnover cycle later. Including Java as an example. So when you look for library books, make sure to check the publication date and don't settle for obsolete books.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!