File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Having problem with Even/odd program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Having problem with Even/odd program" Watch "Having problem with Even/odd program" New topic
Author

Having problem with Even/odd program

Abdulmajeed Alroumi
Greenhorn

Joined: Jun 24, 2012
Posts: 6
Hi there everyone,
I hope everyone is doing great.

I have tried to create isEven program using the .Operator but I kept on having problems with it. Can you guys please check it and provide with an explanation if possible.
Thanks,

Here is the program:


Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> Can you guys please check it and provide with an explanation if possible.

far better for you to explain:
the problem/s you're having
the result you expect
the result you get
Abdulmajeed Alroumi
Greenhorn

Joined: Jun 24, 2012
Posts: 6
Sorry for not providing you with enough information

Okay, I have tried with this program many ways, each time I have to initialize x with a number and then it decides if it's odd or even which is I am not looking for. The output I am looking for is when I typed down a number it shows if it's odd or even. So, I said let's give it a though and try public boolean isEven(int num){}; method. It showed me two errors, one x has to be initialize with a number. Two this method must return a result of type boolean.

Thanks
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> It showed me two errors, one x has to be initialize with a number.

so, try doing what the message says

> Two this method must return a result of type boolean.

OK, this is your method
public boolean isEven(int num){return};

what do you think you should test?
and how would you return the result?
Abdulmajeed Alroumi
Greenhorn

Joined: Jun 24, 2012
Posts: 6
The this is that if I assign x with number, it will not work as the way I wanted...I am asking if there is a way to compile the program without assigning x with a number...because what I am trying to do is that if I need to have the number check it first if it's odd or even...but it will not work in this way if I assigned x with a number...as for the boolean...from my understanding you can only use return once in a method...and what I am trying to return to see the value if it's true of false
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> I am asking if there is a way to compile the program without assigning x with a number

you could have x as a class field, but it will have a default value of 0, so, give it 0.
if you want to give x a value during the program, it will change from 0.

> from my understanding you can only use return once in a method

you can only return once, but you can determine 'when' it is returned
e.g


in the method, don't have the test as an if-else (try it and see what happens)
Greg Ferguson
Ranch Hand

Joined: Jun 04, 2012
Posts: 34
Hi Abdulmajeed,
Learn how to use the modulus "%" operator and you will easily be able to determine whether a number is even or odd.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7801
    
  21

@Buenny - Just a style tip: You don't need to include an entire quote (especially containing code) for a 1-line reply.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Greg Ferguson
Ranch Hand

Joined: Jun 04, 2012
Posts: 34
@Winston - Thanks, I deleted the unnecessary part.
Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
Abdulmajeed Alroumi wrote:The this is that if I assign x with number, it will not work as the way I wanted...I am asking if there is a way to compile the program without assigning x with a number

You have to assign a value to x but there are a number of ways of doing this.
You can assign it a literal value
You can pass a value as a command line argument and convert it to an int
You can ask the user to type a value and then read it in. You use System.out.println() to print a message and then Scanner or an InputStream attached to System.in to read the value. Do a Google search for java i/o tutorial

You can write a simple GUI application that allows the user to enter a number. Do a Google search for java swing tutorial if you want to try this.
Abdulmajeed Alroumi
Greenhorn

Joined: Jun 24, 2012
Posts: 6
Thanks for the reply, the program has been compiled successfully
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Having problem with Even/odd program