Jump to content

Java: Random numbers

Go to solution Solved by MrMG,

When you create an array in java you have to say how long it is in the beginning. You do that with the following line:

arrayNumber = new int[number];

This code is correct. The problem here is that there is no value currently stored inside of "number".

If you look at this line:

int number, randomNumber;

You create the variables but you never say what is stored inside of them. Normally Java would just put in a default value of 0. But because you are creating them in the main method (and because you should really always say what the value in a variable should be no matter where in your code), Java instead assumes that you forgot to give the variable a value and throws this error that you are getting (as far as I know).

 

So to fix your issue, you need to tell the program exactly what value is stored inside of the variable "number". In your case this should work just fine:

number = 10;

Just make sure you add this line into your code before the line where you create your array.

Hey guys,

 

I am not sure how to get this code done.

The goal is to type in 10 numbers and get a random number out of those as an output.

 

import javax.swing.*;
import java.util.*;

public class Aufgabe51
{
	public static void main(String[] args)
	{

		int[] arrayNumber;
		int number, randomNumber;
		Scanner input;
		Random random;
		
	
		arrayNumber = new int[number];
		input = new Scanner(System.in);
		random = new Random();
		
		
		System.out.println ("Type in 10 numbers.");
		
		
		for(int i = 0; i < 10; i++)
		{
			System.out.println ("Type in one number.");
			arrayNumber[i] = input.nextInt();
		}
		

		randomNumber = random.nextInt(arrayNumber.length);
		arrayNumber[randomNumber];
		
	
		System.out.println ("The random number is: " + (arrayNumber[randomNumber]));
		System.exit(0);
	}
}

 

Link to comment
https://linustechtips.com/topic/1041833-java-random-numbers/
Share on other sites

Link to post
Share on other sites

What's your issue with the code. I don't know Java well enough to understand but from my understanding that should work. Although one issue is that arrayNumber.length should return 10, where I think your array is only 0-9 (Idk if Java indexing starts at 0 or 1, if it starts at 1 ignore this).

Current Desktop Build | 2200G | RX 580 4GB | 8GB RAM | CTRL | Logitech G Pro Wireless

Laptop | 2018 MBA 256/16GB | MX Master 

Link to comment
https://linustechtips.com/topic/1041833-java-random-numbers/#findComment-12370429
Share on other sites

Link to post
Share on other sites

3 minutes ago, zlolslavez said:

What's your issue with the code. I don't know Java well enough to understand but from my understanding that should work. Although one issue is that arrayNumber.length should return 10, where I think your array is only 0-9 (Idk if Java indexing starts at 0 or 1, if it starts at 1 ignore this).

Yes, Java index starts at 0.

 

My problem is that I do not get any output. 

I only get the error:

 

error: not a statement
                arrayNumber[randomnumber];

 

 

Link to comment
https://linustechtips.com/topic/1041833-java-random-numbers/#findComment-12370437
Share on other sites

Link to post
Share on other sites

Just now, Hip said:

My problem is that I do not get any output. 

I only get the error:

 

error: not a statement
                arrayNumber[randomnumber];

 

 

Oh, just delete that line that doesn't do anything I think.

Current Desktop Build | 2200G | RX 580 4GB | 8GB RAM | CTRL | Logitech G Pro Wireless

Laptop | 2018 MBA 256/16GB | MX Master 

Link to comment
https://linustechtips.com/topic/1041833-java-random-numbers/#findComment-12370445
Share on other sites

Link to post
Share on other sites

1 minute ago, MrMG said:

you have declared a variable number as an int, but you have never said what the value of that variable is. Then you use it to create an array. But there is no number stored in the variable. That is why you get that error message.

Ah I see!! But how can I solve this now? I stored the numbers in arrayNumber.

Link to comment
https://linustechtips.com/topic/1041833-java-random-numbers/#findComment-12370461
Share on other sites

Link to post
Share on other sites

You just need to say somewhere before the line:

arrayNumber = new int[number];

what the value of number is. You could write

number = 10;
arrayNumber = new int[number];

or you could ask the user to tell you how many elements you want in your array using the scanner.

 

Something else I noticed is that you use the number "10" in your for loop. That is not really a good idea. Instead you should consider using arrayNumber.length like this:

for(int i = 0; i < arrayNumber.length; i++)
		{
			System.out.println ("Type in one number.");
			arrayNumber[i] = input.nextInt();
		}

Like this, if you change the size of your array later, your code will adapt to the changes automatically.

Link to comment
https://linustechtips.com/topic/1041833-java-random-numbers/#findComment-12370488
Share on other sites

Link to post
Share on other sites

Atleast one problem is that you have a line where you declare arrayNumber[randomNumber] but never assign it to anything. It's just sitting there doing nothing by itself.

 

Delete the line above your println() statement and see what happens.

 

Additionally, you try to use number before assigning anything to it.

ENCRYPTION IS NOT A CRIME

Link to comment
https://linustechtips.com/topic/1041833-java-random-numbers/#findComment-12370519
Share on other sites

Link to post
Share on other sites

24 minutes ago, MrMG said:

you could ask the user to tell you how many elements you want in your array using the scanner.

 

Something else I noticed is that you use the number "10" in your for loop. That is not really a good idea. Instead you should consider using arrayNumber.length like this:


for(int i = 0; i < arrayNumber.length; i++)
		{
			System.out.println ("Type in one number.");
			arrayNumber[i] = input.nextInt();
		}

Like this, if you change the size of your array later, your code will adapt to the changes automatically.

