Results 1 to 5 of 5
  1. #1
    whatson is offline Member
    Join Date
    Aug 2012
    Posts
    1
    Rep Power
    0

    Default Java runtime error: String index out of range.

    I've written a program that displays tokens dropped into a board depending on the user's selection of which column to drop it in. I'm getting a runtime error

    java.lang.StringIndexOutOfBoundsExcepti… String index out of range: 42
    at java.lang.String.substring(Unknown Source)
    at A1Program.extractRow(A1Program.java:83)
    at A1Program.start(A1Program.java:19)
    at A1Application.main(A1Application.java:11…
    at sun.reflect.NativeMethodAccessorImpl.inv… Method)
    at sun.reflect.NativeMethodAccessorImpl.inv… Source)
    at sun.reflect.DelegatingMethodAccessorImpl… Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at edu.rice.cs.drjava.model.compiler.JavacC…
    when I run the program, and I'm a bit lost as to where I might have gone wrong.

    My code is:

    public class A1Program {

    public void start() {

    String inputString = " ";
    boolean gameOver = false;

    String tokens = inputString;

    while (!gameOver) {
    int column = columnResult();
    tokens = enterToken("X", tokens, column);
    System.out.println(tokens);
    displayBoard(extractRow(5, tokens));
    displayBoard(extractRow(4, tokens));
    displayBoard(extractRow(3, tokens));
    displayBoard(extractRow(2, tokens));
    displayBoard(extractRow(1, tokens));
    displayBoard(extractRow(0, tokens));
    System.out.println("---------------");
    }
    }

    private int columnResult(){
    System.out.println("Enter column for X:");
    String keyInput = Keyboard.readInput();
    int column1 = Integer.parseInt(keyInput);
    return column1;
    }
    private String enterToken(String tokenSymbol, String inputString, int column){
    String columnEdit = inputString.substring(column*6-6,column*…

    String columnEdit1 = columnEdit.trim();
    String columnEdit2 = columnEdit1+tokenSymbol+" ";
    String columnEdit3 = columnEdit2.substring(0,6);
    String start = inputString.substring(0,column*6-6);
    String end = inputString.substring(column*6);

    System.out.println(start);
    return start+tokenSymbol+end;
    }

    private String extractRow(int row, String tokens) {
    String rowString1 = tokens.substring(row, row + 1);
    String rowString2 = tokens.substring(row + 6, row + 7);
    String rowString3 = tokens.substring(row + 12, row + 13);
    String rowString4 = tokens.substring(row + 18, row + 19);
    String rowString5 = tokens.substring(row + 24, row + 25);
    String rowString6 = tokens.substring(row + 30, row + 31);
    String rowString7 = tokens.substring(row + 36, row + 37);

    return "|" + rowString1 + "|" + rowString2 + "|"
    + rowString3 + "|" + rowString4 + "|" + rowString5
    + "|" + rowString6 + "|" + rowString7 + "|";

    }

    private void displayBoard(String extractRow) {

    System.out.println(extractRow);

    }
    }

    Any help is appreciated!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Java runtime error: String index out of range.

    Can you show us which line is causing the error? Also please try to edit your post above and place [code] [/code] tags above and below your posted code to help make it retain its formatting and be readable.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Java runtime error: String index out of range.

    .... or not ...

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default Re: Java runtime error: String index out of range.

    Quote Originally Posted by whatson View Post
    String rowString2 = tokens.substring(row + 6, row + 7);
    String rowString3 = tokens.substring(row + 12, row + 13);
    String rowString4 = tokens.substring(row + 18, row + 19);
    String rowString5 = tokens.substring(row + 24, row + 25);
    String rowString6 = tokens.substring(row + 30, row + 31);
    String rowString7 = tokens.substring(row + 36, row + 37);
    It almost inevitable that you get SIOOBE when you see lines of code like. It is dicfficult to guarantee that all Strings are going to be long enough that x + 37 will still be in bounds.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default Re: Java runtime error: String index out of range.

    Quote Originally Posted by Fubarable View Post
    .... or not ...
    Probably not.

    Cross posted
    Java method arrangement, runtime error - Stack Overflow
    Help with Java task please (method arrangement)? - Yahoo! Answers

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 2
    Last Post: 05-07-2012, 06:31 PM
  2. String index out of range: -1 Error.
    By ashishb4 in forum New To Java
    Replies: 12
    Last Post: 09-28-2011, 07:32 PM
  3. String index out of range: -1 due to length function
    By ashishb4 in forum New To Java
    Replies: 1
    Last Post: 06-03-2011, 07:04 AM
  4. Replies: 2
    Last Post: 01-07-2011, 11:50 AM
  5. String index out of range: -1 Please Help
    By davetheant in forum New To Java
    Replies: 22
    Last Post: 09-11-2010, 12:32 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •