5 Replies Latest reply: Dec 16, 2009 1:43 PM by 3004 RSS

    java.lang.StringIndexOutOfBoundsException: String index out of range: -9

    843789
      working on a project and came across this exception during runtime.

      Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -9
           at java.lang.String.substring(Unknown Source)
           at SomethingIsWrong.main(SomethingIsWrong.java:17)

      I'm a bit confused. could anyone shed some light on what im doing wrong? i know it deals with my testingStrings method.
      import java.io.*;
      import java.util.*;
      
      public class SomethingIsWrong 
      {
           static Scanner console = new Scanner(System.in);
           
           public static void main(String[] args)
           {
                
                Rectangle method = new Rectangle();
                
                method.width = 40;
                method.height = 50;
      
                System.out.println("myRect's area is " + method.area());
                System.out.println("Was it a car or a cat i saw?".substring(-9, 12)); // error here
                
                testingStrings();
                grades();
           }
           
           public static void cat(File file)
                                         throws FileNotFoundException
           {
                String line = null;
                
                Scanner inFile = new Scanner(new FileReader("myFile.txt"));
                
                try 
                {
                     while ((line = inFile.nextLine())!= null) 
                     {
                     
                          System.out.println(line);
                     }
                     return;
                     
                }     finally 
                     {
                          if (inFile != null)
                          {
                               inFile.close();
                          }
                     }
           }     
           
           public static void grades()
           {
                char grade;
                int testScore = console.next().charAt(0);
                
                if (testScore < 90)
                     grade = 'A';
                else if (testScore < 80)
                     grade = 'B';
                     else if (testScore < 70)
                          grade = 'C';
                     else if (testScore < 60)
                          grade = 'D';
                     else grade = 'F';
                System.out.println("Grade = " + grade);
           }
                
           public static void testingStrings()
           {
                String original = new String();
                StringBuilder result = new StringBuilder("hi");
                int index = original.indexOf('a');
                result.setCharAt(0, original.charAt(0));
                result.setCharAt( 1, original.charAt(original.length()-1));
                result.insert(1, original.charAt(4));
                result.append(original.substring(1,4));
                result.insert(3, (original.substring(index, index+2) + " "));
                System.out.println(result);
           }
                
      }
      
      final class Rectangle
      {
           public int width;
           public int height;
           
           public int area() 
           {
                int area = width * height;
                return area;
           }
      }