Java 使用Character.getNumericValue时StringIndexOutOfBoundsException

Java 使用Character.getNumericValue时StringIndexOutOfBoundsException,java,runtime-error,stringindexoutofbounds,Java,Runtime Error,Stringindexoutofbounds,这是我的tic-tac-toe程序的accept函数,因此s只将数据存储为字符串格式,并在0,0或2,2之间 我现在使用getNumericValue函数分别将数字存储在p和q中,但是在运行期间,当我尝试将值存储在p中时,会出现StringIndexOutOfBounds异常 只有当choice()函数决定x或O时,问题才会出现 在accept()之前调用,否则运行正常。选择()功能有什么问题 你的问题是: 这是我的tic-tac-toe程序的accept函数,因此s只以0,0或2,2之间的格式

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

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

只有当choice()函数决定x或O时,问题才会出现 在accept()之前调用,否则运行正常。选择()功能有什么问题

你的问题是:

这是我的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);

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

如果您只是试图将
0,0
1,1
这样的值存储在两个独立的
int
变量中,那么这应该是一个简单的过程。不过,您应该对错误输入采取预防措施。 因此,您的
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正确地提到的,您应该为变量使用专有名称。这提高了代码的可读性。

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

问题 每当试图在CHAR之后接受字符串或任何其他数据类型(例如,此处输入“ch”之后的“s”的输入)时,就会产生此问题

解决方案 以字符串形式读取输入,并提取第一个字符以使用和丢弃后续空格和换行符 不过,我也找到了解决这个问题的方法。:)


读这本书。如果传递的索引大于等于字符串长度,则会出现
StringIndexOutOfBoundsException
。换句话说,
s
是否少于两个字符,使得
2
成为无效索引?输入格式是什么?@ShyamBaitmangalkar String即使以正确的格式提供了正确的输入,s出于某种原因始终存储空字符串,Character.getNumericValue返回-1。如果
s
始终是空字符串,那么你的问题很可能是重复的。当然,在调用方法之前很难确定xy发生了什么。我使用的是BufferedReader而不是Scanner进行输入。
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();
    }
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();              
    }
}