So hello everyone! This is my school project but I'm having a problem in my code. So here is the program i want to create. I want to create a program that can generate 5 numbers from 1-9 then user will guess it. My problem only is on the generating some unique numbers. My point in generating numbers are like there shouldn't be have a same number in generating it. EG.:"1 ,5 ,3 ,2, 6" this is correct while E.G:"2, 5, 4, 3, 2" is wrong. So i already have a code. please help me to fix/build it! Thanks alot.

import java.util.ArrayList;
 import java.util.Arrays;

     ArrayList<Integer> numbers = new ArrayList<Integer>();
for( a = 0; a < 9; a++)
           {
                numbers.add(a+1);
            }   
            Collections.shuffle(numbers);
            number1= numbers.get(0) ;
            number2= numbers.get(1);
            number3= numbers.get(2);
            number4= numbers.get(3);
            number5= numbers.get(4);

Please help me. THANKS

Use the Random() to generate numbers from 1 to 9 then check if the number has already been used

Use the Random() to generate numbers from 1 to 9 then check if the number has already been used

But how? I already try to use the random generator but still it gets the save number.

Here's the code i use:

import java.io.*;
import java.util.Random;
    public class GuessingGame
    {
        public static void main (String args[]) throws IOException
        {
            //Coded By ©Jonaii DO NOT RIP SHIITT..
            BufferedReader guess= new BufferedReader (new InputStreamReader(System.in));
            Random generator= new Random();
            double a,b,c,d,e;
            int x, bulls, eye, p, n, number1, number2 , number3, number4, number5;
            String name;
            x=1;
            bulls=0;
            eye=0;
            number1= 1+generator.nextInt(9);
            number2= 1+generator.nextInt(9);
            number3= 1+generator.nextInt(9);
            number4= 1+generator.nextInt(9);
            number5= 1+generator.nextInt(9);
            System.out.print(number1);
            System.out.print("\n"+number2);
            System.out.print("\n"+number3);
            System.out.print("\n"+number4);
            System.out.print("\n"+number5);
            
            System.out.print("\nEnter your 1st guess: ");
            name= guess.readLine();
            a= Double.parseDouble(name);
            System.out.print("\nEnter your 2nd guess: ");
            name= guess.readLine();
            b= Double.parseDouble(name);
            
            System.out.print("\nEnter your 3rd guess: ");
            name= guess.readLine();
            c= Double.parseDouble(name);
            
            System.out.print("\nEnter your 4th guess: ");
            name= guess.readLine();
            d= Double.parseDouble(name);
            
            System.out.print("\nEnter your 5th guess: ");
            name= guess.readLine();
            e= Double.parseDouble(name);
           
            
            
            if(a==number1)
            {
               bulls=bulls+1;
            }
             else if((a==number2) && (a==number3) && (a==number4) && (a==number5))
            {
                eye=eye+1;
            }
            if(b==number2)
            {
               bulls=bulls+1;
            }
            else if((b==number1) && (b==number3) && (b==number4) && (b==number5))
            {
                eye=eye+1;
            }
            if(c==number3)
            {
               bulls=bulls+1;
            }
            else if((c==number1) && (c==number2) && (c==number4) && (c==number5))
            {
                eye=eye+1;
            }
            if(d==number4)
            {
               bulls=bulls+1;
            }
            else if((d==number1) && (d==number2) && (d==number3) && (d==number5))
            {
                eye=eye+1;
            }
            if(e==number5)
            {
               bulls=bulls+1;
            }
            else if((e==number1) && (e==number2) && (e==number3) && (e==number4))
            {
                eye=eye+1;
            }
           
            
            
            
            
            
            
           
            
            System.out.print("\nBulls: "+ bulls);
            System.out.print("\nEye: "+ eye);
            
        }
    }

My point in generating numbers are like there shouldn't be have a same number in generating it

You can use an if statement or a loop to check if the number has already been used

number1= 1+generator.nextInt(9);
            number2= 1+generator.nextInt(9);
            while(number2==number1){ //simple sample only
            number2= 1+generator.nextInt(9); //assign another value
            }

note: there are a lot of ways to achieving this try experimenting on your own ;)

But how? I already try to use the random generator but still it gets the save number.

I'm not sure what you mean here can you be a bit more specific at what part of the code?

You can use an if statement or a loop to check if the number has already been used

number1= 1+generator.nextInt(9);
            number2= 1+generator.nextInt(9);
            while(number2==number1){ //simple sample only
            number2= 1+generator.nextInt(9); //assign another value
            }

note: there are a lot of ways to achieving this try experimenting on your own ;)


I'm not sure what you mean here can you be a bit more specific at what part of the code?

Sorry typo error. Same**.

What was wrong with the logic and technique in the first code you posted?

As NormR1 said, as far as i can see the first code you posted to start the thread was just fine? and the reason is look at what the shuffle methods definition is:"Randomly permutes the specified list using a default source of randomness. All permutations occur with approximately equal likelihood.
The hedge "approximately" is used in the foregoing description because default source of randomenss is only approximately an unbiased source of independently chosen bits. If it were a perfect source of randomly chosen bits, then the algorithm would choose permutations with perfect uniformity".

unless you ment the 5 numbers that must be randomly generated is whats giving you problems?

which i'd suggest why not just create a list of numbers 1-9, shuffle them, and then only use the first 5 numbers of the list to add to the correct answers array which will be checked if the user was able to guess them or not?(therefore there will be no duplicates and much less extra coding-shortest method is the best :))

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.