Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: Beginner at Java: I need to figure out how to print a number which is odd and negative and fits in a formula.

1. ## Beginner at Java: I need to figure out how to print a number which is odd and negative and fits in a formula.

I am really new to java and wanted to know how I can print an int which is both negative and odd and if so, needs to be put into the formula (n^2+1) . I am not looking particularly for answers (although I would appreciate them greatly) I just generally want to better my knowledge of java. Thanks in advanced.  Reply With Quote

3. ## Re: Beginner at Java: I need to figure out how to print a number which is odd and negative and fits in a formula.

how I can print an int
Can you use the System.out.println() method to print it?  Reply With Quote

4. ## Re: Beginner at Java: I need to figure out how to print a number which is odd and negative and fits in a formula.

Yes I can use println  Reply With Quote

5. ## Re: Beginner at Java: I need to figure out how to print a number which is odd and negative and fits in a formula.

That answers the question. Did you have any other questions?  Reply With Quote

6. ## Re: Beginner at Java: I need to figure out how to print a number which is odd and negative and fits in a formula.

I was wondering how I could go about printing a number both negative and odd using "if" statements and (I'm assuming but not entirely sure) boolean?  Reply With Quote

7. ## Re: Beginner at Java: I need to figure out how to print a number which is odd and negative and fits in a formula.

Are you asking how to determine if a number is odd and negative?
A test for even or odd is to compare the results of theNumber % 2 : 1 is odd, 0 is even
A test for negative is if theNumber < 0  Reply With Quote

8. ## Re: Beginner at Java: I need to figure out how to print a number which is odd and negative and fits in a formula.

Thank you I will try this and come back

--- Update ---

That was helpful (i now understand the modulus operator) but I am getting problems in my code where it is not converting to boolean. See I'm trying to get the code to run with two requirements but I can't seem to do that. This is my code for the int so far:

public static void main(String[] args){
int n=11;
if (n<0){
if (n%2);
System.out.println(n^2+1));
}  Reply With Quote

9. ## Re: Beginner at Java: I need to figure out how to print a number which is odd and negative and fits in a formula.

I am getting problems
Please copy the full text of the error messages and paste it here.

[code=java]
[/code]
to get highlighting and preserve formatting.  Reply With Quote

10. ## Re: Beginner at Java: I need to figure out how to print a number which is odd and negative and fits in a formula.

Sure! Hold on...
```package variables;
public class SimpleCalc {
public static void main(String[] args){
int n=11;
if (n<0){
if (n%2);                                           //Error Message occurs here: Mismatch - cannot convert to boolean
System.out.println(n^2+(n/2));
}
if (n>=0) {                                         //The ones after this are fine
System.out.println(n^3*(n-1));
}

if (n>=0) {
System.out.println(n^-1*(3*n));
}
}
}```  Reply With Quote

11. ## Re: Beginner at Java: I need to figure out how to print a number which is odd and negative and fits in a formula.

The expression in the if statement must use a boolean operator that returns a boolean result. % returns a numeric value.
See the tutorial for a list of operators that return a boolean result:
Equality, Relational, and Conditional Operators (The Java™ Tutorials > Learning the Java Language > Language Basics)
The if statement requires a boolean value, not a numeric value.

a == b is a boolean value
a + b is a numeric value
(a + b) > 3 is a boolean value  Reply With Quote 