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 7 of 7

Thread: Help! Error with a save and load feature of a java chess game

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help! Error with a save and load feature of a java chess game

    Currently I am trying to make a chess game, and one of the part is the save and load feature
    Each chess have four data include x,y coordinate, rank and the color.

    For the save feature, I use the file output method to save the data in a txt file. The first and second number is xy coordinate, and the third is color, the last is rank.
    For instance, I save it like this:
    "chess0816chess0618chess1712chess2614chess3415ches s3513"

    But I got some problem on the load part. I know how to input the txt file but I got some problem on the loading.
    The loading code is like this:



    public void load() throws Exception{

    File inputfile = new File ("output.txt");
    Scanner input = new Scanner(inputfile);
    String thisisgreat = input.next();

    for (int i =0;i<=thisisgreat.length();i++){

    char x = thisisgreat.charAt(i);
    char y = thisisgreat.charAt(i+1);
    char k = thisisgreat.charAt(i+2);
    char p = thisisgreat.charAt(i+3);

    if ( (x>='0'|| x<='9')&&(y>='0'|| y<='9')&&(k>='0'|| k<='9')&&(p>='0'|| p<='9')){

    Chess ch ;
    ch = new Chess( k, p );
    GWorld.addOneObject( ch, x, y );

    }


    }


    input.close();
    }
    When I run the program, it said java.lang.StringIndexOutOfBoundsException: String index out of range: 144

    Can anyone help please thank you


  2. #2
    Member
    Join Date
    Nov 2013
    Location
    Bangalore, India
    Posts
    70
    My Mood
    Cool
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Default Re: Help! Error with a save and load feature of a java chess game

    Best way to save the state of an object is through serialization. why can't you do that and serialize the object while saving and retreive the object through de-serialization.

  3. #3
    Junior Member
    Join Date
    Nov 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help! Error with a save and load feature of a java chess game

    Quote Originally Posted by dineshj83 View Post
    Best way to save the state of an object is through serialization. why can't you do that and serialize the object while saving and retreive the object through de-serialization.
    Sorry I am a beginner and haven't learn serialized. All i learned is that the fileio
    Do you have any other suggestions ? thank you

  4. #4
    Member
    Join Date
    Nov 2013
    Location
    Bangalore, India
    Posts
    70
    My Mood
    Cool
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Default Re: Help! Error with a save and load feature of a java chess game

    Quote Originally Posted by davidchan View Post
    Sorry I am a beginner and haven't learn serialized. All i learned is that the fileio
    Do you have any other suggestions ? thank you
    Are you familiar with XML?. In that case you can store your data in XML and read it back using parsers which is easy

  5. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,307
    My Mood
    Mellow
    Thanks
    252
    Thanked 779 Times in 764 Posts

    Default Re: Help! Error with a save and load feature of a java chess game

    Or if we go back to your original question, since you're in complete control of what is being stored as the state of the game, you can make your life easier. This string of stored data is confusing:

    "chess0816chess0618chess1712chess2614chess3415 ches s3513"

    and I have no idea what it means. In your original explanation, it sounds like there are 4 important numbers: x,y,color,rank, so store them exactly like that:

    x,y,color,rank // the data necessary to define a chess piece separated by commas

    then to read each line from the file:

    String[] chessPieceData = input.nextLine().split( "," );

    And then you can create the chess piece with the chessPieceData[] array:

    ChessPiece chessPiece = new ChessPiece( chessPieceData );

    Let me know if you have any questions.

  6. #6
    Junior Member
    Join Date
    Nov 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help! Error with a save and load feature of a java chess game

    Quote Originally Posted by GregBrannon View Post
    Or if we go back to your original question, since you're in complete control of what is being stored as the state of the game, you can make your life easier. This string of stored data is confusing:

    "chess0816chess0618chess1712chess2614chess3415 ches s3513"

    and I have no idea what it means. In your original explanation, it sounds like there are 4 important numbers: x,y,color,rank, so store them exactly like that:

    x,y,color,rank // the data necessary to define a chess piece separated by commas

    then to read each line from the file:

    String[] chessPieceData = input.nextLine().split( "," );

    And then you can create the chess piece with the chessPieceData[] array:

    ChessPiece chessPiece = new ChessPiece( chessPieceData );

    Let me know if you have any questions.
    I try your method and get some changes but still can't operate,

    my input file like this("0816,0618,1712,2614,4615,5713,6611,6817,0201 ,0007,1103,2205,4204,5102,6208,6006"_
    my code:

    public void reload() throws Exception{

    File inputfile = new File ("output.txt");
    Scanner input = new Scanner(inputfile);
    String[] thisisgreat = input.next().split(",");

    for (int i =0;i<=18;i++){

    int x = thisisgreat[i].charAt(0);
    int y = thisisgreat[i].charAt(1);
    int k = thisisgreat[i].charAt(2);
    int p = thisisgreat[i].charAt(3);

    Chess ch ;
    ch = new Chess( k, p );
    GWorld.addOneObject( ch, x, y );
    }

    }


    After I run the program, it said java.lang.ArrayIndexOutOfBoundsException: 16
    How can I do?Thank you

  7. #7
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,307
    My Mood
    Mellow
    Thanks
    252
    Thanked 779 Times in 764 Posts

    Default Re: Help! Error with a save and load feature of a java chess game

    The error message is very pointed and helpful. These are the simpler kind of errors that you should know how to fix at this point. You can't iterate past the end of an array, hence the error.

    You should also know to avoid the use of "magic numbers" like the '18' in the for loop. A for loop that iterates elements of an array should be:

    for ( int i = i ; i < myArray.length ; i++ )

Similar Threads

  1. Major Help, SAVE ME!! Java Dice Game - Keep Score?
    By jbarcus81 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 4th, 2012, 07:36 AM
  2. newbie question: Error: Could not find or load main class Java Result: 1
    By ideaman in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 1st, 2012, 11:40 PM
  3. Chess game help
    By that_guy in forum Java Theory & Questions
    Replies: 3
    Last Post: December 4th, 2011, 08:42 PM
  4. Simple game that requires me to load game settings from a file
    By 14fenix in forum Java Theory & Questions
    Replies: 5
    Last Post: December 1st, 2011, 09:21 PM
  5. Arraylist Save and Load
    By frankycool in forum Collections and Generics
    Replies: 1
    Last Post: November 14th, 2009, 06:48 AM