Member Avatar for naomi_01

Hi, I'm trying to have a set of random numbers into ArrayList and to print it.
e.g. if the total number is 10, [2,3,5] or [1,5,2,2], etc.
In below, it gives me each numbers but because y gets zero, it throws an exception. Can I have help to print all array elements for below ? tks, naomi

import java.io.*;
import java.util.*;

public class Dice {
	public static void main(String[] args) throws FileNotFoundException {
		int x;
		int y;
		x = 0;
		y = 50;
		x = y - x;
		ArrayList<Integer> list = new ArrayList<Integer>();
		
		for (x = x; x > 0; x++) {

			Scanner user = new Scanner(System.in);
			Random r = new Random();

			int roll;
			int diff;
		

			roll = r.nextInt(y) + 1;

			diff = 0;

			for (int n : list) {
				x = diff;
			}

			x = y - roll;

			System.out.println(+roll);
			System.out.println(+diff);
			System.out.println("x=" + x);
			System.out.println("y=" + y);

			list.add(roll);
		
			if (y>0){
				y=x;}
			else y=1;
		
				
		}System.out.println(list);
		

	}
}

What exception on which line?

Member Avatar for naomi_01

Hi James, tks for your help, I get below results when I run it.

1
0
x=9
y=10
8
0
x=1
y=9
1
0
x=0
y=1
Exception in thread "main" java.lang.IllegalArgumentException: n must be positive
at java.util.Random.nextInt(Unknown Source)
at Dice.main(Dice.java:22)

Yes the

r.nextInt(y)

in line 22 expects a positive value. However shouldnt the condition

if (y>0){
y=x;}
else y=1;

be changed as

if (x>0){
y=x;}
else y=1;

Because I think that is what you are trying to do. Not to make y a negative value

Member Avatar for naomi_01

tks for reply, but it went into the infinite loop...

Yes your copnditons in the for loop says

for (x = x; x > 0; x++)

also I strongly advice you to change the variable x in for loop as it shadows the variable used before.

Coming to the point you say ru the loop as long as x is greater than 0 and you increment x by 1 which definitely would take yo to infinite loop. Think of the condition as to how many times the loop shhould run and change the condition accordingly. Also change the variable name as advised because it confuses as to what you are really trying to do, whether you are considering x as the variable declared in method level or the variable used for the loop level. On changing the vaariable you will get a clear picture on what to do....

Member Avatar for naomi_01

Yes your copnditons in the for loop says

for (x = x; x > 0; x++)

also I strongly advice you to change the variable x in for loop as it shadows the variable used before.

Coming to the point you say ru the loop as long as x is greater than 0 and you increment x by 1 which definitely would take yo to infinite loop. Think of the condition as to how many times the loop shhould run and change the condition accordingly. Also change the variable name as advised because it confuses as to what you are really trying to do, whether you are considering x as the variable declared in method level or the variable used for the loop level. On changing the vaariable you will get a clear picture on what to do....

Thank you Zaad, but sorry what do you mean by change variable x in for loop ? I'm sorry but I'm very novice. appreciate a lot. Naomi

The thing is that you have declared a variable x in line 6 and you are usng the same variable as an increment in the for loop.

And in line 10 you have assigned the value of x as

x=y-x

which means x becomes 50.

The problem that you stated regarding going for an infinite loop is here. Because you have stated the condition in line 13 as

for (x = x; x > 0; x++)

This means loop for the value of x (which is 50) as long as x > 0 and in each iteration of the loop increment the value of x by 1.

Which is like the value of x will always be greater than 0 and the loop would never end.

That is what I tried to point out for the problem you having the infinite loop.

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.