Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

1. Why??

So I was making this program to learn a bit of java, and to hopefully make my homework a bit easier. I just have 1 error, I didn't know if I need to show the whole program, but I didn't think it would hurt. The FindMass method isn't done btw.

Code:
import TerminalIO.*;

public class AtomicMass {

public static double FindMass(String x) {
if (x == "H") return 1.008;
else if (x == "He") return 4.003;
else if (x == "Li") return 6.941;
else if (x == "Be") return 9.012;
else if (x == "B") return 10.821;
else if (x == "C") return 12.011;
else if (x == "N") return 14.006;
else if (x == "O") return 16;
else if (x == "F") return 18.998;
else if (x == "Ne") return 10.180;
else if (x == "Na") return 22.99;
else if (x == "Mg") return 24.305;
else if (x == "Al") return 26.982;
else if (x == "Si") return 28.086;
else if (x == "P") return 30.974;
else if (x == "S") return 32.066;
else if (x == "Cl") return 35.453;
else if (x == "Ar") return 39.948;
else if (x == "K") return 39.098;
else if (x == "Ca") return 40.078;
else if (x == "Sc") return 44.956;
else if (x == "Ti") return 47.867;
else if (x == "V") return 50.942;
else if (x == "Cr") return 51.996;
else if (x == "Mn") return 54.938;
else if (x == "Fe") return 55.845;
else if (x == "Co") return 58.933;
else if (x == "Ni") return 58.693;
else if (x == "Cu") return 63.546;
else if (x == "Zn") return 65.38;
else if (x == "Ga") return 69.723;
else if (x == "Ge") return 72.64;
else if (x == "As") return 74.922;
else if (x == "Se") return 78.96;
else if (x == "Br") return 79.904;
else if (x == "Kr") return 83.798;
else return 0.0;
}

public static void main(String[] args) {
AtomicMass Mass = new AtomicMass();

String formula = input.readLine("Enter formula: ") + "\0";
String element;
int multiplier = 0;
int subscript;
double mass = 0.0;
int count;

while (Character.isDigit(formula.charAt(0))) {
multiplier = (multiplier * 10) + formula.charAt(0) - 48;
formula = formula.substring(1,formula.length());
}
System.out.println("Formula: " + formula);
System.out.println("Multiplier: " + multiplier);

count = 0;
while (count < formula.length() - 1) {
if (Character.isLetter(formula.charAt(count))) {
if (Character.isUpperCase(formula.charAt(count))) {
element = formula.charAt(count);	//incompatible types

if (Character.isLetter(formula.charAt(count + 1))) {
if (Character.isLowerCase(formula.charAt(count + 1))) {
element = element + formula.charAt(count + 1);

}
}
}
}

System.out.println("Element Mass: " + element + "," + Mass.FindMass(element));
if (Character.isDigit(formula.charAt(count + element.length() - 1))) {
subscript = formula.charAt(count + element.length() - 1);
mass = mass + (Mass.FindMass(element) * multiplier * subscript);
} else {
mass = mass + (Mass.FindMass(element) * multiplier);
}

System.out.println("Element: " + element);
System.out.println("Mass: " + mass);

count++;
}
}
}
Error is commented

This program should give the atomic mass of a formula entered (i.e. 2H2O should output 20.032) (any #s after element names act as subscripts)

Also you can ignore / remove the random system.out.println commands, I just put them in there to know what is happening

I apologize if you consider this code "sloppy" I'm new to this

• ctrl - f for "incompatible types" to find error

• In the future please select a more suitable title to describe the issue. Also, the actual compile time error helps too.
The problem is your datatypes. element is a string, but you are trying to give it a char. The first thing you'll need to do is initialize element to an empty string. Since its populated within an IF branch, you cannot use it outside of the if branch without giving it a default value.
Next, there are a few ways to cast a char to a string. Given the use of the Character class, I'd probably just stick with it to keep it consistent. Character has a static toString in addition to an instance toString, so you can simply use Character.toString(formula.charAt(count)); and assign that to your string.

Edit:
BTW, if you can use it, I'd recommend using a Map type to deal with the element data. Then you can associate the "H" with 1.008 directly with an mymap.get("H");. Maps are a somewhat more advanced feature of Java, and probably among the last collection you will learn (with the tree and graph probably being the last).

Edit:
Oh yes I forgot I was going to mention this. Be careful with this type of usage: if (mystring == "X"). Strings are objects in java, so comparing an object to a constant only works if that object is of that constant type. So saying String x = "X"; will allow you to compare x == "X", but saying String x = new String("X"); or pulled from a part of another string will not. If you are looking for comparison of two strings in java for their values, you should always use .equals or .equalsIgnoreCase if you don't care about the case. x == y compares two objects for their actual memory address.

• Thank you very much, I was having a lot of trouble with this

I will look into maps, and also implement string.equals as opposed to ==, (I had learned string.equals, but I guess I just forgot about it)

• Originally Posted by MrKnowItAllxx
Thank you very much, I was having a lot of trouble with this

I will look into maps, and also implement string.equals as opposed to ==, (I had learned string.equals, but I guess I just forgot about it)
Yep, its a shame that due to the same reason above strings cannot be used for switch statements either.
As for a map, like I said this is more of an advanced feature which you probably won't want to use if you are in a course for java. But a map is a collection, so you would do this:
PHP Code:
public class AtomicMass {
public final static
java.util.Map<StringDoublemass = new java.util.Hashtable<StringDouble>();
static
{

mass.put("H"1.008);

mass.put("He"4.003);

// ...

}
public static
double findMass(String element)
{
return
mass.get(element);
}

// ...

You can see that in this particular example it will not help you much by way of code cutdown, but it would sure make adding easy (you only have 36 elements from the looks of it, which is a little short of the. . . 117 is it?). I noticed as well that these methods you are using are static except for your input method (which I swapped to a Scanner since I don't have TerminalIO.KeyboardReader as that is custom). This indicates to me that you have not learned Object usage at this point dealing with instances, so the above you should avoid, but keep aside for when you do start dealing with collections.

What I can see this class becoming is that of Compound which would either contain or extend a Map<String, Element> where the Element is an instance of an element that would fully describe the element properties (half lifes, full name, periodic number and weights, the person and year discovered, etc). In turn this would become a way to verify a provided element grouping as being compatible, so you'll be able to do some really neat stuff. Unfortunately from chemistry world, I haven't been in a chem class for over 15 years, so I couldn't help you on that.

•