-1

I'm new to java and trying to create a simple calculator using methods. I know this isn't perfect, but I'm trying to take a string, convert it to doubles, and then add, subtract, whatever. This is what I've done so far... the code runs... but it throws a weird illegal exception saying that "+" is a dangling modifier. My question is why? It seems like this would work.... but obviously it doesn't.

Code:

import java.util.Scanner;
public class calculator4 {
    
    public static void main(String [] args){
        double sum;
        double subtract;
        double multi;
        double dividend;
        String problem;
        System.out.println("This is a program designed to perform basic calculator functions.");
        System.out.println("Please enter a problem using the following operations:");
        System.out.println("For addition, use +.");
        System.out.println("For subtraction, use -.");
        System.out.println("For multiplication, use *");
        System.out.println("For division, use /.");
        System.out.println("These are the only accepted values.");
        System.out.println("Please enter your problem:");
        Scanner scnr = new Scanner(System.in);
        problem = scnr.nextLine();
        if(problem.contains("+")){
            String [] part = problem.split("+");
            String part1 = part [0];
            String part2 = part [1];
            double add1 = Double.parseDouble(part1);
            double add2 = Double.parseDouble(part2);
            sum = add1 + add2;
            
        }
        else if(problem.contains("-")){
            String [] part = problem.split("-");
            String part1 = part [0];
            String part2 = part [1];
            double subtract1 = Double.parseDouble(part1);
            double subtract2 = Double.parseDouble(part2);
            subtract = subtract1 - subtract2;
        }
        else if(problem.contains("*")){
            String [] part = problem.split("*");
            String part1 = part [0];
            String part2 = part [1];
            double multi1 = Double.parseDouble(part1);
            double multi2 = Double.parseDouble(part2);
            multi = multi1 * multi2;

        }
        else if(problem.contains("/")){
            String [] part = problem.split("/");
            String part1 = part [0];
            String part2 = part [1];
            double dividend1 = Double.parseDouble(part1);
            double dividend2 = Double.parseDouble(part2);
            dividend = dividend1 * dividend2;
        }
    }
        public double addition(double sum){
            return sum;
        }
        public double subtraction(double subtract){
            return subtract;
        }
        public double multipliation(double multi){
            return multi;
        }
        public double division(double dividend){
            return dividend;
        }
        calculator4 addition = new calculator4();
        calculator4 subtraction = new calculator4();
        calculator4 multiplication = new calculator4();
        calculator4 division = new calculator4();
        

    }
Jens
  • 67,715
  • 15
  • 98
  • 113
newbie
  • 1
  • Using Java naming conventions will make your code easier to read. A class should be named starting with an uppercase letter. So `Calculator4`, not `calculator4`. – Basil Bourque Apr 19 '21 at 06:28
  • Format your code sample properly. Either indent all of it with four SPACE characters, or surround with a pair of triple back-ticks. – Basil Bourque Apr 19 '21 at 06:28
  • 1
    Does this answer your question? [java.util.regex.PatternSyntaxException: Dangling meta character '+' near index 0 +](https://stackoverflow.com/questions/40246231/java-util-regex-patternsyntaxexception-dangling-meta-character-near-index-0) – Jalil.Jarjanazy Apr 19 '21 at 06:30
  • Yes! Thank you very much! – newbie Apr 19 '21 at 06:32
  • In the future, before posting, search for your error message. – Basil Bourque Apr 19 '21 at 06:32

1 Answers1

-1

It interprets the plus as meta character. Try to escape the "+" with "\" like:

if(problem.contains("\\+")){
   ...
}

You have to use a double Backslash to escape the Backslash itself, too.

This applies to the other operands (e.g -,*, ) also.