5 Replies - 1328 Views - Last Post: 17 April 2013 - 09:53 AM Rate Topic: -----

#1 vicarcadin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 17-April 13

Problem with Lottery game in Java

Posted 17 April 2013 - 09:08 AM

Hello, I have problem with checking user input in my program, how can I check if user input is in range (1-45) and if input is not repeating. I tried try and catch to trow message when user is trying to enter letters and it works.

I don't know how to check when user input when it is in array. It's better to user input be entered one by one?
Like this?

int num1 = input.nextInt();
int num2 = input.nextInt();
int num3 = input.nextInt();
int num4 = input.nextInt();
int num5 = input.nextInt();



Another problem is when program will throws error, how can user re-enter new numbers?

My code
import java.util.*;

class Lottery {
	
    public static void main ( String[] args ) {
		
		System.out.println("\nWelcome to the Lottery game.");
		System.out.println("You can enter numbers from 1 to 45.");
		
		// User input into an array
		int[] input = new int[6];
		
		Scanner scanner = new Scanner(System.in);
		
			try {
			
				System.out.println("\nPlease enter your 6 lucky numbers: ");
				for(int j = 0; j < 6; j++) {
				
					input[j] = scanner.nextInt();
				}
	
			} catch (InputMismatchException nfe) {
			
				System.out.println("\nERROR: Please enter only numbers!");
			}
			
		// Printing out unique winning numbers from random generator
		System.out.println("\nWinning numbers: ");
		MultiRandomGenerator mrg = new MultiRandomGenerator();
		int[] set;
		
		set = mrg.getSet();
		for (int i = 0; i < set.length; i++) {
			
			System.out.print(set[i] + " ");
		}
		
		// Loops for counting how many numbers user has guessed right
		int count = 0;
		int scount = 0;
		
		for(int i = 0; i < input.length; i++) {
		
			for(int k = 0; k < set.length; k++) {
			
				if (k < 6) {
				
					if (set[k] == input[i]) {
				
						count++;
					} 
				
				} else {
				
					if (set[k] == input[i]) {
				
						scount++;
					}
				
				}
		
			}
		}
		System.out.print("\n\nYou guessed right " + count + " winning numbers.");
		System.out.print("\nYou guessed right " + scount + " suplementary numbers.");
		
		// If statments for printing out winning prizes
		if (count == 6) {
		
			System.out.println("\nYou have won 1st price!");
		} if (count == 5 && scount == 1) {
		
			System.out.println("\nYou have won 2st price!");
		} if (count == 5) {
		
			System.out.println("\nYou have won 3st price!");
		} if (count == 4) {
		
			System.out.println("\nYou have won 4st price!");
		} if (count == 3 && scount == 1) {
		
			System.out.println("\nYou have won 5st price!");
		} if (count == 1 && scount == 2) {
		
			System.out.println("\nYou have won 6st price!");
		} else {
		
			System.out.println("\nSorry, you didn't won anything.");
		}
		
			
	}
	

}


Is This A Good Question/Topic? 0
  • +

Replies To: Problem with Lottery game in Java

#2 jon.kiparsky  Icon User is offline

  • Chinga la migra
  • member icon


Reputation: 10720
  • View blog
  • Posts: 18,353
  • Joined: 19-March 11

Re: Problem with Lottery game in Java

Posted 17 April 2013 - 09:24 AM

Okay starting with the beginning.

View Postvicarcadin, on 17 April 2013 - 11:08 AM, said:

Hello, I have problem with checking user input in my program, how can I check if user input is in range (1-45) and if input is not repeating. I tried try and catch to trow message when user is trying to enter letters and it works.


I don't know how to check when user input when it is in array. It's better to user input be entered one by one?
Like this?

int num1 = input.nextInt();
int num2 = input.nextInt();
int num3 = input.nextInt();
int num4 = input.nextInt();
int num5 = input.nextInt();




No, it's much better to use an array. That way you can loop through the inputs rather than having to hard-code each name that you want to check.

To check integer range, ask "is this number greater than zero and less than or equal to 45?". To check repeats, loop through the array of values you've already received and ask, "is the value I just got equal to this value I've already receieved?"
Was This Post Helpful? 0
  • +
  • -

#3 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 203
  • View blog
  • Posts: 1,731
  • Joined: 13-March 10

Re: Problem with Lottery game in Java

Posted 17 April 2013 - 09:25 AM

You could just create a helper method like this:

private static boolean isValid(int number){
        return number > 1 && number < 45;
}


and then use it in the if() statement.
Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3464
  • View blog
  • Posts: 15,676
  • Joined: 20-September 08

Re: Problem with Lottery game in Java

Posted 17 April 2013 - 09:27 AM

I would use Set<Integer> to check for duplicates. On a given input (1-6), if set.size() != input then the last entered number was a duplicate.

As for validating input, it's easier to read a String and then check input.matches("\\d+")
Was This Post Helpful? 0
  • +
  • -

#5 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 203
  • View blog
  • Posts: 1,731
  • Joined: 13-March 10

Re: Problem with Lottery game in Java

Posted 17 April 2013 - 09:30 AM

View Postg00se, on 17 April 2013 - 09:27 AM, said:

I would use Set<Integer> to check for duplicates. On a given input (1-6), if set.size() != input then the last entered number was a duplicate.


The add() method returns a boolean so it could be used instead of the set.size() != input.
Was This Post Helpful? 0
  • +
  • -

#6 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3464
  • View blog
  • Posts: 15,676
  • Joined: 20-September 08

Re: Problem with Lottery game in Java

Posted 17 April 2013 - 09:53 AM

Quote

The add() method returns a boolean so it could be used instead of the set.size() != input.
Good point!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1