I want to do this in the for loop because the user should not be able to decide how many numbers he wants to add. It should be 10.

 

 

@straight_stewie

When I delete  arrayNumber[randomNumber]; I just get an error variable "number" might not have been initialized

Link to comment
https://linustechtips.com/topic/1041833-java-random-numbers/#findComment-12370557
Share on other sites

Link to post
Share on other sites

1 minute ago, Hip said:

I want to do this in the for loop because the user should not be able to decide how many numbers he wants to add. It should be 10. 

That is fine, but you should still do it the way I wrote. Just don't use a scanner for the size of the array.

 

There are many reasons why you should avoid "magic numbers" in your code. Magic numbers make your code harder to understand for other people (or yourself if you look at your code after a few months). It makes it easier to introduce bugs to your code, because every time you change the length of your array you also have to change the number in the for loop. In your case it is fine because your code isn't that long, but once you start working on bigger projects this can become a real problem, believe me I have learned it the hard way :) .

Link to comment
https://linustechtips.com/topic/1041833-java-random-numbers/#findComment-12370583
Share on other sites

Link to post
Share on other sites

7 minutes ago, MrMG said:

That is fine, but you should still do it the way I wrote. Just don't use a scanner for the size of the array.

 

There are many reasons why you should avoid "magic numbers" in your code. Magic numbers make your code harder to understand for other people (or yourself if you look at your code after a few months). It makes it easier to introduce bugs to your code, because every time you change the length of your array you also have to change the number in the for loop. In your case it is fine because your code isn't that long, but once you start working on bigger projects this can become a real problem, believe me I have learned it the hard way :) .

Yeah I just want to try it out this way with the scanner because I have not worked with scanner yet, so I want to practise this. Can you tell me how to solve this with the variable numbers? I'm not sure what to do now.

Would number = 10; be enough for that?

Link to comment
https://linustechtips.com/topic/1041833-java-random-numbers/#findComment-12370596
Share on other sites

Link to post
Share on other sites

When you create an array in java you have to say how long it is in the beginning. You do that with the following line:

arrayNumber = new int[number];

This code is correct. The problem here is that there is no value currently stored inside of "number".

If you look at this line:

int number, randomNumber;

You create the variables but you never say what is stored inside of them. Normally Java would just put in a default value of 0. But because you are creating them in the main method (and because you should really always say what the value in a variable should be no matter where in your code), Java instead assumes that you forgot to give the variable a value and throws this error that you are getting (as far as I know).

 

So to fix your issue, you need to tell the program exactly what value is stored inside of the variable "number". In your case this should work just fine:

number = 10;

Just make sure you add this line into your code before the line where you create your array.

Link to comment
https://linustechtips.com/topic/1041833-java-random-numbers/#findComment-12370639
Share on other sites

Link to post
Share on other sites

18 minutes ago, MrMG said:

When you create an array in java you have to say how long it is in the beginning. You do that with the following line:


arrayNumber = new int[number];

This code is correct. The problem here is that there is no value currently stored inside of "number".

If you look at this line:


int number, randomNumber;

You create the variables but you never say what is stored inside of them. Normally Java would just put in a default value of 0. But because you are creating them in the main method (and because you should really always say what the value in a variable should be no matter where in your code), Java instead assumes that you forgot to give the variable a value and throws this error that you are getting (as far as I know).

 

So to fix your issue, you need to tell the program exactly what value is stored inside of the variable "number". In your case this should work just fine:


number = 10;

Just make sure you add this line into your code before the line where you create your array.

Thanks for your help! 

Is there also any way to solve this without telling number = 10; and instead of that the loop value 10 decides how big the arrayNumber is? I'm curious.

 

Also thanks to  @straight_stewie It works now.

 

Final solution:

public class Aufgabe51
{
	public static void main(String[] args)
	{

		int[] arrayNumber;
		int number, randomNumber;
		Scanner input;
		Random random;
		
		number = 10;
		arrayNumber = new int[number];
		input = new Scanner(System.in);
		random = new Random();
		
		
		System.out.println ("Type in 10 numbers.");
		
		
		for(int i = 0; i < 10; i++)
		{
			System.out.println ("Type in one number.");
			arrayNumber[i] = input.nextInt();
		}
		

		randomNumber = random.nextInt(arrayNumber.length);
		arrayNumber[randomNumber];
		
	
		System.out.println ("The random number is: " + (arrayNumber[randomNumber]));
		System.exit(0);
	}
}

 

Link to comment
https://linustechtips.com/topic/1041833-java-random-numbers/#findComment-12370715
Share on other sites

Link to post
Share on other sites

Not as far as I know. In Java you always have to specify the size of the array before you can use it. Alternatively you could of course use a List instead, like ArrayLists: https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html .

Your code then may look like this:

 public static void main(String[] args)
    {

        ArrayList<Integer> arrayNumber;
        int randomNumber;
        Scanner input;
        Random random;


        arrayNumber = new ArrayList<>();
        input = new Scanner(System.in);
        random = new Random();


        System.out.println ("Type in 10 numbers.");


        for(int i = 0; i < 10; i++)
        {
            System.out.println ("Type in one number.");
            arrayNumber.add(input.nextInt());
        }


        randomNumber = random.nextInt(arrayNumber.size());


        System.out.println ("The random number is: " + (arrayNumber.get(randomNumber)));
    }

Of course you also have to add

import java.util.ArrayList;

above your class

Link to comment
https://linustechtips.com/topic/1041833-java-random-numbers/#findComment-12370721
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×