获取错误代码,但不知道原因

时间:2014-12-05 21:28:29

标签: java indexoutofboundsexception

我需要为分数计算器编写一个代码,可以对两个分数进行加,减,乘和除。我有这个代码,并收到错误消息

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
    at java.lang.String.substring(Unknown Source)
    at Calculator.run(Calculator.java:24)
    at Calculator.main(Calculator.java:13)

我知道错误信息给了我需要修复的地方,但我无法弄清楚我做错了什么。我仍然是Java的新手,所以它可能是一个非常简单的解决方案。

提前谢谢。

    import java.util.*;

    public class Calculator {
    public static void main(String[] args) {
        System.out.println("Please enter two fractions to add, subtract, multiply, or divide\nor\nType 'quit' to exit the program.");
        Boolean on = true;
        Scanner console = new Scanner(System.in);
        while (on) {
            String input = console.nextLine();
            if (input.equalsIgnoreCase("quit")) {
                on = false;
            } else
                System.out.println(run(input));
        }
    }

    public static String run(String input) {
        int indexOfSecondSpace = 0;
        int indexOfOperation = 0;
        String firstNumber = "0";
        String secondNumber = "0";
        int beginning = input.indexOf(" ") + 1;
        int end = input.indexOf(" ", input.indexOf(" "));
        String operator = input.substring(beginning, end);
        if (input.contains("+") == true) {
            indexOfOperation = input.indexOf("+");
        } else if (operator.equals("-")) {
            indexOfOperation = input.indexOf("-");
        } else if (operator.equals("*")) {
            indexOfOperation = input.indexOf("*");
        } else if (operator.equals("/")) {
            indexOfOperation = input.indexOf("/");
        }
        firstNumber = (input.substring(input.indexOf(" ")));
        secondNumber = (input.substring(input.indexOf(" ") + 1));
        int a = 0;
        int b = 0;
        int c = 0;
        int d = 0;  
        if (firstNumber.contains("/")) {
            a = Integer.parseInt(firstNumber.substring(0,firstNumber.indexOf("/")));
            b = Integer.parseInt(firstNumber.substring(0),firstNumber.indexOf("/"));
        } else if (!firstNumber.contains("/"))
            a = Integer.parseInt(input.substring(0, input.indexOf(" ")));
            b = Integer.parseInt("1");
        {
            if (secondNumber.contains("/")) {
                c = Integer.parseInt(secondNumber.substring(secondNumber.indexOf("/")));
                d = Integer.parseInt(secondNumber.substring(secondNumber.indexOf("/" + 1, secondNumber.length())));
            } else if (!secondNumber.contains("/")) {
                c = Integer.parseInt(secondNumber.substring(secondNumber.length()));
                d = Integer.parseInt("1");
            }
        }
        return input;

    }
    public static String calculate(String input, int a, int b, int c, int d){
    if (input.contains ("+"))
    {
        System.out.println("your answer is " + (a*d + b*c)+"/" +(b*d));
    }
    else if  (input.contains("-"))
    {
        System.out.println("your answer is " + (a*d - b*c)+ "/" +(b*d));
    }
    else if (input.contains("/"))
    {
        System.out.println("your answer is " + (a*d)/(b*c)+ "/" +(b*d));
    }
    else if (input.contains("*"))
    {
        System.out.println("your answer is " + (a*c) +"/" +(b*d));
    }
    return input;
}
}

2 个答案:

答案 0 :(得分:2)

如果找不到字符串,String.indexOf方法将返回-1。在这个宣传片中你有:

int beginning = input.indexOf(" ") + 1;
int end = input.indexOf(" ", input.indexOf(" "));

导致

int beginning = input.indexOf(" ") + 1; // = -1 + 1 = 0
int end = input.indexOf(" ", input.indexOf(" ")); // input.indexOf(" ", -1); ERROR!

你很可能是:

int beginning = input.indexOf(" ") + 1; // = -1 + 1 = 0
int end = input.indexOf(" ", beginning ); // input.indexOf(" ", 0); Great job!

答案 1 :(得分:0)

ug_的答案是对的..顺便说一句,你的方法run()包含很多错误(我试图运行它)

我认为固定版本对你有用..这里是:(检查substring()方法;))

public static String run(String input) {
int indexOfSecondSpace = 0;
int indexOfOperation = 0;
String firstNumber = "0";
String secondNumber = "0";
int beginning = input.indexOf(" ") + 1;
int end = input.indexOf(" ", beginning);
String operator = input.substring(beginning, end);
if (input.contains("+") == true) {
    indexOfOperation = input.indexOf("+");
} else if (operator.equals("-")) {
    indexOfOperation = input.indexOf("-");
} else if (operator.equals("*")) {
    indexOfOperation = input.indexOf("*");
} else if (operator.equals("/")) {
    indexOfOperation = input.indexOf("/");
}
firstNumber = (input.substring(0, input.indexOf(" ")));
secondNumber = (input.substring(beginning + 1));
int a = 0;
int b = 0;
int c = 0;
int d = 0;  
if (firstNumber.contains("/")) {
    a = Integer.parseInt(firstNumber.substring(0,firstNumber.indexOf("/")));
    b = Integer.parseInt(firstNumber.substring(0,firstNumber.indexOf("/")));
} else if (!firstNumber.contains("/"))
    a = Integer.parseInt(input.substring(0, input.indexOf(" ")));
    b = Integer.parseInt("1");
{
    if (secondNumber.contains("/")) {
        c = Integer.parseInt(secondNumber.substring(secondNumber.indexOf("/")+1));
        d = Integer.parseInt(secondNumber.substring(secondNumber.indexOf("/")+1));
    } else if (!secondNumber.contains("/")) {
        c = Integer.parseInt(secondNumber.substring(secondNumber.length()));
        d = Integer.parseInt("1");
    }
}
return input;
}