-1

Ok so I'm trying to make a command line calculator where it detects if your trying to add/subtract/multiply/divide using the String.contains method. Multiplication and division work just fine, but when I try to add or subtract, it spits out a bunch of errors. Can someone please tell me what I'm doing wrong?

import java.util.Scanner;

public class CalculatorCode {
    public static void main(String[] args) {
        while(true){
             Scanner input = new Scanner(System.in);
             String userinput = input.nextLine();
    if(userinput.contains("x")){    
        String[] multiply = userinput.split("x");
        String multiply1 = multiply[0];
        String multiply2 = multiply[1];
        double a = Double.parseDouble(multiply1);
        double b = Double.parseDouble(multiply2);
        double multoutput = a * b;
        System.out.println(a + "x" + b + "=" + multoutput);
            }
    if(userinput.contains("/")){
        String[] divide = userinput.split("/");
        String divide1 = divide[0];
        String divide2 = divide[1];
        double c = Double.parseDouble(divide1);
        double d = Double.parseDouble(divide2);
        double divideoutput = c/d;
        System.out.println(c + "/" + d + "=" + divideoutput);
            }
    if(userinput.contains("-")){
        String[] subtract = userinput.split("-");
        String subtract1 = subtract[1];
        String subtract2 = subtract[2];
        double e = Double.parseDouble(subtract1);
        double f = Double.parseDouble(subtract2);
        double suboutput = e - f;
        System.out.println(e + "-" + f + "=" + suboutput);
            }
    if(userinput.contains("+")){
        String[] add = userinput.split("+");
        String add1 = add[1];
        String add2 = add[2];
        double g = Double.parseDouble(add1);
        double h = Double.parseDouble(add2);
        double addoutput = g - h;
        System.out.println(g + "-" + h + "=" + addoutput);
            }
        }
    }
}

Errors for addition:

Exception in thread "main" java.util.regex.PatternSyntaxException: Dangling meta character '+' near index 0
+
^
    at java.util.regex.Pattern.error(Pattern.java:1713)
    at java.util.regex.Pattern.sequence(Pattern.java:1878)
    at java.util.regex.Pattern.expr(Pattern.java:1752)
    at java.util.regex.Pattern.compile(Pattern.java:1460)
    at java.util.regex.Pattern.(Pattern.java:1133)
    at java.util.regex.Pattern.compile(Pattern.java:823)
    at java.lang.String.split(String.java:2292)
    at java.lang.String.split(String.java:2334)
    at CalculatorCode.main(CalculatorCode.java:36)

Subtraction Errors:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
    at CalculatorCode.main(CalculatorCode.java:29)
MultiplyByZer0
  • 6,302
  • 3
  • 32
  • 48
Illorum
  • 123
  • 10

7 Answers7

2

please change your code to below .You have used the array indexes wrongly.

    import java.util.Scanner;

public class CalculatorCode {
    public static void main(String[] args) {
        while(true){
             Scanner input = new Scanner(System.in);
             String userinput = input.nextLine();
    if(userinput.contains("x")){    
        String[] multiply = userinput.split("x");
        String multiply1 = multiply[0];
        String multiply2 = multiply[1];
        double a = Double.parseDouble(multiply1);
        double b = Double.parseDouble(multiply2);
        double multoutput = a * b;
        System.out.println(a + "x" + b + "=" + multoutput);
            }
    if(userinput.contains("/")){
        String[] divide = userinput.split("/");
        String divide1 = divide[0];
        String divide2 = divide[1];
        double c = Double.parseDouble(divide1);
        double d = Double.parseDouble(divide2);
        double divideoutput = c/d;
        System.out.println(c + "/" + d + "=" + divideoutput);
            }
    if(userinput.contains("-")){
        String[] subtract = userinput.split("\\-");
        String subtract1 = subtract[0];
        String subtract2 = subtract[1];
        double e = Double.parseDouble(subtract1);
        double f = Double.parseDouble(subtract2);
        double suboutput = e - f;
        System.out.println(e + "-" + f + "=" + suboutput);
            }
    if(userinput.contains("+")){
        String[] add = userinput.split("\\+");
        String add1 = add[0];
        String add2 = add[1];
        double g = Double.parseDouble(add1);
        double h = Double.parseDouble(add2);
        double addoutput = g + h;
        System.out.println(g + "+" + h + "=" + addoutput);
            }
        }
    }
}
Lakshmi
  • 2,204
  • 3
  • 29
  • 49
2

check this lines in add and subtract

String add1 = add[1];
        String add2 = add[2];

array index starts from 0

Looking into other multiplications and dividing statements from your post I assume that you are passing 3 charcters Thats why you are getting java.lang.ArrayIndexOutOfBoundsException

try with this

String add1 = add[0];
        String add2 = add[1];

Similarily for the subtractions use this

String subtract1 = subtract[0];
        String subtract2 = subtract[1];

after changing the above two lines your full code will be like this

