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

• March 18th, 2013, 05:36 AM
IneedHelpwithJava
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.
• March 18th, 2013, 05:56 AM
Norm
Re: Beginner at Java: I need to figure out how to print a number which is odd and negative and fits in a formula.
Quote:

how I can print an int
Can you use the System.out.println() method to print it?
• March 18th, 2013, 03:18 PM
IneedHelpwithJava
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
• March 18th, 2013, 03:24 PM
Norm
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?
• March 18th, 2013, 03:28 PM
IneedHelpwithJava
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?
• March 18th, 2013, 03:35 PM
Norm
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
• March 18th, 2013, 05:00 PM
IneedHelpwithJava
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));
}
• March 18th, 2013, 05:03 PM
Norm
Re: Beginner at Java: I need to figure out how to print a number which is odd and negative and fits in a formula.
Quote:

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.
• March 18th, 2013, 05:04 PM
IneedHelpwithJava
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...
Code java:

```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)); } } }```
• March 18th, 2013, 05:49 PM
Norm
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