hello
i have problem creating a Sequence application that generates a series of random numbers from 0 to 9 until a 0 is generated and then displays the length of the sequence of numbers.
the output should look similar to this
3 6 7 8 9 0
Length of the sequence was 6

please help complete the program because i dont know what to do or where to begin.
this is what i have so far

import java.util.Scanner
import java.util.Random 
public class Sequence
{
  public static void main(String[] args)
  {
  Scanner input = new Scanner (System.in);
Random r = new Random();
int randomNum = r.nextInt(9 - 0 +1)+0; 


}
}

Dani AI

Generated

A clear, minimal plan will make this exercise straightforward: produce random digits 0 through 9 until a 0 appears, print each digit (space-separated) as it is generated, and print the total count (including the terminating 0). The Scanner in the OP's snippet is not needed for this task unless extra user input is required; it can be removed to keep the program focused.

Implementation checklist (follow in order):

  1. Create a Random instance and a counter initialized to zero.
  2. Repeatedly generate the next digit in the inclusive range 0–9. Print the digit followed by a space and increment the counter.
  3. Stop the loop when the generated digit is 0.
  4. Finally, print a short message with the counter value.

Common pitfalls and troubleshooting:

  • The usual bug is a wrong bound when generating numbers. The Random method that accepts a bound returns values from 0 up to bound-1, so make sure the bound is set so 0 through 9 can actually appear. If 0 can never be produced (for example because you added 1 to the result), the loop will never terminate. The JavaDoc for this behavior is authoritative: Random.nextInt(int) JavaDoc.
  • To debug, temporarily seed the generator for repeatable runs or print each generated value with iteration info. If you want all digits printed at once, accumulate them (e.g., a StringBuilder) and print the final string; otherwise printing as you go is fine.
  • Remove unused objects (like the unused Scanner) to avoid warnings.

On the thread: supplied the right loop idea but introduced off-by-one issues in the edits; is correct that working through these small steps teaches the most. Follow the checklist above, test with a fixed seed, and the program will behave as expected.

Recommended Answers

All 3 Replies

import java.util.Random;
public class Sequence{
	public static void main(String args[]){
                //loop counter
		int counter = 0;
		int randomNum;
		Random r = new Random();
		do{
			randomNum = r.nextInt(9);
			System.out.print(randomNum);
			counter++;
		}while(randomNum != 0);
		System.out.print("\nLength of the sequence was "+counter);
	}
}

Sample Output:

7176430
Length of the sequence was 7


Why do you do the OPs homework instead of trying to help him learn to do it himself?
You don't learn much copying and pasting.

If you are going to post code, you should add enough comments to the code to describe your logic and what the code does to get the answer.

Or are you just trying to show off that you know how to write a program?

My bad I though it was pretty clear while going through the code. Can't edit the first post I have to wait 30 min.

Edit: I can't edit the first post.

import java.util.Random;
public class Sequence{
	public static void main(String args[]){
        //loop counter; keeps track on the number of generated random numbers
		int counter = 0;
		int randomNum;
		final int N = 9;
		Random r = new Random();
		do{
			randomNum = r.nextInt(N) + 1;//generate a random number from 0-9
			System.out.print(randomNum);
			counter++;                   //increment the counter each time a random number is generated
		}while(randomNum != 0);    //while randomNum is not equal to 0 continue generating random numbers, If 0 then stop
		
		System.out.print("\nLength of the sequence was "+counter);
	}
}
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.