diy900
diy900

Reputation: 1

Getting error code, but don't know why

I need to write a code for a fraction calculator that can add, subtract, multiply, and divide two fractions. I have this code and am getting the error message

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)

I know the error message gives me the spot I need to fix but I cannot figure out what i have done wrong. I am still pretty new to Java so it is probably a very easy fix.

Thank you in advance.

    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;
}
}

Upvotes: 0

Views: 129

Answers (2)

Pierfrancesco Soffritti
Pierfrancesco Soffritti

Reputation: 1738

ug_'s answer is right.. by the way, your method run() contains plenty of errors (i tried to run it)

i thought a fixed version would be useful to you.. here it is: ( check the substring() methods ;) )

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;
}

Upvotes: 0

ug_
ug_

Reputation: 11440

The String.indexOf method will return -1 if the string was not found. At this snipplet you have:

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

Resulting in

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

And you likely ment is:

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

Upvotes: 2

Related Questions