I have a question regarding comparing two strings? and checking if they are identical. Below is the question explenation
// returns true if f.makeMolecular() is is equal to this.makeMolecular
// e.g. terms = {Term('B',3),Term('A',6)} and f = {Term('B',1),Term('A',3),Term('B',1),Term('A',3)}
// would return true
// The makeMolecular() method sorts the Term Strings into Alphabetical order with the number linked to the letter added and following behind the letter.
E.G Both of the makeMolecular should return a string of "A6B3"
I want to check if they are identical using and IF statement and taking advatange of the == method in Java.
Below folows one solution to checking if the strings are identical, but as mentioned i would like to do it with an iff statement.
{
this.makeMolecular();
f.makeMolecular();
return identical(f);
}
This is what i have tried so far:
char b = f.makeMolecular();
char b = f.makeMolecular();
if (a==b){
return true;
}
else{
return false;
}
and i get the following error: Incompatible types: void cannot be converted to char. I am quite certain the use of "char" is incorrect here, but i am not sure of what to use instead.
So I am wondering how to convert these strings(?) to a constant and then checking if they are identical via an IF-statement.
// Edit:
By request; this is the method for makeMolecular()
public void makeMolecular()
{
char element = ' ';
int atoms = 0;
ArrayList<Term> mol = new ArrayList<Term>();
while (terms.size() > 0){
if (mol.size() > 0 && mol.get(mol.size() - 1).getElement() == nextElement().getElement() ){
atoms = mol.get(mol.size() - 1).getAtoms() + nextElement().getAtoms();
element = nextElement().getElement();
mol.remove(mol.size() - 1);
mol.add(new Term(element,atoms));
}
else {
mol.add(nextElement());
}
terms.remove(nextElement());
}
terms = mol;
}