how to convert bolean to int

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 600 Views - Last Post: 30 May 2019 - 08:27 AM Rate Topic: -----

#1 [email protected]   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • 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   User is online

  • D.I.C Lover
  • member icon

Reputation: 775
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 [email protected]   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • 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)
Was This Post Helpful? 0
  • +
  • -

#4 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 775
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#5 [email protected]   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#6 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 775
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#7 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2876
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#8 Atspulgs   User is offline

  • D.I.C Addict

Reputation: 100
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#9 [email protected]   User is offline

  • D.I.C Head

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

Re: how to convert bolean to int

Posted 30 May 2019 - 02:55 AM

View Postastonecipher, 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?


Yea I changed it already
Was This Post Helpful? 0
  • +
  • -

#10 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 775
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#11 [email protected]   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • 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.


Task
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?
Was This Post Helpful? 0
  • +
  • -

#12 NormR   User is online

  • D.I.C Lover
  • member icon

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

Re: how to convert bolean to int

Posted 30 May 2019 - 04:24 AM

Quote

Task
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?
Was This Post Helpful? 0
  • +
  • -

#13 g00se   User is online

  • D.I.C Lover
  • member icon

Reputation: 3635
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#14 [email protected]   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • 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();

Was This Post Helpful? 0
  • +
  • -

#15 overwhelmed_student   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • 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;


Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2