Reputation: 314
I'm 14 so we're learning about scientific equations and ions in school. Keep in mind I am very new to java and got the map thing from googling. I am also new to scientific equations hehe, so if you see anything please point it out (Although that is not part of the main question). I need practice in both areas so why not create a java program about science? ;) Anyway, I've got to the stage where I need to get the last 2 items in the String and change them to an integer and then I think I need to do algebra in java to solve a little equation for getting the little number infront of the letters. That's all I need to do then I can carry on making my program :D. Any help is very much appreciated and if you see anything else please point it out. Thanks!
This is the file: Run.java
package scientificFormula;
public class Run {
public static void main(String[] args) {
Formula formula = new Formula();
formula.compound1 = args[0];
formula.compound2 = args[1];
String theFormula = formula.createFormula();
System.out.println("Compound: " + args[0] + " " + args[1]
+ " = " + theFormula);
}
}
and Formula.java
:
package scientificFormula;
import java.util.HashMap;
import java.util.Map;
public class Formula {
String compound1;
String compound2;
static private Map<String, String> map = new HashMap<String, String>();
void initiateIons() {
//1+
map.put("Hydrogen", "H^1+");
map.put("Lithium", "Li^1+");
map.put("Sodium", "Na^1+");
map.put("Potassium", "K^1+");
map.put("Rubidium", "Rb^1+");
//2+
map.put("Magnesium", "Mg^2+");
map.put("Calcium", "Ca^2+");
map.put("Strontium", "Sr^2+");
//3+
map.put("Aluminium", "Al^3+");
//3-
map.put("Nitrogem", "N^-3");
map.put("Phosphorus", "P^-3");
//2-
map.put("Oxygen", "O^-2");
map.put("Sulfar", "S^-2");
map.put("Selenium", "Se^-2");
//1-
map.put("Fluorine", "F^-1");
map.put("Chlorine", "Cl^-1");
map.put("Bromine", "Br^-1");
map.put("Iodine", "I^-1");
}
String createFormula() {
initiateIons();
//Example Calcium Iodine:
//2x + -1y = 0
//x = 1 and y = 2
String symbol1 = map.get(compound1);
String symbol2 = map.get(compound2);
return symbol1 + symbol2;
}
}
edit:
In reply to the 2 comments below from Jigar and Eran:
I went in properties and changed the arguments on Run.java
...
input:
Sodium Chlorine
output:
Compound: Sodium Chlorine = Na^1+Cl^1-
for example this string: "Iodine", "I^-1" and the 2 items are -1 I want to make that int. Thanks.
Upvotes: 0
Views: 179
Reputation: 4349
It sounds like you want the last two characters of the string not bits. If I'm not mistaken you want to grab the -2
at the end of something like Oxygen.
This can be done with these lines of code:
int i = Integer.parseInt(str.replace("+", "").substring(str.length() - 2));
The replace is necessary because parseInt does not like +
in front of values only -
.
Using Hot Licks suggestion of split
you can do something like:
String[] symbol1Parts = symbol1.split("\\^");
int symbol1Int = Integer.parseInt(symbol1Parts[1].replace("+", "")); // the [1] assumes that there will only be one ^ character before the charge
String[] symbol2Parts = symbol2.split("\\^");
int symbol2Int = Integer.parseInt(symbol2Parts[1].replace("+", "")); // the [1] assumes that there will only be one ^ character before the charge
Upvotes: 4