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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

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. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    20
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    20
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2013
    Posts
    20
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Mar 2013
    Posts
    20
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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));
    }

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Mar 2013
    Posts
    20
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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));
    				}
    	}
    }
    Last edited by IneedHelpwithJava; March 18th, 2013 at 05:10 PM. Reason: Update

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 2
    Last Post: December 3rd, 2012, 01:02 PM
  2. Can't figure out how to print out number of lines and words in a file in c++
    By javapenguin in forum Other Programming Languages
    Replies: 1
    Last Post: January 29th, 2012, 07:53 PM
  3. [SOLVED] Is it possible to get factorial of negative number
    By Lokesh in forum Java Theory & Questions
    Replies: 3
    Last Post: August 4th, 2011, 05:45 PM
  4. Need this to end when a negative number is entered
    By ponyexpress in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 28th, 2010, 09:02 AM
  5. Replies: 4
    Last Post: June 10th, 2009, 01:04 AM

Tags for this Thread