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 4 of 4

Thread: If Statement

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default If Statement

    Hi guys. Ive created a program that computes powers when typed in as command line arguments. I have the following code, but i cannot understand why the IF statement doesnt make the result 0 is a power 0 is typed in as a command line argument:

    int mantissa = Integer.parseInt(args[0]);
    int exponent = Integer.parseInt(args[1]);
    int answer = mantissa;

    for(int i=1; i < exponent; i++)
    answer=answer*mantissa;

    if (args[1] == 0)
    answer = 1;

    regards

    Shyam


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: If Statement

    See Common Java Mistakes, mainly several posts down to the one that describes Problem description: == operator or equals() method

  3. The Following User Says Thank You to copeg For This Useful Post:

    javapenguin (October 26th, 2010)

  4. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: If Statement

    Quote Originally Posted by Shyamz1 View Post
    Hi guys. Ive created a program that computes powers when typed in as command line arguments. I have the following code, but i cannot understand why the IF statement doesnt make the result 0 is a power 0 is typed in as a command line argument:

    int mantissa = Integer.parseInt(args[0]);
    int exponent = Integer.parseInt(args[1]);
    int answer = mantissa;

    for(int i=1; i < exponent; i++)
    answer=answer*mantissa;

    if (args[1] == 0)
    answer = 1;

    regards

    Shyam
    x ^ 0 = x/x. 0^0 = 0/0. That is indeterminate.

    To get it to be negative exponents, simply have it inverse the result gotten by your for loop and then

    do this :

    public double negativeExponents(int x, int y)
    {

    if (x ==0 && y >=0)
    return;

    double d = 1/ otherMethod(num, -power);
    return d;

    }

    I should note that this won't work for raising something to the 1/3 power or the square root, etc, unless you use doubles, but you're better off using the Math.pow(double num, double power) for those.


    I've been able to get the compiler to output Indeterminate or Infinity or - Infinity for 0/0 and 1/0 and -1/0, respectively. However, it only does that in some cases for some odd reason. Also, Math.sqrt(-n) returns NaN, "Not a Number" I suppose it means, unless n is 0 or negative.

  5. #4
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: If Statement

    Quote Originally Posted by copeg View Post
    See Common Java Mistakes, mainly several posts down to the one that describes Problem description: == operator or equals() method
    Wait, yeah, you're right, args[0] is a String and Strings are compared with .equals.

Similar Threads

  1. help with switch statement
    By robertsbd in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 12th, 2010, 12:52 PM
  2. If Statement in SQL String
    By Steffi1013 in forum Loops & Control Statements
    Replies: 0
    Last Post: March 30th, 2010, 03:25 PM
  3. If-Else Statement help
    By SnowCrashed in forum Loops & Control Statements
    Replies: 5
    Last Post: February 9th, 2010, 07:57 PM
  4. If Statement Java Need help
    By Keno888 in forum Loops & Control Statements
    Replies: 5
    Last Post: October 25th, 2009, 01:53 AM
  5. If statement
    By Dave in forum Loops & Control Statements
    Replies: 2
    Last Post: August 27th, 2009, 10:11 AM