title: Working on Program Called DieToss that Searches for Duplicate Tosses. How can I limit where parentheses can close?
body: <p>Hello StackoverFlow Community</p>
  <p>I am working on a DieToss program intended to print random sequence numbers given an inputted length and the randomized toss needs to be between 1 and 6. The goal is to put parentheses around the longest or first string of duplicates.</p>
  <p>The issue here is that it continues to put parentheses around all duplicate strings of integers and I have been unsuccessful in my efforts.</p>
  <p>Below is the program to look for duplicates in a given array of integers.</p>
  <pre><code>public class DieToss {
      public static void main(String[] args) {
          boolean restartQ = true;
          
           while(restartQ){
              int numDie = 0;
              
              Scanner in = new Scanner(System.in);
              
              boolean ehandle = true;
              
              System.out.println(&quot;Tell me how many dice you want me to throw?&quot;);
              
              System.out.println(&quot;Be sure that it is 10 or greater but only up to 30&quot;);
              
              
              while (ehandle) {
                  try {
                    numDie = in.nextInt();
                    if(numDie &lt;= 30 &amp;&amp; numDie &gt;= 10){
                        ehandle = false;
                    }
                    
                    else{
                        System.out.println(&quot;Please Re-enter. It needs to be greater than or equal to 10 but only up to 30.&quot;);
                        System.out.println(&quot;Tell me how many dice you want me to throw?&quot;);
                        continue;
                    }
                  } 
                  catch (Exception e) {
                    System.out.println(&quot;Please enter integers only&quot;);
                    
                    System.out.println(&quot;Tell me how many dice you want me to throw?&quot;);
              
                    System.out.println(&quot;Be sure that it is 10 or greater but only up to 30&quot;);
                    in.next();
                  }
              }
              
              int[] dieToss = new int[numDie];
              Random random = new Random();
              
      
              for (int i = 0; i &lt; dieToss.length; i++) {
                  dieToss[i] = 1 + random.nextInt(6);
              }
      
              //String dieArr = (Arrays.toString(dieToss));
              
              //System.out.print(Arrays.toString(dieToss));
              
              boolean parenOpen = false;
              
              
              for (int i = 0; i &lt; dieToss.length; i++){
                  if (i &lt; dieToss.length - 1)
                  {
                      if (dieToss[i] == dieToss[i + 1] &amp;&amp; !parenOpen )
                      {
                          System.out.print(&quot;(&quot;);
                          parenOpen = true;
                      }
                      System.out.print(dieToss[i] + &quot; &quot;);
                      if (dieToss[i] != dieToss[i + 1] &amp;&amp; parenOpen)
                      {
                          System.out.print(&quot;)&quot;);
                          parenOpen = false;
                      }
      
                  }
      
              }
              System.out.println(dieToss[dieToss.length - 1]);
              System.out.println(&quot; &quot;);
              
              System.out.println(&quot;Any duplicates that I have found are displayed above in parentheses.&quot;);
              if (parenOpen)
              {
                  System.out.print(&quot;)&quot;);
              }
              
              System.out.println(&quot;Would you like to reroll? Maybe test your luck?&quot;);
              System.out.println(&quot; &quot;);
              System.out.println(&quot;Enter any key if you would like to. Press n to exit.&quot;);
              
              Scanner in2 = new Scanner(System.in);
              
              String reRoll = in2.nextLine();
              
              if(reRoll.equals(&quot;n&quot;) || reRoll.equals(&quot;N&quot;)){
                  restartQ = false;
              }
              else{
                  continue;
              }
          }
      }
  }
  </code></pre>
  <p>How can I modify this block of code to limit itself to the longest string of duplicates?</p>
  <p>Any help is greatly appreciated.</p>