-8

I want the application to create a sequence that generates a series of random numbers from 0 to 9 util a 0 is generated and then display the length of the sequence of the numbers. Right now my program only prints 0 or 9 as I don't know the random number generator method.My question is how do I generate a random integer between 0 and 9(inclusive) unit a 0 is generated.

import java.util.Random;
import java.util.ArrayList;

public class Sequence {
   public static void main(String[] args) {
       ArrayList<Integer>  lista = new ArrayList<Integer>();
       lista.add(0, 9);



       Random r = new Random();
       System.out.println(lista.get(r.nextInt(lista.size())));
      }
   }

4 Answers4

0

Consider the following code:

ArrayList<Integer> nums = new ArrayList<>();
Random r = new Random();
int num = r.nextInt(10);

while(num != 0)
{
    nums.add(num);
    num = r.nextInt(10);
}

System.out.println(nums.size());

To avoid the List never generating because num's first generated random number is 0, consider using an if statement and reassign the value accordingly.

mperic
  • 118
  • 9
  • This only prints the value 2. The program needs to keep generating values between 0 and 9 until it generates 0. It also needs to print the sequence and the length of the sequence @mperic – user11833872 Jul 26 '19 at 18:15
  • OK. Then use a for-each loop to iterate through your List and print the elements sequentially - after the loop is finished. As for the length: just use the size() method. – mperic Jul 26 '19 at 18:16
  • How exactly would I do that, sorry I'm a beginner @mperic – user11833872 Jul 26 '19 at 18:17
  • Take a look at Oracle's Java tutorials. https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html – mperic Jul 26 '19 at 18:20
0

Your current implementation is currently doing the following:

  1. Creating a list with 1 element with the value of 9
  2. Your call to Random is evaluating to Random.nextInt(1) which will always return 0 as the parameter to nextInt() is an exclusive upper bound from 0 to that bound.
  3. In short, your implementation will always return, the first item in your list, which is 9

If you want to get a random number between 0 and 9 then you need to call Random.nextInt() with an upper bound of 10, i.e. Random.nextInt(10).

To generate a sequence you need to a while loop that terminates when a 0 is returned from random.

Finally, as you don't seem to care about the sequence generated and just the length you can dispense with the List.

So you could try something like the following:

    public static void main(String[] args) {
        Random r = new Random();
        int count = 0;
        while (r.nextInt(10) != 0) {
            count++;
        }
        System.out.println("Sequence Length = " + count);
    }
Marc G. Smith
  • 876
  • 6
  • 8
0

Consider using a while-loop:

import java.util.Random;

class Sequence {
    public static void main(String[] args) {
        Random r = new Random();
        int count = 0;
        int num = -1;
        System.out.println("The sequence is:");
        while (num != 0) {
            num = r.nextInt(10);
            System.out.print(num + " ");
            count++;
        }
        System.out.printf("%nThe length of the sequence is: %d%n", count);
    }
}

Example Output:

The sequence is:
3 9 3 9 4 0
The length of the sequence is: 6

Alternatively if you need to store the sequence in a collection:

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

class Sequence {
    public static void main(String[] args) {
        Random r = new Random();
        List<Integer> result = new ArrayList<>();
        int num = -1;
        while (num != 0) {
            num = r.nextInt(10);
            result.add(num);
        }
        System.out.printf("The sequence is: %s%n", result);
        System.out.printf("The length of the sequence is: %d%n", result.size());
    }
}
Sash Sinha
  • 18,743
  • 3
  • 23
  • 40
0

A while loop would be very useful for this. Examine how you say you want numbers to be generated until a 0 is generated. When you want something to happen over and over until a condition is met, use a while loop. Here's an example of a while loop that would run until a 0 is generated:

ArrayList<Integer>  lista = new ArrayList<Integer>();
Random myRandom = new Random();

int next = myRandom.nextInt(10);
//We use 10 because nextInt() is non-inclusive for the upper bound. It will generate 0 to 9
while(next != 0) {
    lista.add(next);
    next = myRandom.nextInt(10)
}
lista.add(next); //When we get here, a 0 has been generated. Add it to the list.

//To display the length of the list, print out lista.size()
System.out.println(lista.size());

I'm assuming you want the sequence to stop when a 0 is generated and still add that 0 to the list. If not, exclude the add(next) after the loop.