if statement - If Else: String Equality (Java) -
lab description : compare 2 strings see if each of 2 strings contains same letters in same order.
this have far far:
import static java.lang.system.*; public class stringequality { private string wordone, wordtwo; public stringequality() { } public stringequality(string one, string two) { setwords (wordone, wordtwo); } public void setwords(string one, string two) { wordone = one; wordtwo = two; } public boolean checkequality() { if (wordone == wordtwo) return true; else return false; } public string tostring() { string output = ""; if (checkequality()) output += wordone + " not have same letters " + wordtwo; else output += wordone + " have same letters " + wordtwo; return output; } }
my runner looks this:
import static java.lang.system.*; public class stringequalityrunner { public static void main(string args[]) { stringequality test = new stringequality(); test.setwords(hello, goodbye); out.println(test); } }
everything compiling except runner. keeps saying hello , goodbye aren't variables. how can fix program not read hello , goodbye variables, strings?
problem code checkequality()
, comparing string's position in memory when use ==
use .equals()
check string
public boolean checkequality() { if (wordone == wordtwo) //use wordone.equals(wordtwo) here return true; else return false; }
Comments
Post a Comment