• 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:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

First Time with Arrays  RSS feed

 
Greenhorn
Posts: 13
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, I have to write a program for a course and I've been beating my head against the keyboard trying to figure out what I'm doing wrong. I apologize in advance for the lengthy post, but I'm sure this is something simple and it's time ot call in some Pro-Level help as, with Finals week approaching, I cannot have one bug taking me down for more than the 3 hours it already has.

The program is: create a class that creates lottery drawing objects with methods that will check user number picks against a randomly generated 5 digit "drawing" and report back how many numbers are a match.

Simple enough, right? Somehow I've made a mess of it.

Here is my class: (I will note, I primarily use JOPtionPane for user interaction as I just like the interface better, so anywhere you see "System.out.print" its my effort to see whats going on and debug. Disregard this.(I'm leaving it in case you can explain what I'm seeing with that output because I cannot figure it out)



And here is my Demo Program:



The errors I am currently fighting with:
1) Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 2 out of bounds for length 1
at RobinsonC_Lottery.matchedNumbers(RobinsonC_Lottery.java:71)
at RobinsonC_LotteryDemo.main(RobinsonC_LotteryDemo.java:39)

This one has me stumped. I read through my matchedNumbers() method out loud in the class and it sounds like this:

"FOR instances of this loop where int variable index that has been initialized to zero are evaluated to be less than the length of the lotteryDrawing array:
IF LotteryDrawing array's element value at subscript position matching index2's value exactly matches playerPicks arrays element value at the same subscript position:
increment "matches" variable 1X. Increment index2 - new iteration of For Loop. (repeat)

The intent here is for the index 2 variable to increment concurrently for both arrays, stepping through the arrays concurrently and if theres a match, then increment matches.

Then, "matches" will be used to set the size of the matchedNumbers array, then another for loop should record the matching numbers in that array for return to Main Method.

I know an ArrayList will automatically expand but that was not listed as a part of the assignment so I can't use that. I've been docked before for including things we haven't been taught and/or the assignment doesnt ask for. I need to use the practices here properly to demonstrate an understanding of the basics and after 3 hours of grinding on this, I'm stumped.

I dropped all these System output lines in to see whats going on and I cannot figure out why index seems to be incrementing more times for lotteryDrawing than playerPicks.

Here's the System Output I get.

Constructor index value-1
Constructor index value-2
Constructor index value-3
Constructor index value-4
Constructor index value-5
Before FOR - lotterydraw length - 5
1--FOR -lotteryDrawing index-3-FOR -playerPicks-
0- FOR -matches
6--FOR -lotteryDrawing index-3-FOR -playerPicks-
0- FOR -matches
1--FOR -lotteryDrawing index-3-FOR -playerPicks-
0- FOR -matches
4--FOR -lotteryDrawing index-3-FOR -playerPicks-
0- FOR -matches
8--FOR -lotteryDrawing index-3-FOR -playerPicks-
0- FOR -matches
0-matchedNumbers length
Before FOR - lotterydraw length - 5
1--FOR -lotteryDrawing index-3-FOR -playerPicks-
0- FOR -matches
6--FOR -lotteryDrawing index-3-FOR -playerPicks-
0- FOR -matches
1--FOR -lotteryDrawing index-3-FOR -playerPicks-
0- FOR -matches
4--FOR -lotteryDrawing index-3-FOR -playerPicks-
0- FOR -matches
8--FOR -lotteryDrawing index-3-FOR -playerPicks-
0- FOR -matches
0-matchedNumbers length
0-main
Before FOR - lotterydraw length - 5
1--FOR -lotteryDrawing index-3-FOR -playerPicks-
0- FOR -matches
6--FOR -lotteryDrawing index-3-FOR -playerPicks-
0- FOR -matches
1--FOR -lotteryDrawing index-3-FOR -playerPicks-
0- FOR -matches
4--FOR -lotteryDrawing index-3-FOR -playerPicks-
0- FOR -matches
8--FOR -lotteryDrawing index-3-FOR -playerPicks-
0- FOR -matches
0-matchedNumbers length




2) When it matches ANY of the numbers it reports a grand prize winner - Looking at my Demo program, I feel like I pretty clearly outline the else clause in the if statement that controls this so I believe problem 1) is whats contributing to problem 2)

You might ask why I use standard FOR loops and not enhanced ones, and  this may be contributing to my issue: The answer is because I sort of understand the enhanced for, but I'm not really getting where I would use the new int variable in these circumstances where I am comparing values to each other, and the chapter in the text explained B before A, so I haven't been able to break that down.

If anyone can help me clear the roadblock I would appreciate it. As always, any otherwise critiques are also welcome but please list separately from suggestions on this solution because I copy and paste them into a separate file of "Get better at this stuff" lol.

As far as comments, I get graded on how concise they are and complete so I cant just jot down kinda what I think it's doing because I'll  just have to go back later and retype them all - that drives this bad habit of not having any at rough draft.

Thanks!


EDIT: I figured this out after walking away for a while:

1) each reference to the method in the main program (for length or for toString the return array) executed the method, incrementing the "matches" variable in the method.

2) This was an issue:


First off, the index variables throughout are local to their methods and wont impact each other. I thought they were, because I was watching the weird value change activity due to number 1) above. Then, having the matchedNumbers index match that of 2 much longer arrays will not work.

I couldn't figure out how to remove the post, so at least if it's going to stay, it will have a solution.
Staff note (Knute Snortum):

Please don't edit your original post with new information. Instead, post a reply.

 
Marshal
Posts: 64179
215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suggest you should start by formatting your code correctly, with the right indentation and breaking the long lines. As it is, your code is hard to follow.
Make sure to initialise every field in every constructor. Otherwise you are risking incorrect results and exceptions of a different kind.
Don't mix a for‑each loop (=enhanced for) and indices. If you need indices, you want a plain for loop. Regard a for‑each loop as read‑only.
You are correctly drawing “random” numbers, but you don't usually use random numbers for a lottery; you usually use random selection from a reducing population. I haven't got the time to explain how to do that. Sorry.
The exception message you are showing is because you are using an index too large for the size of array. If you have a five‑element array, it has indices 0 1 2 3 and 4. Make sure your arrays are the same size. Beware of variables with the same name because you can confuse the two (line 6 and line 11).
Where do you get matchedNumbers.length from? The size of an array does not tell you how many elements have been correctly assigned; that doesn't count correct guesses or anything like that.
I usually fulminate about parallel arrays, but that is probably a reasonable solution for lottery numbers. If you are simply testing whether the numbers at index i are the same, you have to have the same numbers in the same order.
Don't say you are accessing them concurrently, because you are only using one thread.
 
Your buns are mine! But you can have this tiny ad:
Create Edit Print & Convert PDF Using Free API with Java
https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!