• Post Reply Bookmark Topic Watch Topic
  • New Topic

ArrayIndexOutOfBoundsException  RSS feed

 
John Morgan
Ranch Foreman
Posts: 78
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I get this error when I am trying to ready an array. I know what the error means but no idea as to why it is showing up when I have the variable set to the length of the array.  any suggestions?



Error Code is:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 12
at tutorials.randomDate.getMonth(randomDate.java:39)
at tutorials.randomDate.main(randomDate.java:12)
 
John Morgan
Ranch Foreman
Posts: 78
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Figured one part out changed "int rng = months.length;" to "int rng = (months.length)-1;"

however after running through about 100 times January never showed up. I see no reason why the rng variable should not equal 0.
 
Ron McLeod
Bartender
Posts: 1603
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Morgan wrote:...I see no reason why the rng variable should not equal 0.

Since you are adding 1 to the random number, the result can never be less than 1.

    int rng = rand.nextInt(r) + 1;
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carefully read the documentation for java.util.Random (←click that link).

Random.nextInt(n) will return values that range from 0 to n-1.  When you add 1 to that, you'll adjust the range to 1 to n. 

Java uses 0-based index values for arrays. So the first element in your array, "January", has index of 0. Since you adjusted your values, you've essentially excluded the first element.

You only need to create a Random object once then reuse it for all your nextInt() calls. You can move line 43 outside of any methods and declare rand as a static field instead.

Another thing, the name rng is not a good name. For one thing, it's not pronounceable. Where did your vowels go? It saves you nothing to lose vowels in your variable names. It does cost you a bunch in terms of readability and clarity of your code. So, make your names pronounceable and readable, so your code isn't so cryptic.

More importantly, if "rng" is supposed to be "range", then the name is misleading. The value that it represents is not a range. It's a single number. There's really no need for that method at all. If you make the Random object a static field, then all you have to write in the getMonth() method is rand.nextInt(months.length).  However, if your intent is to make your code more expressive, this version would make more sense:

This way, you can reuse this method to pick out a random element from any String array that you pass to it.
 
John Morgan
Ranch Foreman
Posts: 78
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I actually just saw this as I went to lunch and have it fixed. Thanks for the confirmation though.

Ron McLeod wrote:
John Morgan wrote:...I see no reason why the rng variable should not equal 0.

Since you are adding 1 to the random number, the result can never be less than 1.

    int rng = rand.nextInt(r) + 1;
 
John Morgan
Ranch Foreman
Posts: 78
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am an old school gamer and to me rng (or RNG) has always meant "Random Number Generator" which is why I chose it for that particular variable. 

Junilu Lacar wrote:
Another thing, the name rng is not a good name. For one thing, it's not pronounceable. Where did your vowels go? It saves you nothing to lose vowels in your variable names. It does cost you a bunch in terms of readability and clarity of your code. So, make your names pronounceable and readable, so your code isn't so cryptic.

More importantly, if "rng" is supposed to be "range", then the name is misleading. The value that it represents is not a range. It's a single number. There's really no need for that method at all. If you make the Random object a static field, then all you have to write in the getMonth() method is rand.nextInt(months.length).  However, if your intent is to make your code more expressive, this version would make more sense:
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Morgan wrote:I am an old school gamer and to me rng (or RNG) has always meant "Random Number Generator" which is why I chose it for that particular variable. 

That still makes the name misleading though. The random generator is rand.  If you look at its API, you'll see that it can also generate other things besides random numbers.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And why are you using the == operator on those Strings? That is likely to fail. If it works at all it is because you are working in special circumstances.
Since you have a restricted number of options which can all be specified at compile time, consider using a switch block instead of those multiple ifs.
What does the getDay method do? That isn't obvious from its name. Why are you passing an int argument to it, since the method never uses that int?
 
Liutauras Vilda
Sheriff
Posts: 4918
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, code is short and probably meant to be simple, unfortunately there isn't anything simple with it.

1. The variable names you chose are non-informative: gd, gm, gy, r, y, monthStr (don't include type to a name).

2. Cl nm spe t st wt n ur cs rd -- > RD (confusing, isn't?), what I meant to say is one line below.
2. Class name supposed to start with an upper case randomDate --> RandomDate.

3. Those months you may want to use as enum's from java.time.Month (since Java 1.8)
 
John Morgan
Ranch Foreman
Posts: 78
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are a lot of things in this code that could be shorter or have variables better defined, the idea behind it was to learn how to pass variables from one class to another and return a variable back. I do appreciate the feedback and the hints on changing things to make it simpler but as I am learning I am taking different skills and redoing them in code (even though I know there are shorter ways) to start getting things in grained. I will be shortening them up and being more descriptive in any new code I work on.

Thanks for the insight all.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!