Java Character Error (AQA COMP1)

Computer Science and ICT discussion, revision, exam and homework help.

Announcements Posted on
IMPORTANT: You must wait until midnight (morning exams)/4.30AM (afternoon exams) to discuss Edexcel exams and until 1pm/6pm the following day for STEP and IB exams. Please read before posting, including for rules for practical and oral exams. 28-04-2013
Sign in to Reply
  1. Mouldy_Underwear's Avatar
    • Banned
    • Posts: 22
    • Warning points: 1000
    Java Character Error (AQA COMP1)
    I get this error message "Exception in thread "main" java.lang.StringIndexOutOfBounds Exception: String index out of range: 0
    at java.lang.String.charAt(Unknown Source)
    at AQAConsole.readChar(AQAConsole.j ava:37)
    at AQAConsole.readChar(AQAConsole.j ava:29)
    at COMP1_Summer_2012_SkelProg_Java. getMove(COMP1_Summer_2012_SkelPr og_Java.java:248)
    at COMP1_Summer_2012_SkelProg_Java. playGame(COMP1_Summer_2012_SkelP rog_Java.java:367)
    at COMP1_Summer_2012_SkelProg_Java. <init>(COMP1_Summer_2012_SkelPro g_Java.java:422)
    at COMP1_Summer_2012_SkelProg_Java. main(COMP1_Summer_2012_SkelProg_ Java.java:444)"

    My teacher says that it may be related to the coding in one of these sections:

    char getMove() {
    boolean charEntered = false;
    char move;
    move = console.readChar();
    charEntered = Character.isLetter(move);
    if (!charEntered){
    move='x';
    }
    else
    {
    move = Character.toUpperCase(move);
    }
    move = Character.toUpperCase(move);
    console.println();
    return move;


    boolean checkValidMove(CellReference playerPosition, char direction) {
    boolean validMove;
    validMove = true;
    if (!(direction == 'N' || direction == 'S' || direction == 'W'
    || direction == 'E' || direction == 'M')) {
    validMove = false;
    }
    return validMove;
    }



    void playGame(char[][] cavern, CellReference[] trapPositions, CellReference monsterPosition, CellReference playerPosition, CellReference flaskPosition, Logical monsterAwake) {
    int count;
    boolean eaten;
    boolean flaskFound;
    char moveDirection;
    boolean validMove;
    eaten = false;
    flaskFound = false;
    displayCavern(cavern, monsterAwake);
    do {
    do {
    displayMoveOptions();
    moveDirection = getMove();
    validMove = checkValidMove(playerPosition, moveDirection);
    } while (!validMove);
    if (moveDirection != 'M') {
    makeMove(cavern, moveDirection, playerPosition);
    displayCavern(cavern, monsterAwake);
    flaskFound = checkIfSameCell(playerPosition, flaskPosition);
    if (flaskFound) {
    displayWonGameMessage();
    }


    We have tried fixing it, the teacher says that the character isn't being read when we press enter, the problem occurs when we don't enter a letter into the program and simply press enter, how do I fix this?!?!?
    Last edited by Mouldy_Underwear; 20-03-2012 at 14:41.
  2. roblee's Avatar
    • Exalted and Worshipped Member
    • Posts: 1,062
    Re: Java Character Error (AQA COMP1)
    It's a bug in the provided AQAConsole class. Obviously they're using something similar to
    Code:
    String line = scanner.readLine();
    return line.charAt(0);
    When they should be using
    Code:
    do {
      String line = scanner.readLine();
    } while (line.length() < 1);
    return line.charAt(0);
    Or even better:
    Code:
    do {
      int c = System.in.read();
     if (c == -1) throw new EOFException();
    } while (!Character.isLetter( (char) c));
    
    return (char) c;
Sign in to Reply
Share this discussion:  
Article updates
Moderators

We have a brilliant team of more than 60 volunteers looking after discussions on The Student Room, helping to make it a fun, safe and useful place to hang out.

Reputation gems:
The Reputation gems seen here indicate how well reputed the user is, red gem indicate negative reputation and green indicates a good rep.
Post rating score:
These scores show if a post has been positively or negatively rated by our members.