title: How can I stop printing a string at a varying index?
body: <p>As a starter project, I'm making a code that writes out digits of pi. The code prompts for an int, and prints that many decimal places of pi, which uses a predetermined string. The problem is that each individual digit is on a separate line, like this:</p>
  
  <blockquote>
    <p>3<br>
    .<br>
    1<br>
    4<br>
    1<br>
    5<br>
    9</p>
  </blockquote>
  
  <p>That code used a <em>for</em> loop that ends at the index that is given by the scanner. It printed one character at a time. I'm struggling to find a way to make the code print ten-digit substrings of pi separately, each on their own line, meaning 26 digits would be printed like this:</p>
  
  <blockquote>
    <p>3 .<br>
    1415926535<br>
    8979323846<br>
    264338</p>
  </blockquote>
  
  <p>That is two sets of ten, and a third set containing the remaining six. I'm struggling to divide the string printing into groups of ten, and still have the printing stop at the given index, even if that index is not a multiple of ten. In addition, the <em>for</em> loop from the old code is being used for a digit counter, so I would like to keep it. Placing "/n" into the string to make spaces interrupts the counting system, as those characters are counted and printed as well, invalidating that option. Thus far I haven't had any other ideas to fix this. If you know of a way to print in this fashion, let me know. My code is below, feel free to copy it and experiment, and credit me if you decide to use it somewhere.</p>
  
  <pre><code>import java.util.Scanner;
  public class PiWriter {
      public static void main(String[] args) {
          final String pi = "3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679";
          Scanner theScanner = new Scanner(System.in);
          System.out.println("How many decimal places of pi do you want to see?\nEnter an integer less than or equal to 100");
          int stopAt = theScanner.nextInt();
          char currentChar;
          int num0 = 0;
          int num1 = 0;
          int num2 = 0;
          int num3 = 0;
          int num4 = 0;
          int num5 = 0;
          int num6 = 0;
          int num7 = 0;
          int num8 = 0;
          int num9 = 0;
          //Causes the code to ignore the "3." at the start.
          stopAt = stopAt + 2;
          for (int loopNum = 0; loopNum &lt; stopAt; loopNum++) {
              System.out.println(pi.charAt(loopNum));
              currentChar = pi.charAt(loopNum);
              //This counts how many times a digit occurs.
              switch (currentChar) {
                  case '0':
                      num0++;
                      break;
                  case '1':
                      num1++;
                      break;
                  case '2':
                      num2++;
                      break;
                  case '3':
                      num3++;
                  case '4':
                      num4++;
                      break;
                  case '5':
                      num5++;
                      break;
                  case '6':
                      num6++;
                      break;
                  case '7':
                      num7++;
                      break;
                  case '8':
                      num8++;
                      break;
                  case '9':
                      num9++;
                      break;
                  default:
                      break;
              }
          }
          //Further ignores "3."
          num3--;
          //Corrects number for message.
          stopAt = stopAt - 2;
          System.out.println("Successfully printed first " + stopAt + " decimal places of pi.\nShow digit count?\nY/N");
          theScanner.nextLine();
          char response = theScanner.nextLine().charAt(0);
          if (response == 'y'||response == 'Y') {
              //This displays how many times each digit occurred.
              if (num0 == 1) {
                  System.out.println("1 zero");
              }
              else {
                  System.out.println(num0 + " zeroes");
              }
              if (num1 == 1) {
                  System.out.println("1 one");
              }
              else {
                  System.out.println(num1 + " ones");
              }
              if (num2 == 1) {
                  System.out.println("1 two");
              }
              else {
                  System.out.println(num2 + " twos");
              }
              if (num3 == 1) {
                  System.out.println("1 three");
              }
              else {
                  System.out.println(num3 + " threes");
              }
              if (num4 == 1) {
                  System.out.println("1 four");
              }
              else {
                  System.out.println(num4 + " fours");
              }
              if (num5 == 1) {
                  System.out.println("1 five");
              }
              else {
                  System.out.println(num5 + " fives");
              }
              if (num6 == 1) {
                  System.out.println("1 six");
              }
              else {
                  System.out.println(num6 + " sixes");
              }
              if (num7 == 1) {
                  System.out.println("1 seven");
              }
              else {
                  System.out.println(num7 + " sevens");
              }
              if (num8 == 1) {
                  System.out.println("1 eight");
              }
              else {
                  System.out.println(num8 + " eights");
              }
              if (num9 == 1) {
                  System.out.println("1 nine");
              }
              else {
                  System.out.println(num9 + " nines");
              }
          }
      }
  }
  </code></pre>