java - How to mark the output and remove zeros?

This is a program to simulate rolling a dice 20 times and marking the most consecutive number, a dice doesn't have 0 which I don't know how to remove, I'm new to java

``package diceroll;import java.util.Random;public class Roll {//public static void main(String[] args) {private static Random generator = new Random();public static int[] randomIntArray(int length, int n){int [] ems = new int [length];for (int i = 0; i <ems.length; i++)ems[i] = generator.nextInt(n);return ems;}}``

``package diceroll;import java.util.Arrays;public class Dice {public static void main(String[] args) {int[] ems = Roll.randomIntArray(20,7);System.out.println(Arrays.toString(ems));}}``

When it prints it prints something like this:

[4, 2, 2, 5, 2, 0, 5, 0, 5, 5, 1, 3, 3, 3, 1, 4, 3, 1, 4, 4]

How can I make the code not generate zeros and mark the most consecutive numbers like this:

[4, 2, 2, 5, 2, 0, 5, 0, 5, 5, 1, (3, 3, 3), 1, 4, 3, 1, 4, 4]

To avoid 0 you must add 1 to nextInt output (it starts from 0).
You can mark the most consecutive numbers during array population.
Something like

``````int length=1;
int defLength=0;
int startIdx=0;
int defStartIdx=0;
int endIdx=0;
int defEndIdx=0;
// Needed in order not to override start index
boolean found = false;
if(efs[i] == efs[i-1]) {
++length;
if(!found) {
startIdx=i-1;
found = true;
}
} else {
if(found) {
endIdx=i-1;
found=false;
}
if(defLength < length) {
defEndIdx = endIdx;
defStartIdx = startIdx;
found = false;
}
}
``````

Then you need a print method to put your parenthesis before printing efs[defStartIdx] and before efs[defEndIdx]

How can I make the code not generate zeros ...

Since `Random#nextInt(n)` returns a number between 0 (inclusive) and n (exclusive) you might just add 1 to the result to change that to a number between 1 and n (both inclusive). If you want n to be excluded, pass `n-1` to `nextInt(...)`.

... mark the most consecutive numbers ...

You'd have to calculate that yourself and provide your own printing routine.

To calculate the longest row of equal numbers, just iterate over the array once and calculate the length of each sequence (length = 1 if the numbers are different and length += 1 if they are equal). Whenever you discover a new sequence or hit the end of the array, check if the last sequence was longer than the longest before and if so, update the starting index of that sequence and the new max length.

When iterating over the array for printing you then check whether you enter the sequence you calculated before and if so, print `(`. Then you check whether you leave the sequence again or hit the end and print `)`.