Results 1 to 5 of 5
  1. #1
    Mayur is offline Member
    Join Date
    Jan 2009
    Posts
    24
    Rep Power
    0

    Default StringIndexOutOfBoundsException Error

    Ok this is probably a simple fix but here is part of my code that I seem to be having trouble with. Basically when it asks for input, the input should be "char space char". e.g. "d a". If I enter an input without a space, I get no errors but there needs to be a space in between. It specifically highlights the line "move2 = userIn..."
    Java Code:
            System.out.println("1. Enter your move: ");
            userInput = keyboard.next();
            userInput = userInput.toUpperCase();        // convert the user input to all upper case
            
            if( userInput.charAt( 0) == 'x') {
                System.out.println("ENTER BREAK HERE LATER");
            }
            
            move1 = userInput.charAt( 0);
            move2 = userInput.charAt( 2);
            
            System.out.println("" + move1 + move2);
    Any help is greatly appreciated.

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

    Default

    What's keyboard? Is it a Scanner object? If so, you may wish to use keyboard.nextLine() rather than keyboard.next().

    Also, you'll likely have better answers here if you could post a small compilable code sample, something we all can run and test. Though YMMV.

    Best of luck.

  3. #3
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Yes, I assume it is a Scanner (or at least something similar). @OP, as said you need to use nextLine() as whitespace is the default "separator" for Scanner (and Scanner-like) Classes. so next will retreive only the first character if the second character is a space.

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

    Default

    For an instance of compilable code, check out this:
    Java Code:
    import java.util.Scanner;
    
    public class Fubar
    {
      public static void main(String[] args)
      {
        String userInput = "";
        Scanner keyboard = new Scanner(System.in);
        char move1, move2;
        
        System.out.println("1. Enter your move: ");
        userInput = keyboard.nextLine();
        userInput = userInput.toUpperCase();        // convert the user input to all upper case
        
        if( userInput.charAt( 0) == 'x') {
            System.out.println("ENTER BREAK HERE LATER");
        }
        
        move1 = userInput.charAt( 0);
        move2 = userInput.charAt( 2);
        
        System.out.println("" + move1 + move2);
      }
    }

  5. #5
    Mayur is offline Member
    Join Date
    Jan 2009
    Posts
    24
    Rep Power
    0

    Default

    Sorry I'm just paranoid about posting more of my code because this is for a class and I'm afraid someone might rip it off and then I'll get in trouble.

    keyboard is a scanner. And nextLine() did the trick. Can't believe I forgot about that lol.

    Thanks again guys. Really appreciate it.

Similar Threads

  1. Replies: 3
    Last Post: 01-23-2009, 08:53 AM
  2. error 530 error authentication required
    By rgale in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 05-12-2008, 04:28 PM
  3. StringIndexOutOfBoundsException
    By ravian in forum New To Java
    Replies: 2
    Last Post: 01-29-2008, 11:25 PM

Posting Permissions

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