# how to convert bolean to int

• (2 Pages)
• 1
• 2

## 18 Replies - 600 Views - Last Post: 30 May 2019 - 08:27 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=416283&amp;s=0bb2b268677ad44d1808285ae282bf43&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 [email protected]

Reputation: 2
• Posts: 174
• Joined: 24-February 15

# how to convert bolean to int

Posted 29 May 2019 - 04:01 AM

I got an error in my code that I can't convert boolean to an integer, can somebody help me with that? I need to check if a number is odd or not.

Solution.java:17: error: incompatible types: int cannot be converted to boolean
if(N % 2)
^
1 error

```        if(N % 2)
{
System.out.println("Weird");
}
else if(N == 0 && N >= 3 && N <= 5)
{
System.out.println("Not Weird");
}
else if(N == 0 && N >= 6 && N <= 20)
{
System.out.println("Weird");
}
else if(N == 0 && N >= 20)
{
System.out.println("Not Weird");
}
scanner.close();

```

This post has been edited by [email protected]: 29 May 2019 - 04:02 AM

Is This A Good Question/Topic? 0

## Replies To: how to convert bolean to int

### #2 NormR

• D.I.C Lover

Reputation: 775
• Posts: 5,834
• Joined: 25-December 13

## Re: how to convert bolean to int

Posted 29 May 2019 - 04:08 AM

What value is the statement on line 1 testing for?
The % operator returns an int value.
The if statement requires a boolean value? For example the == operator returns a boolean value.

### #3 [email protected]

Reputation: 2
• Posts: 174
• Joined: 24-February 15

## Re: how to convert bolean to int

Posted 29 May 2019 - 04:19 AM

I have put now if(N % 2 == 0)

### #4 NormR

• D.I.C Lover

Reputation: 775
• Posts: 5,834
• Joined: 25-December 13

## Re: how to convert bolean to int

Posted 29 May 2019 - 04:29 AM

Quote

I have put now if(N % 2 == 0)

Does the code now do what you want?

### #5 [email protected]

Reputation: 2
• Posts: 174
• Joined: 24-February 15

## Re: how to convert bolean to int

Posted 29 May 2019 - 04:33 AM

Not really expected output needs to be not weird: https://imgur.com/oOvnmsh

### #6 NormR

• D.I.C Lover

Reputation: 775
• Posts: 5,834
• Joined: 25-December 13

## Re: how to convert bolean to int

Posted 29 May 2019 - 04:40 AM

What values of N should give the desired output result?

### #7 astonecipher

• Senior Systems Engineer

Reputation: 2876
• Posts: 11,250
• Joined: 03-December 12

## Re: how to convert bolean to int

Posted 29 May 2019 - 04:44 AM

So, you start out [attempting to] test a condition, but it cannot be evaluated because it is not a true/ false question. You fix the expression so that it does, and that removes the initial error. However, did you correct the expressions in the rest of the code?
```if(N % 2 == 0) <--- Fixed
{
System.out.println("Weird");
}
else if(N == 0 && N >= 3 && N <= 5) <--- Not Fixed
{
System.out.println("Not Weird");
}
else if(N == 0 && N >= 6 && N <= 20) <--- Not Fixed
{
System.out.println("Weird");
}
else if(N == 0 && N >= 20) <--- Not Fixed
{
System.out.println("Not Weird");
}
scanner.close();

```

So, are you trying to use the modulus on N or is that for a single condition only?

### #8 Atspulgs

Reputation: 100
• Posts: 537
• Joined: 29-July 09

## Re: how to convert bolean to int

Posted 29 May 2019 - 06:46 AM

Input: 24
Expected output: Not Weird
----

In your logic you're checking if N % 2 == 0 which is true for any even number.
In this case, the even number would be considered weird because 24 fully divides by 2 without any remainders... therefore the condition would evaluate as true.

Also...
N == 0 && N >= 3 && N <= 5

