0

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;
}
joguel10
  • 9
  • 3

1 Answers1

0

It should be

 String a = f.makeMolecular();
 String b = f.makeMolecular();

 if(a.equals(b))
     return true;
 else
     return false;
Amriteya
  • 1,118
  • 15
  • 37
  • 1
    It should be `return a.equals(b);`. Or `Objects.equals(a, b)` if `a` might be null. – Andy Turner Apr 15 '16 at 07:22
  • Thanks for the quick response, but when i try this solution i get the following error: "Incompatible types: void cannot be converted to java.lang.String – joguel10 Apr 15 '16 at 07:23
  • What is return type of makeMolecular() method? – Amriteya Apr 15 '16 at 07:24
  • @Amriteya `void`, clearly. – Andy Turner Apr 15 '16 at 07:25
  • I am not sure, but it is from public void makeMolecular() who then turns it into an ArrayList i think ? Sorry if this is confusing – joguel10 Apr 15 '16 at 07:27
  • @Andy He hasn't given the code for the method makeMolecular(). Couldn't be sure about that – Amriteya Apr 15 '16 at 07:27
  • the code of the method makeMolecular() is as follow: public void makeMolecular() { char element = ' '; int atoms = 0; ArrayList molecule = new ArrayList(); – joguel10 Apr 15 '16 at 07:29
  • while (terms.size() > 0){ if (molecule.size() > 0 && molecule.get(molecule.size() - 1).getElement() == nextElement().getElement() ){ atoms = molecule.get(molecule.size() - 1).getAtoms() + nextElement().getAtoms(); element = nextElement().getElement(); molecule.add(new Term(element,atoms)); molecule.remove(molecule.size() - 1); } else { molecule.add(nextElement()); } terms.remove(nextElement()); } terms = molecule; – joguel10 Apr 15 '16 at 07:30
  • Yes, clearly: "**void** cannot be converted to java.lang.String". And no, @HankyPanky, that `a` is undefined is not the reason, since it is defined in this code. Redefining `b` would give a different error first. – Andy Turner Apr 15 '16 at 07:31
  • I can make an edit to the original questions to include the method makeMolecular() if that clears things up. – joguel10 Apr 15 '16 at 07:34