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

Thread: Why does java not honor my if/else statments

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Why does java not honor my if/else statments

    Hey everyone, complete noob here, I'm writing a program for my class about a quadratic equation. Everything in the code seems correct, only java just doesnt seem to care about if/else statements, anyway here is my code:
    /**
     * 
     */
    package edu.vtc.aav10260.cis2261;
    import java.util.Scanner;
    /**
     * @author andre
     *
     */
    public class Quadratic {
     
    	/**
    	 * @param args unused
    	 */
    	public static void main(String[] args) {
    		Scanner input = new Scanner(System.in);
    		String equation = "( ax^2 + bx + c = 0)"; 
    		System.out.println(equation);
    		System.out.println("Enter the coefficients");
    		System.out.print("a= "); 
    		double a = input.nextDouble();
    		System.out.print("b= ");
    		double b = input.nextDouble();
    		System.out.print("c= ");
    		double c = input.nextDouble();
    		System.out.println();
    		double determinant = Math.sqrt((b*b)-(4*a*c));
    		System.out.println(determinant);
     
    		if (determinant < 0)
    		{
    		System.out.println("There are no solutions");
    		} 
    		else if  (determinant == 0)
    		{
    		System.out.println("There is one solution");
    		double solution = (-b/(2*a));
    		System.out.println(solution);
    		}
    		else if (determinant > 0);
    		{
    		System.out.println("There are two solutions");
    		double solution1 = ((-b + determinant)/(2*a));
    		double solution2 = ((-b - determinant)/(2*a));
    		System.out.println(solution1);
    		System.out.println(solution2);
    		}
     
     
    	}
     
    }
    The program works fine if the determinant is positive and has two solutions, but if it is only one solution, the results read like this:

    ( ax^2 + bx + c = 0)
    Enter the coefficients
    a= 1
    b= 2
    c= 1

    0.0
    There is one solution
    -1.0
    There are two solutions
    -1.0
    -1.0

    And if the determinant is negative, giving no solutions, it looks like this:

    ( ax^2 + bx + c = 0)
    Enter the coefficients
    a= 1
    b= 2
    c= 3

    NaN
    There are two solutions
    NaN
    NaN

    This makes no sense as my statements explicitly state certain things to do if the determinant is less than, equal to or greater than zero, however the program seems to just print everything out regardless of what the determinant is
    Last edited by helloworld922; September 11th, 2012 at 10:38 PM. Reason: please use [code] tags


  2. #2
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Why does java not honor my if/else statments

    Quote Originally Posted by mflb94 View Post
    ... java just doesnt seem to care about if/else statements...
    .
    .
    .
    		else if  (determinant == 0)
    		{
    		    System.out.println("There is one solution"); // <--- This is where things are OK, right?
    		    double solution = (-b/(2*a));
    		    System.out.println(solution);
    		}
    		else if (determinant > 0); //<---This is where things go wrong, right?
    		{
    		    System.out.println("There are two solutions");
    .
    .
    .
    This makes no sense as my statements explicitly state certain things to do if the determinant is less than, equal to or greater than...
    Well, I can certainly see what you intended for it to do.

    However...

    The program does what you tell it to do, not what you intend for it to do. (Does this make me smarter than the "computer" or not?)

    Generic methodology for debugging: When things seem to be going right, but then they go wrong, look very carefully at the place where things go wrong...

    Bottom line: Trust me, Java cares. It really, really cares.


    Cheers!


    Z
    Last edited by Zaphod_b; September 11th, 2012 at 10:48 PM.

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Why does java not honor my if/else statments

    else if (determinant > 0);
    You have an extra semi-colon at the end. This is basically telling Java to ignore this else/if branch.

Similar Threads

  1. using if/else statments
    By new2java in forum Loops & Control Statements
    Replies: 1
    Last Post: September 25th, 2009, 12:21 AM