Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 子字符串导致字符串索引超出范围错误_Java_Indexing_Range_Substring - Fatal编程技术网

Java 子字符串导致字符串索引超出范围错误

Java 子字符串导致字符串索引超出范围错误,java,indexing,range,substring,Java,Indexing,Range,Substring,我正在写一个程序,可以反转2到4位数字,我写了一个方法可以做到这一点。项目构建中没有错误,但我在尝试运行它时得到“字符串索引超出范围:-3”。我对编程相当陌生,我不知道我做错了什么 代码如下: public static void main(String[] args) { int num1 = 1; int num2 = 1; Scanner console = new Scanner(System.in);

我正在写一个程序,可以反转2到4位数字,我写了一个方法可以做到这一点。项目构建中没有错误,但我在尝试运行它时得到“字符串索引超出范围:-3”。我对编程相当陌生,我不知道我做错了什么

代码如下:

    public static void main(String[] args)
    {
        int num1 = 1;
        int num2 = 1;

        Scanner console = new Scanner(System.in);

        while (num1 <1000 || num1 > 9999)
        {
            System.out.println("Please enter a positive 4 digit number");
            num1 = console.nextInt();
        }

        String numString1 = Integer.toString(num1);

        while (num2 <1000 || num2 > 9999)
        {
            System.out.println("Please enter another positive 4 digit number");
            num2 = console.nextInt();
        }

        String numString2 = Integer.toString(num2);

        int numReverse1 = stringReverse(numString1);

        int numReverse2 = stringReverse(numString2);

        System.out.println(numReverse1 + numReverse2);

        System.out.println("The product of your 2 reversed numbers is: " + (numReverse1 * numReverse2));

    }

    public static int stringReverse (String numString)
    {
        String c1c2c3 = numString.substring(4);
        String c2c3c4 = numString.substring(1);

        String c1c2 = c1c2c3.substring(3);
        String c3c4 = c2c3c4.substring(1);

        String c1 = c1c2.substring(2);
        String c2 = c1c2.substring(1);
        String c3 = c3c4.substring(2);
        String c4 = c3c4.substring(1);

        String numStringReverse = c4 + c3 + c2 + c1;

        int reversedString = Integer.parseInt(numStringReverse);

        return reversedString;
    }
}
publicstaticvoidmain(字符串[]args)
{
int num1=1;
int num2=1;
扫描仪控制台=新扫描仪(System.in);
而(num1 9999)
{
System.out.println(“请输入一个正4位数字”);
num1=console.nextInt();
}
字符串numString1=Integer.toString(num1);
while(numm2 9999)
{
System.out.println(“请输入另一个正4位数字”);
num2=console.nextInt();
}
字符串numString2=Integer.toString(num2);
int numReverse1=stringReverse(numString1);
int numReverse2=stringReverse(numString2);
System.out.println(numReverse1+numReverse2);
System.out.println(“两个反向数字的乘积为:”+(numReverse1*numReverse2));
}
公共静态int-stringReverse(字符串numString)
{
字符串c1c2c3=numString.substring(4);
字符串c2c3c4=numString.substring(1);
字符串c1c2=c1c2c3。子字符串(3);
字符串c3c4=c2c3c4。子字符串(1);
字符串c1=c1c2。子字符串(2);
字符串c2=c1c2。子字符串(1);
字符串c3=c3c4。子字符串(2);
字符串c4=c3c4。子字符串(1);
字符串numStringReverse=c4+c3+c2+c1;
int reversedString=Integer.parseInt(numStringReverse);
返回反向字符串;
}
}
行中:

String c1c2c3 = numString.substring(4);
String c1c2 = c1c2c3.substring(3);
您正在尝试访问一个不存在的位置。Java中的数组由0开始,直到“-1”。
开始索引是包含的,结束索引在
子字符串中是独占的

stringReverse`的正确版本如下:

public static int stringReverse (String numString)
{
    String c1c2c3 = numString.substring(0,3);
    String c2c3c4 = numString.substring(1,4);

    String c1c2 = c1c2c3.substring(0,2);
    String c3c4 = c2c3c4.substring(1,3);

    String c1 = c1c2.substring(0,1);
    String c2 = c1c2.substring(1,2);
    String c3 = c3c4.substring(0,1);
    String c4 = c3c4.substring(1,2);

    String numStringReverse = c4 + c3 + c2 + c1;

    int reversedString = Integer.parseInt(numStringReverse);

    return reversedString;
}

另外,请阅读此文。

在Java中,可能有更优雅的方法来反转字符串。如果您想在Java中找到一个简单的替代方法,请查看该类

检查文档并查找
反向
方法