Created
January 31, 2019 15:36
-
-
Save LucioSaldivar/1f78eebc54eb2fa58ef6cae6c4a916a8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.regex.Matcher; | |
import java.util.regex.Pattern; | |
public class Calculator { | |
public static void main(String[] args) { | |
double answer, num1, num2; | |
String input = Utils.getInput("Type in Equation"); // type ANY oporation | |
Pattern p = Pattern.compile("\\d+"); // \\d+ allows you to add first digit plus any others | |
Pattern z = Pattern.compile("\\p{Punct}"); | |
Matcher l = z.matcher(input); | |
Matcher m = p.matcher(input); | |
String bucket = ""; | |
String oporator = ""; | |
while (l.find()) { | |
oporator = l.group(); | |
} | |
while (m.find()) { | |
bucket += " " + m.group(); | |
} | |
bucket = bucket.trim(); | |
String[] buckets = bucket.split(" "); | |
double[] numbuck = new double[buckets.length]; | |
for (int i = 0; i < buckets.length; i++) { | |
numbuck[i] = Double.parseDouble(buckets[i]); | |
} | |
num1 = numbuck[0]; | |
num2 = numbuck[1]; | |
switch (oporator) { | |
case "+": | |
answer = num1 + num2; | |
System.out.println(answer); | |
break; | |
case "-": | |
answer = num1 - num2; | |
System.out.println(answer); | |
break; | |
case "*": | |
answer = num1 * num2; | |
System.out.println(answer); | |
break; | |
case "/": | |
answer = num1 / num2; | |
System.out.println(answer); | |
break; | |
default: | |
System.out.println("Sorry, try again."); | |
break; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment