3

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?

Peter O.
  • 32,158
  • 14
  • 82
  • 96

4 Answers4

4

the problem is that you assign it instead of ask if it's equal (assign '=', equals '==')
this is the solution for that line: also if it's strings use .equals()

if (currentStat.equals(h))
Goran Štuc
  • 581
  • 4
  • 15
1

The = operator in Java is intended as an assignment operator, so when you write x = y you 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 by Object which is boolean equals(Object o). To understand exactly why it is so, take a look here or here.

Community
  • 1
  • 1
Jack
  • 131,802
  • 30
  • 241
  • 343
1

A few problems here:

if (currentStat =  h);

First, you have a semicolon at the end which will cause the if-statement to not function as you want. It is basically conditionally executing an empty statement. The following block of code (which you intended to be conditional) will always be run. Remove the semicolon to avoid this.

Also, you only have = instead of ==. This means you are assigning the value of h to currentStat. This is a classic, classic typo. As @YogendraSingh mentioned in the other answer, you should use the equals() method to perform comparisons on strings in Java. Otherwise you are only comparing to see if the two items are exactly the same object. Do detect when the same string text is held in two different objects you need to use equals().

So change this line to:

if (h.equals(currentStat))
Ben Kelly
  • 1,304
  • 7
  • 9
0

Assuming h is a string, use equals method for string comparison as:

      if(currentStat.equals(h))

Currently you are using assignment operator =, which assigns the string value of h to currentStat.

When you change the operator to comparison operator == then it will compare the object instance equality and not the value, which I think you don't want/

Yogendra Singh
  • 33,927
  • 6
  • 63
  • 73
  • @user1869638: I am really surprised this time with the suggestions of using `==` operator to compare the string and that also getting voted so much :) I have never seen this before. May be, I am missing something big here. Since you are using `scan.next()`, I know its string and hence I advice you to use `equals` not `==`. – Yogendra Singh Dec 02 '12 at 01:31