user1129139
user1129139

Reputation:

Type mismatch: cannot convert from integer to boolean

public boolean clearSelection() {
    int i = 0;
    if (!this.m_SelectedComps.isEmpty()) {
        i = 1;
        Iterator localIterator = this.m_SelectedComps.iterator();
        while (localIterator.hasNext())
            ((AnnotComponent) localIterator.next()).remove();
        this.m_SelectedComps.clear();
    }

    return i;
}

How to convert the integer to boolean?

Upvotes: 19

Views: 123303

Answers (7)

Mohsin
Mohsin

Reputation: 86

You can't type cast between integer and boolean in Java but you could use the following technique which I use oftenly:

To convert boolean into integer:

int i;
return i != 0;

To convert integer into boolean:

boolean b;
return b ? 1 : 0;

Upvotes: 1

Filip Spiridonov
Filip Spiridonov

Reputation: 36250

Convert int to boolean:

return i > 0;

Upvotes: 5

tbraun
tbraun

Reputation: 2666

I know this thread is old but wanted add some code that helped me and might help others searching this...

You could use org.apache.commons.lang api to convert an int to boolean using the BooleanUtils class:

BooleanUtils.toBoolean(int value)

"Converts an int to a boolean using the convention that zero is false." (Javadocs)


Here are the Maven & Gradle dependencies, just make sure you check you're using the latest version on the link http://mvnrepository.com/artifact/org.apache.commons/commons-lang3

Maven Dependency:

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.4</version>
</dependency>

Gradle Dependency:

'org.apache.commons:commons-lang3:3.4'

Upvotes: 16

Hogan
Hogan

Reputation: 70528

Try using this return

return i == 1;

or just use a boolean to start (with a better name):

 public boolean clearSelection()
  {
    boolean flag = false;
    if (!this.m_SelectedComps.isEmpty())
    {
      flag = true;
      Iterator localIterator = this.m_SelectedComps.iterator();
      while (localIterator.hasNext())
        ((AnnotComponent)localIterator.next()).remove();
      this.m_SelectedComps.clear();
    }
    return flag;
  }

It continues to mystify me why people use i -- a horrible variable name. Looks like 1 and does not convey any meaning.

Upvotes: 46

Priyank Doshi
Priyank Doshi

Reputation: 13161

May be you can just modify your return statement without much change to code as below:

return i > 0 ? true : false ;

Upvotes: 16

dervel
dervel

Reputation: 78

public boolean clearSelection(){
    int i = 0;
    if (!this.m_SelectedComps.isEmpty())
    {
        i = 1;
        Iterator localIterator = this.m_SelectedComps.iterator();
        while (localIterator.hasNext())
            ((AnnotComponent)localIterator.next()).remove();
        this.m_SelectedComps.clear();
     }
     return (i!=0);
}

Upvotes: 1

MByD
MByD

Reputation: 137442

Declare i as a boolean:

public boolean clearSelection()
{
    boolean i = false;
    if (!this.m_SelectedComps.isEmpty())
    {
        i = true;
        Iterator localIterator = this.m_SelectedComps.iterator();
        while (localIterator.hasNext())
          ((AnnotComponent)localIterator.next()).remove();
        this.m_SelectedComps.clear();
    }
    return i;
}

Upvotes: 2

Related Questions