title: Java - How do I implement sliding window for program
body: <p>I am struggling with how to implement <strong>sliding windows</strong> to the following problem below. </p>
  
  <p>I am writing a program that should ask the location of a file (which has the digits of PI) and the number of digits to parse via the Scanner class from user.</p>
  
  <p>Problem:
  A company is evaluating if it can assign phone numbers by parsing the number of PI after decimal digits. Assume each phone number has 5 digits. So the first 5 digits after the decimal point for the first phone number, the 5 digits starting from the second position form the second phone number (it follows a sliding window pattern where you slide one digit at a time).</p>
  
  <ul>
  <li>If all the phone numbers are unique, then the program should print "Task success, all phone numbers are unique." </li>
  <li>However, if not, then it should print "Phone numbers are repeated". Also the total phone numbers generated should be printed. The program should be able to read 100,000 digits of PI.</li>
  </ul>
  
  <p>Below is my attempt so far.</p>
  
  <p>How can the sliding window be implemented?</p>
  
  <p>Any suggestions are much appreciated.</p>
  
  <pre><code>import java.io.BufferedReader;
  import java.io.FileInputStream;
  import java.io.FileNotFoundException;
  import java.io.InputStreamReader;
  import java.util.HashSet;
  import java.util.Scanner;
  import java.util.Set;
  
  
  public class Problem3 {
      public static void main(String[] args) throws Exception {
          FileInputStream inputstream = null;
          BufferedReader reader = null;
          Set&lt;String&gt; set = new HashSet&lt;String&gt;();
  
          @SuppressWarnings("resource")
          Scanner input = new Scanner(System.in);
  
          try {
              System.out.println("Give the location of the file (example: C:\\Users\\Onur\\workspace\\ProgAssign3\\src\\pi.txt):");
              String fileloc = input.nextLine();
  
              inputstream = new FileInputStream(fileloc);
              reader = new BufferedReader(new InputStreamReader(inputstream));
              String stringinput;
  
              System.out.println("Number of digits of PI to parse: ");
              int parsenum = input.nextInt() + 2;
              String[] stringarray = new String[parsenum];
  
              while((stringinput = reader.readLine()) != null) {
                  stringinput = stringinput.substring(2, parsenum);
  
                  for(int i = 0; i &lt; stringinput.length(); i++) {
                      stringarray = stringinput.split("");
                  }
              }
  
              for(int i = 0; i &lt; 5; i++){
                  set.add(stringarray[i]);
              }
  
              System.out.println(set);
  
          } 
          catch (FileNotFoundException exception) {
              System.err.println("File not found, please try again");
              main(null);
          }
          catch (Exception e) {
              System.err.println("Invalid input entered");
          }
          finally {
              reader.close();
          }
      }
  }
  </code></pre>