import java.util.Scanner;

public class CalculatorCode {
    public static void main(String[] args) {
        while(true){
             Scanner input = new Scanner(System.in);
             String userinput = input.nextLine();
    if(userinput.contains("x")){    
        String[] multiply = userinput.split("x");
        String multiply1 = multiply[0];
        String multiply2 = multiply[1];
        double a = Double.parseDouble(multiply1);
        double b = Double.parseDouble(multiply2);
        double multoutput = a * b;
        System.out.println(a + "x" + b + "=" + multoutput);
            }
    if(userinput.contains("/")){
        String[] divide = userinput.split("/");
        String divide1 = divide[0];
        String divide2 = divide[1];
        double c = Double.parseDouble(divide1);
        double d = Double.parseDouble(divide2);
        double divideoutput = c/d;
        System.out.println(c + "/" + d + "=" + divideoutput);
            }
    if(userinput.contains("-")){
        String[] subtract = userinput.split("-");
        String subtract1 = subtract[0];
        String subtract2 = subtract[1];
        double e = Double.parseDouble(subtract1);
        double f = Double.parseDouble(subtract2);
        double suboutput = e - f;
        System.out.println(e + "-" + f + "=" + suboutput);
            }
    if(userinput.contains("+")){
        String[] add = userinput.split("+");
        String add1 = add[0];
        String add2 = add[1];
        double g = Double.parseDouble(add1);
        double h = Double.parseDouble(add2);
        double addoutput = g - h;
        System.out.println(g + "-" + h + "=" + addoutput);
            }
        }
    }
}

Please use nested if else for better efficiency.May be this will help you to know more

Also as other answer suggests you need to escape - and +

Community
  • 1
  • 1
SpringLearner
  • 13,738
  • 20
  • 78
  • 116
2

- Your problem here are special symbols like + and -.

- You need to use escape characters to eradicate these errors.

Eg:

prefix them with \\

\\+ and \\-
Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
1

Because the + character is a special character in regular expressions.

You'll need to escape the + in your call to split(). Note that you'll need two backslashes since a single backslash is used for escape sequences such as a new-line and tab character.

Example:

String[] add = userinput.split("\\+");
PakkuDon
  • 1,627
  • 4
  • 22
  • 21
1

characters like + , - are used for regex patterns hence you get the dangling meta character exception. Instead you can try to use StringTokenizer. For eg in your case for addition:-

 if(userinput.contains("+")){
        StringTokenizer st = new StringTokenizer(userinput, "+");
        double addedNumber = 0;
        while(st.hasMoreTokens()){
            String add = st.nextToken();
            addedNumber = addedNumber+Double.parseDouble(add);
        }
        System.out.println("Your result is:- "+addedNumber);
    }
Madhura
  • 551
  • 5
  • 18
1

You are using:

String add1 = add[1];
String add2 = add[2];
// and
String subtract1 = subtract[1];
String subtract2 = subtract[2];

but you are supposed to be using (just like with multiplication/divison):

String add1 = add[0];
String add2 = add[1];
// and
String subtract1 = subtract[0];
String subtract2 = subtract[1];

Also, these lines:

double addoutput = g - h;
System.out.println(g + "-" + h + "=" + addoutput);

are for adding, so you should change them to:

double addoutput = g + h;
System.out.println(g + "+" + h + "=" + addoutput);

Finally, + is a special character that needs to be escaped with a \\

So you need to change this:

String[] add = userinput.split("+");

to this:

String[] add = userinput.split("\\+");
Michael Yaworski
  • 13,410
  • 19
  • 69
  • 97
  • @JqueryLearner I don't care. You also pasted all of his code back for no reason, still with two errors, which I have mentioned in my edit. – Michael Yaworski Jan 03 '14 at 06:33
  • I dont copy others answers.i have pasted all the code after explaining what to change and where to change.Check lakshmi answer,Its perfect.Also my answer was before Op posted the stacktrace – SpringLearner Jan 03 '14 at 06:36
  • @JqueryLearner Neither do I. I post answers specifically showing what is wrong and needs to be changed. He posted the entire class. That's fine and all, but it's verbose. Just because I posted after someone else doesn't mean I copied anyone. I did all of this myself in my own testing workspace. – Michael Yaworski Jan 03 '14 at 06:38
  • may be you might have copied others answers but if you are answer is similar to other answers then I think its just a copy unless your reason is different from the others.I can just say this is the **combination of all the answers** posted over here – SpringLearner Jan 03 '14 at 06:41
  • 1
    It's a good answer. Who cares whether he copied others or not? – Dawood ibn Kareem Jan 03 '14 at 06:41
0

use try catch block to find where exactly the issue is. I guess problem might be due to space or termination character in input. Like "2 + 3" or "2+3\0" instead of "2+3"

Array index starts with 0. I guess u know that. :)

Venkatesh K
  • 4,364
  • 4
  • 18
  • 26