If N == 0 it can never be greater or equal to 3 nor can it ever be less than or equal to 5. Even if the number is between 3 and 5, it will not be 0 therefore the whole condition would be false.
Similarly for the following...

-------

For the answer of the original question... in java boolean is special and it can only be true or false. If you wanted to represent it as a number you would have to do something like this... myBool?1:0

This post has been edited by Atspulgs: 29 May 2019 - 06:48 AM

### #9 [email protected]

Reputation: 2
• Posts: 174
• Joined: 24-February 15

## Re: how to convert bolean to int

Posted 30 May 2019 - 02:55 AM

astonecipher, on 29 May 2019 - 04:44 AM, said:

So, you start out [attempting to] test a condition, but it cannot be evaluated because it is not a true/ false question. You fix the expression so that it does, and that removes the initial error. However, did you correct the expressions in the rest of the code?
```if(N % 2 == 0) <--- Fixed
{
System.out.println("Weird");
}
else if(N == 0 && N >= 3 && N <= 5) <--- Not Fixed
{
System.out.println("Not Weird");
}
else if(N == 0 && N >= 6 && N <= 20) <--- Not Fixed
{
System.out.println("Weird");
}
else if(N == 0 && N >= 20) <--- Not Fixed
{
System.out.println("Not Weird");
}
scanner.close();

```

So, are you trying to use the modulus on N or is that for a single condition only?

### #10 NormR

• D.I.C Lover

Reputation: 775
• Posts: 5,834
• Joined: 25-December 13

## Re: how to convert bolean to int

Posted 30 May 2019 - 03:58 AM

Is the program working as desired now?

### #11 [email protected]

Reputation: 2
• Posts: 174
• Joined: 24-February 15

## Re: how to convert bolean to int

Posted 30 May 2019 - 04:09 AM

No and hackerrank also not telling what is wrong, I don't really know what to change.

I have searched up how to check an odd number.
I have searched up how to check an even number.
I have set the range between 2 and 5
I have set the range between 6 and 20 and I have checked if n is == to 20.

Given an integer, n, perform the following conditional actions:

If n is odd, print Weird
If n is even and in the inclusive range of 2 to 5, print Not Weird
If n is even and in the inclusive range of 6 to 20, print Weird
If n is even and greater than 20, print Not Weird

Can you tell me what I'm missing in my code?

### #12 NormR

• D.I.C Lover

Reputation: 775
• Posts: 5,834
• Joined: 25-December 13

## Re: how to convert bolean to int

Posted 30 May 2019 - 04:24 AM

Quote

Given an integer, n, perform the following conditional actions:

If n is odd, print Weird
If n is even and in the inclusive range of 2 to 5, print Not Weird
If n is even and in the inclusive range of 6 to 20, print Weird
If n is even and greater than 20, print Not Weird

Ok, start with the first test - where and how does the code test if n is odd?

### #13 g00se

• D.I.C Lover

Reputation: 3635
• Posts: 16,701
• Joined: 20-September 08

## Re: how to convert bolean to int

Posted 30 May 2019 - 04:40 AM

Also it would be a good idea to post your current code

### #14 [email protected]

Reputation: 2
• Posts: 174
• Joined: 24-February 15

## Re: how to convert bolean to int

Posted 30 May 2019 - 05:31 AM

I finally compared my code to someone from GitHub and changed some lines to make it work.

```String ans = "";

if(N % 2 == 1){
ans = "Weird";
} else {

if(N >= 6 && N <= 20) {
ans = "Weird";
} else {
ans = "Not Weird";
}

}
System.out.println(ans);
scanner.close();
```

### #15 overwhelmed_student

Reputation: 1
• Posts: 71
• Joined: 10-May 18

## Re: how to convert bolean to int

Posted 30 May 2019 - 06:05 AM

As a tip, you could have ans = "Not Weird" on default and get rid of the last else-statement:
```String ans = "Not Weird";

```

Or just say this, you initialize it later anyways:
```String ans;

```

• (2 Pages)
• 1
• 2

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }