• Post Reply Bookmark Topic Watch Topic
  • New Topic

boolean always return false for some reason  RSS feed

 
Liat Ledder
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello

I am a beginner, I am trying to run this code and it is always telling me my Boolean is false for some reason. Any advice will help!
Thank you so much

 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liat Ledder wrote:



You can't pass values back by assigning them to parameters like you do in isEven(...). Java is pass by value, so the result in the isEven parameter list is not the same variable as the result in the main method.

You need to get rid of the 'result' parameter from isEven altogether. When you do that you will notice you need to make a change to the code in the main method too.

Also, never have code like the following:



Whenever you see that sort of code you should replace it with



Also your isEven() method can be written much more simply. In fact you can reduce it down to a single statement. I'll leave it up to you to see if you can figure out how.
 
Paul Clapham
Sheriff
Posts: 22526
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And also in the "Beginners write way too much code" category, consider this code fragment:



First it's unnecessary to write "if X then FOO else if not X then BAR"; it's sufficient to write "if X then FOO else BAR". So:



Second it's unnecessary to write "if X then true else false; it's sufficient to write "if X" in that sort of case. So:



(Sorry, Mike! I didn't read down to the last line of your post.)
 
Liat Ledder
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much for your answers

I tried to simplify the mod statement and get read of my result variable, and i think I made more mess
 
Pradip Talekar
Greenhorn
Posts: 2
IntelliJ IDE Java MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to CodeRanch

I've observed your code and below are my observations:

you do not need this line

Because, you're taking the user input using the Scanner object "input", and you've assigned it directly to your num variable using below code

If you plan to use num1 variable in your code, which I feel is unnecessary then your main method should carry the declaration for num1 variable.

Also, if you're getting rid of num1 variable,
you should call the function isEven() by passing num variable and not num1.

then your function call would look like



I guess this is it and your code will work properly!

This is my first comment and I'm also new to Java programming, so please correct me if I'm missing the track!!
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You've changed your isEven method so it no longer returns a value. That isn't what I intended you to do.

By convention any method that begins with 'is' should return a boolean.
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Beware of the % operator. You are carefully checking for values of 0 and values of 1. But when you use i % 2 you have three possible results.

You know you can reduce your isEven(int i) method to a single line?
 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also when you return a value you either have to assign it like this ---


Or you could go

If you don't do one or the other you aren't making use of the return value. If you need to save the returned value then use the first way.
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And never write == true or == false or similar. They are not only poor style but also error‑prone; if you write = by mistake you can get two errors for the price of one.
Not if (b == true) ... nor if (b == false) ...
but
if (b) ... or if (!b) ...
 
Liutauras Vilda
Marshal
Posts: 4666
320
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guillermo Ishi wrote:EvenOdd.isEven(num, mod)

And I would avoid such a class name "EvenOdd", as it sounds confusing for me when you're calling method named "isEven".
"EvenOdd.isEven()", opinions could differ. Maybe just call it something related with "Utility" word.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!