有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

使用字符时的java StringIndexOutOfBoundsException。getNumericValue

这是我的tic-tac-toe程序的accept函数,因此s只将数据存储为字符串格式,并在0,0或2,2之间

我现在使用getNumericValue函数分别将数字存储在pq中,但是在运行时,当我尝试将值存储在p中时,会出现StringIndexOutOfBounds异常

The problem is happening only when choice() function to decide x or O is called before accept() else it's running fine. What is the problem with the choice() funtion?

void accept()throws IOException
{
    System.out.println("Your move:");
    String s=xy.readLine();

    int p = (Character.getNumericValue(s.charAt(0)))-1;
    int q = Character.getNumericValue(s.charAt(2))-1;
    if(ar[p][q]==0)
        ar[p][q]=1;
    else
    {
        System.out.println("You can't capture a location that has already been captured!");
        accept();
    }
}



void choice() throws IOException
    {
        System.out.println("Welcome to tictactoe");
        System.out.print("Enter your weapon X or O : ");
        chp = Character.toUpperCase((char)xy.read());

        if (chp=='X')
            chc='O';
        else 
            chc = 'X';

        System.out.println("kkbot chose: "+ chc);
    }

共 (3) 个答案

  1. # 1 楼答案

    你的问题是:

    这是我的tic-tac-toe程序的accept函数,因此s只以0,0或2,2之间的格式存储数据

    但您的代码会:

    String s=xy.readLine();
    int p = (Character.getNumericValue(s.charAt(0)))-1;
    int q = Character.getNumericValue(s.charAt(2))-1;
    

    用户可以输入他想要的任何内容。readLine()中的任何内容都不会阻止他添加空字符串或过长字符串

    在使用该字符串执行任何操作之前,必须验证该字符串是否具有假定的长度;例如:

    String inputFromUser = "";
    do {
      System.out.println("Your move [enter a value like A1]: ");
      inputFromUser = scanner.readLine();
    } while (inputFromUser.length != 2);
    

    除此之外:请为变量使用real名称。s、 xy,p,q。。。不要告诉读者这些变量的用途。是的,你在打字时节省了一点时间;在以后阅读源代码时,您将花费10倍的时间;而且,你也极大地增加了那些丑陋的单字符名字出现愚蠢拼写错误的可能性

  2. # 2 楼答案

    如果您只是试图在两个独立的int变量中存储像0,01,1这样的值,那么这应该是一个简单的过程。不过,您应该对错误输入采取预防措施。 因此,您的accept()方法应该是这样的:

    public void accept(){
            Scanner sc = new Scanner(System.in);
            System.out.println("Your move [Enter marking position in the form x,y]: ");
            String userInput = sc.nextLine();
            String[] userMarkedPositions = userInput.split(",");
            if(userMarkedPositions.length == 2){
                int x = Integer.parseInt(userMarkedPositions[0]);
                int y = Integer.parseInt(userMarkedPositions[1]);
                //Followed by your other operations
                //....
                //....
            }else{
                System.out.println("Invalid input!!");
                System.out.println("Input should be in the form of x,y");
                accept();
            }
            sc.close();
        }
    

    正如@GhostCat正确地提到的,您应该为变量使用专有名称。这提高了代码的可读性

  3. # 3 楼答案

    嘿,我终于在代码中找到了问题。 choice()函数接受一个字符,但在接受该字符串之后,当用户没有机会输入该字符串时,机器会自动将null作为输入字符串(在字符后按空格键时生成)

    问题

    This problem is produced whenever one tries to accept a string or any other data type after CHAR, for example, the input for 's' here after the input 'ch'.

    溶液

    Read input as a string, and extract the first character to consume and discard the subsequent spaces and newlines I also found a way to hack this problem though. :)

    import java.io.*;
    class tictactoe
    {
        BufferedReader xy=new BufferedReader(new InputStreamReader(System.in));
        void accept()throws IOException
        {
            System.out.println("Enter your weapon X or O : ");
            char ch = xy.readLine().charAt(0);  #Ahaaa momemt
        
            System.out.println("Your move:");
            String s=xy.readLine();              
        }
    }