I’m taking a CS course and this code is giving me problems.
while (statScan.hasNext()){
currentStat = statScan.next();
if (currentStat = h);
{
hStat++;
}
System.out.println("" + currentStat);
Look at the “if” statement. Java says “cannot convert string to boolean” and from my understanding boolean is a true/false sorta thing so what Java isn’t understanding is how to evaluate and compare the strings. How do I force it to do so?
The
=operator in Java is intended as an assignment operator, so when you writex = yyou don’t mean is x equal to y but assign value of y to x.The comparison operator is the
==operator but this will compare either primitive types (int, char, float, etc) either references to objects that is not the usual behavior you need. For anything that is not a primitive type you should consider using the method inherited byObjectwhich isboolean equals(Object o). To understand exactly why it is so, take a look here or here.