title: Solution for quadratic returns wrong imaginary solutions when the discriminant is negative
body: <p>I am trying to get into programming more, and this is one thing that a friend recommended that I do. I'm trying to write a quadratic solver, but it is giving the wrong answer, however neither of us can figure out why.</p>
  
  <p>We are using this equation as an example: x^2+4x+5 = 0 <br />
  The solutions for it are x=-2+1 and x=-2-i<br />
  The eclipse console reads this:</p>
  
  <pre><code>Quadratic equation: 
  Please enter A: 
  1
  Please enter B: 
  4
  Please enter C: 
  5
  Your first solution is: -3.0
  Your second solution is: -5.0
  </code></pre>
  
  <p>Here is the code being used:</p>
  
  <pre><code>import java.util.Scanner;
  
  public class Main{
  
      private static Scanner input = new Scanner(System.in);
  
      public static void main(String[] args) {
  
          System.out.println("Quadratic equation: ");
          System.out.println("Please enter A: ");
  
          while(!input.hasNextInt()){
              @SuppressWarnings("unused")
              String broken = input.next();
              System.out.println("Please enter A: ");
          }
  
          int a = input.nextInt();
          System.out.println("Please enter B: ");
  
          while(!input.hasNextInt()){
              @SuppressWarnings("unused")
              String broken = input.next();
              System.out.println("Please enter B: ");
          }
  
          int b = input.nextInt();
          System.out.println("Please enter C: ");
  
          while(!input.hasNextInt()){
              @SuppressWarnings("unused")
              String broken = input.next();
              System.out.println("Please enter C: ");
          }
  
          int c = input.nextInt();
  
          double imaginaryCheck = (b*b)-(4*a*c);
  
          if(imaginaryCheck&gt;0){
              double plus = (-b) + Math.sqrt(imaginaryCheck) / (2 * a);
              double minus = (-b) - Math.sqrt(imaginaryCheck) / (2 * a);
              System.out.format("Your first solution is: %1.2f\n", plus);
              System.out.format("Your second solution is: %1.2f", minus); 
          }else{
              double plus = (-b) + Math.sqrt((-1*imaginaryCheck)) / (2 * a);
              double minus = (-b) - Math.sqrt((-1*imaginaryCheck)) / (2 * a);
              System.out.format("Your first solution is: %1.2fi\n", plus);
              System.out.format("Your second solution is: %1.2fi", minus); 
          }
      }
  }
  </code></pre>
  
  <p>Is there an error somewhere or did I mess up the equation somehow?</p>