-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathStringHandle.java
More file actions
103 lines (88 loc) · 3.32 KB
/
StringHandle.java
File metadata and controls
103 lines (88 loc) · 3.32 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
92
93
94
95
96
97
98
99
100
101
102
103
/*
Jacob John
*/
package assignment2;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class Coefficient {
public static String extractCoefficient(String str) {
String pattern = "^\\d";
// Create a Pattern object
Pattern r = Pattern.compile(pattern);
// Now create matcher object.
Matcher m = r.matcher(str);
if (m.find()) {
return m.group(0);
} else {
return "1";
}
}
public static String removeCoefficient(String str) {
String pattern = "[^\\d](.*)";
// Create a Pattern object
Pattern r = Pattern.compile(pattern);
// Now create matcher object.
Matcher m = r.matcher(str);
if (m.find()) {
return m.group(0);
} else {
return str;
}
}
}
public class StringHandle {
public static void main(String args[]) {
int i;
// get input
Scanner sc = new Scanner(System.in);
System.out.print("Enter equation: ");
String eq = sc.nextLine();
sc.close();
// define re for splits
String arrow = "(\\s->\\s)|(\\s->)|(->\\s)|(->)";
// split into reactants and products
String[] words = eq.split(arrow);
String[] reactant = words[0].split("\\+");
String[] product = words[1].split("\\+");
String[] rmoles = new String[10];
String[] pmoles = new String[10];
for (i = 0; i < reactant.length; i++) {
reactant[i] = reactant[i].replaceAll("\\s+", "");
rmoles[i] = Coefficient.extractCoefficient(reactant[i]);
}
for (i = 0; i < product.length; i++) {
product[i] = product[i].replaceAll("\\s+", "");
pmoles[i] = Coefficient.extractCoefficient(product[i]);
}
System.out.print("(1) Reactants are ");
if (reactant.length > 1) {
for (i = 0; i < reactant.length; i++) {
if (i == 0)
System.out.print(rmoles[i] + " mole/s of " + Coefficient.removeCoefficient(reactant[i]));
else if (i == reactant.length - 1)
System.out.print(" and " + rmoles[i] + " mole/s of "
+ Coefficient.removeCoefficient(reactant[i]));
else
System.out.print(", " + rmoles[i] + " mole/s of " + Coefficient.removeCoefficient(reactant[i]));
}
} else {
System.out.print(rmoles[0] + " mole/s of "
+ Coefficient.removeCoefficient(reactant[0]));
}
System.out.println();
System.out.print("(2) Products are ");
if (product.length > 1) {
for (i = 0; i < product.length; i++) {
if (i == 0)
System.out.print(pmoles[i] + " mole/s of " + Coefficient.removeCoefficient(product[i]));
else if (i == product.length - 1)
System.out.print(" and " + pmoles[i] + " mole/s of " + Coefficient.removeCoefficient(product[i]));
else
System.out.print(", " + pmoles[i] + " mole/s of " + Coefficient.removeCoefficient(product[i]));
}
} else {
System.out.print(pmoles[0] + " mole/s of " + Coefficient.removeCoefficient(product[0]));
}
}
}