Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: I'm getting a random, stringindexoutofbounds exception

  1. #1

    Default I'm getting a random, stringindexoutofbounds exception

    This seems like some java error. I see no error with this code - in fact, it was working perfectly just a while before that. The line before the last line gives me this error: stringindexoutofboundsexceptions: String index out of range : 7, even before I compile the program and input the input.

    public class person_manager {
     
    	static File file = new File("C:\\Documents and Settings\\Administrator\\My Documents\\sorted_list.txt");
    	static File file2 = new File("C:\\Documents and Settings\\Administrator\\My Documents\\the_person_modification_database.txt");
    	static StringBuffer contents = new StringBuffer();
    	static BufferedReader reader = null;
    	static BufferedReader reader1 = null;
    	static int count = 0;
    	static int count1 = 0;
    	static int listlength = 0;
    	static String[] list = null;
    	static String temp1 = null;
    	static Path file1 =  FileSystems.getDefault().getPath("C:\\Documents and Settings\\Administrator\\My Documents\\", "sorted_list.txt");
    	static person[] people_array = null;
    	static int arraysize = 0;
    //63
    	public static void main(String[] args) throws IOException {
     
    		try {
           	       		reader = new BufferedReader(new FileReader(file2));
    			reader1 = new BufferedReader(new FileReader(file2));
     
    	       		String text = null;
     
     
     
    		        // repeat until all lines are read
     
                		while ((text = reader.readLine()) != null) {
    		                contents.append(text).append(System.getProperty("line.separator"));
    				count++;
                		}
     
    			list = new String[count];
    			people_array = new person[count/6];
    			arraysize = count/6;
    			count = 0;
     
    			while ((temp1 = reader1.readLine()) != null) {
     
     
    			if (temp1.substring(0, 2).compareTo("na") == 0 )
    			{
     
    			people_array[count1] = new person();
    			people_array[count1].name=temp1.substring(5, temp1.length());}		
     
    			try {
    			if (temp1.substring(0, 2).compareTo("he") == 0 )
    			{people_array[count1].height=Integer.parseInt(temp1.substring(7, temp1.length()));}
    			} catch (NumberFormatException f) {people_array[count1].height=0;}
    			try {
    			if (temp1.substring(0, 2).compareTo("we") == 0 && (temp1.substring(6, 7).compareTo("-") != 0)  )
    			{
    			people_array[count1].weight=Integer.parseInt(temp1.substring(7, temp1.length()));}
    			} catch (NumberFormatException f) {people_array[count1].weight=0;}
     
    			if (temp1.substring(0, 2).compareTo("ey") == 0)
    			{people_array[count1].eyecolor=temp1.substring(10, temp1.length());}
     
    			if (temp1.substring(0, 7).compareTo("weight-") == 0)
    			{people_array[count1].weight_status=temp1.substring(14, temp1.length());}


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: I'm getting a random, stringindexoutofbounds exception

    if (temp1.substring(0, 7).
    I see numerous line where you are calling substring with hardcoded values. What happens when a String with less than 7 characters is used?
    Improving the world one idiot at a time!

  3. #3

    Default Re: I'm getting a random, stringindexoutofbounds exception

    That never happens. All the lines in the file have 7 characters or more.

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: I'm getting a random, stringindexoutofbounds exception

    Quote Originally Posted by Programming_Hobbyist View Post
    That never happens. All the lines in the file have 7 characters or more.
    With an error like:
    stringindexoutofboundsexceptions: String index out of range : 7,
    ...something happened. Time to do some debugging and see what happened where. Use a debugger or some printlns and verify your assumptions.

  5. #5

    Default Re: I'm getting a random, stringindexoutofbounds exception

    One day it just stopped working. Perhaps there is something wrong with the compiler.

  6. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: I'm getting a random, stringindexoutofbounds exception

    I can pretty much guarantee you it's not the compiler or the Java API, unless you're running some home-brewed version of Java. The official releases from the OpenJDK project, Apple, or Oracle all are top-notch and don't have many errors in them, especially not for these core functions which have been around for about a decade or more now.

    Most likely causes for error:

    1. You had an empty line somewhere in your file (likely the end of your file)
    2. You didn't recompile your program

    To be able to guarantee that substring will succeed you need to check that the string is both not null and that its length is greater than the end index.

    String str = "blah";
    String result;
    if(str != null && str.length() >= 7)
    {
        // safe to call substring for a string with end index 7
        result = str.substring(0, 7);
    }
    else
    {
        // substring is guaranteed to fail if end index is 7 or greater
        System.out.println("The string is either null or is shorter than the minimum required length");
    }

Similar Threads

  1. Generation of random number using random class
    By JavaPF in forum Java SE API Tutorials
    Replies: 1
    Last Post: December 7th, 2011, 05:46 PM
  2. Replies: 5
    Last Post: September 5th, 2011, 10:31 AM
  3. StringIndexOutOfBounds Exception
    By adamLovesCS in forum What's Wrong With My Code?
    Replies: 4
    Last Post: July 16th, 2011, 06:52 AM
  4. Replies: 6
    Last Post: March 25th, 2011, 03:42 PM
  5. Generation of random number using random class
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: April 16th, 2009, 06:10 AM