Results 1 to 6 of 6

Thread: Error output

  1. #1
    Kartright is offline Member
    Join Date
    May 2013
    Posts
    20
    Rep Power
    0

    Default Error output

    For extra credit I needed to make some sort of formula calculator. I went quadratic formula. Teacher told me it had to either print out an error or throw an ArithmeticException when any answers were NaN.
    Here's my current code:
    Java Code:
    import java.util.Scanner;
    import java.lang.Math;
    
    // Quadratic formula is -b +/- root b^2 - 4ac divided by 2a
    
    public class quadratic {
    	public static void main(String args[]){
    		Scanner input = new Scanner(System.in);
    		double a, b, c, x1, x2, top; //top represents the numerator
    		System.out.println("Enter your a, b, and c values (in that order):");
    		a = input.nextDouble();
    		b = input.nextDouble();
    		c = input.nextDouble();
    		top = Math.sqrt((b*b) - (4*a*c));
    		x1 = (-b + top) / (2*a);
    		x2 = (-b - top) / (2*a);
    		if (Double.isNaN(x1)){
    			System.err.println("ERROR: First answer is not a real number!");
    		} else if (Double.isNaN(x2)){
    			System.err.println("ERROR: Second answer is not a real number!");
    		} else {
    			System.out.println("Your answers are: " +x1+ ", " +x2);
    		}
    	}
    
    }
    I need to code it so that if x1 is NaN, an error is thrown (as I somewhat have done), and it displays the x2 answer (and vice versa). I also need to make it so that if both answers are NaN, an error is shown.

    Currently all I can figure out is how to show a message when one of them is NaN, and how to show a message if neither are NaN. Any help would be greatly appreciated.
    Last edited by Kartright; 05-30-2013 at 02:52 AM.

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,766
    Rep Power
    5

    Default Re: Error output

    Java Code:
    boolean x1NaN = Double.isNaN(x1);
    boolean x2NaN = Double.isNaN(x2);
    if (x1NaN && x2NaN) {
           // your message here
    } else if (x1NaN) {
          // your message here
    } else if (x2NaN) {
         // your message here
    }
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Kartright is offline Member
    Join Date
    May 2013
    Posts
    20
    Rep Power
    0

    Default Re: Error output

    Thanks, jim!
    Can you please tell me why it needs to be a boolean, though?

  4. #4
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,766
    Rep Power
    5

    Default Re: Error output

    Because the Double.isNaN() method returns a boolean.
    I just assigned the return values to boolean variables
    and used those instead of re-invoking the methods. Sometimes it
    looks cleaner.

    Whenever you use a logical expression like
    Java Code:
    if ( some expression using && or || )
    the stuff inside the parentheses evaluates to either true or false. So the following
    are equivalent.

    Java Code:
    if (a == b) {
       // do something
    }
    
    // or 
    boolean result = a == b;
    if (result) {
       // do something
    }
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    Kartright is offline Member
    Join Date
    May 2013
    Posts
    20
    Rep Power
    0

    Default Re: Error output

    Oh, alright.
    Thanks!

  6. #6
    Kartright is offline Member
    Join Date
    May 2013
    Posts
    20
    Rep Power
    0

    Default Re: Error output

    Quote Originally Posted by jim829 View Post
    Because the Double.isNaN() method returns a boolean.
    I just assigned the return values to boolean variables
    and used those instead of re-invoking the methods. Sometimes it
    looks cleaner.
    After revising my code, I re-read what you said and figured out what you meant by cleaner.

    Rather than type
    Java Code:
    if (Double.isNaN(x1) && Double.isNaN(x2))
    You simply assigned Double.isNaN(var) to a boolean.
    Makes sense. Thanks, Jim!

Similar Threads

  1. Replies: 2
    Last Post: 05-03-2012, 11:39 AM
  2. Need help with input/output program error
    By stefan2892 in forum New To Java
    Replies: 2
    Last Post: 02-07-2011, 07:57 PM
  3. OutPut Error :/
    By socboy6579 in forum New To Java
    Replies: 2
    Last Post: 12-12-2010, 09:03 PM
  4. Two Class Program but No Output or Error
    By H0AX in forum New To Java
    Replies: 2
    Last Post: 12-09-2010, 06:27 AM
  5. Java, output string, getting correct output? HELP!
    By computerboyo in forum New To Java
    Replies: 2
    Last Post: 02-25-2009, 11:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •