-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddition.java
More file actions
91 lines (73 loc) · 3.17 KB
/
Addition.java
File metadata and controls
91 lines (73 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package arithmeticexpressiontutorial;
/**
*
* @author Matthew Garcia
*/
public class Addition {
public String findAddition(String expression) {
int numberOfAddition = 0;
int index = 0;
while (index < expression.length()) {
String character = expression.substring(index, index + 1);
if (character.equals("+")) {
numberOfAddition++;
}
index++;
}
System.out.println("Number Addition" + numberOfAddition);
String replace = expression;
if (numberOfAddition == 0) {
return replace;
} else {
while (numberOfAddition > 0) {
index = 0;
while (index < replace.length() - 1) {
String character = replace.substring(index, index + 1);
if (character.equals("+")) {
int placeHolder = index;
String digit = "";
String secondDigit = "";
character = replace.substring(index - 1, index);
//first digit
while (!character.equals("+") && !character.equals("-")
&& !character.equals("/") && !character.equals("*")) {
index--;
if (index <= 0) {
character = "+";
} else {
character = replace.substring((index - 1), index);
}
}
digit = replace.substring(index, placeHolder);
// System.out.println("Digit is " + digit);
index = placeHolder;
character = replace.substring(index + 1, index + 2);
//5*45
while (!character.equals("+") && !character.equals("-")
&& !character.equals("/") && !character.equals("*")) {
secondDigit += character;
index += 1;
if (index >= replace.length() - 1) {
character = "+";
} else {
character = replace.substring(index + 1, index + 2);
}
}
// System.out.println("Second Digit " + secondDigit);
double value = Double.valueOf(digit) + Double.valueOf(secondDigit);
replace = replace.replace(digit + "+" + secondDigit, String.valueOf(value));
numberOfAddition--;
index = 0;
}
index++;
}
}
return replace;
}
}
}