• Post Reply Bookmark Topic Watch Topic
  • New Topic

Checking user input against an array in construction by a loop  RSS feed

 
A Jay Smith
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, im a complete beginner at java and programming in general and I could do with some help after spending the last day bashing my head against a wall. What I am doing is kinda beyond my ability but I hope i'll learn from it.

I was trying to make a lottery checker - take 6 numbers from a user check them against 6( + bonus) from a draw.
So far I can check if the numbers are in range and reject non int values with an exception.

However I can't figure out how to check for user input matching numbers already in the array while im looping through getting the user to put values into it without checking the whole array.

I also kinda know that what I have done so far is insane and that collections(Set) would make this easier, or I could have used regex but I didn't know about them when I started, still don't know much about them and I don't want to use them untill I can do this without. Its the first thing past HelloWorld i've tried so I just wanted to jump in!

My best guess is below.




This works but myMatch goes over everything in the storage array, right?

I gave up using a standard for loop that only goes over the previously entered array indexes because I could not get it to work in any way. Could someone show me how you would go about that?
General shouting at my lunatic typings also appreciated!
 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi A Jay Smith,

Welcome to CodeRanch!

Its good to see that in your very first post, you've used code tags and shown some effort.

Now, coming to your problem:

Whtat I understand is:
1) Accepts a number from user
2) If it is already present in the array, then don't add it again.
3) Otherwise, add it in array.

So, this can easily be achieved using Set (say HashSet etc.). An important property of set is that they don't allow duplicates. And of course, if you want, you can lateron convert the set in an array.

I hope this helps.

Apart from this, I would suggest some changes:

1) In your outer loop, you are iterating for 5. If this is because array length is 5, then don't hard code it in for loop. Use n < userNum.length instead of n<=5. This way, it would be easy to change size of array in future.
2) What is 50? Is it your own constraint? Then again, don't use it directly in code. Declare a final variable and use it everywhere.
3) The array 'order' - you are simply using it for display purpose, so that too, can be made final.
 
A Jay Smith
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your reply Anayonkar Shivalkar.
To your points:

1) The loop is set to <=5 because the size of the array must always be int[6] because you must chose 6 numbers to enter a ticket in the UK lottery, but thanks for the advice which would be handy if I wanted to change the game to one that draws less numbers.
Gonna change that for the sake of commonsense, good habbits and reusability. Thanks.

2) 50 is there because the highest available lottery number to choose in the UK is 49. Why I used that over <=49 for the sake of clarity I don't know! Again thanks for the advice, its worth changing to a getGameLimit method that calls a final int to allow reuse. Thanks again.

3) Doh! Will do that, thanks!

From the brief overview of collection/sets I have read they would make things much easier. I'll check out collections and convert what I've done to use them as I learn about them after finishing this as a sort of learning exercise where I ignore that they exist .

What I am really asking is if there is a better way to check the userNum array with a better loop in the myMatch.assessMatch() method so that I dont check the whole thing each time.

I can't figure out how to do it because I want to start the check after the first number has been entered and only check numbers at positions array[n-1], array[0] and whatever is in between when I check.

 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A Jay Smith wrote:I can't figure out how to do it because I want to start the check after the first number has been entered and only check numbers at positions array[n-1], array[0] and whatever is in between when I check.

Well, as I've already said, Set can help you here. Anyway, for the sake of learning, if you want to do it manually, then you can put another loop which will iterate from 0 to n over current array and check if number exists.
 
A Jay Smith
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, im such an idiot. Works fine since i remembered to put the damn incrementor in the right place. Did that wrong then I went crazy and tried to check it in a for loop that would never run :p.
Thanks for your patience and replies, it helped to know there was nothing wrong in checking it manually in another loop.



 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are welcome.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!