title: Substring in Java - length up to a value
body: <p>I'm trying to make a substring which lets me have up to 6 letters of a surname, however what I have here seems to throw an error when it finds a surname of less than 6 letters, I have been looking for hours for a solution with no sucess :/</p>
  
  <pre><code>id = firstName.substring (0,1).toLowerCase() + secondName.substring (0,6).toLowerCase();
  System.out.print ("Here is your ID number: " + id);
  </code></pre>
  
  <p>It's the <code>.substring(0,6)</code>. I need it to be <em>up to</em> 6 letters not exactly 6.</p>
  
  <p>The error:</p>
  
  <pre><code>Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 6
      at java.lang.String.substring(Unknown Source)
      at Test.main(Test.java:27)
  </code></pre>