0

Below is my code. It is a simple class exercise in beginner level Java. I am to compute the distance between points for as long as the user wants to enter points. When I run my code (aside from formatting decimals) it works perfectly, but it terminates after the first run. Can anyone assist? Here is my code:

import java.util.Scanner;

public class Distance{ String a;

public static void main( String[] args){
    Scanner input = new Scanner(System.in);
    String a = "y";
    do {
        System.out.print("Enter x1:");
        double x1 = input.nextDouble();
        System.out.print("Enter y1:");
        double y1 = input.nextDouble();

        System.out.print("Enter x2:");
        double x2 = input.nextDouble();
        System.out.print("Enter y2:");
        double y2 = input.nextDouble();

        double distance = Math.pow(Math.pow(x2-x1,2)+(y2-y1), 0.5);
        System.out.println("The distance between your points is "+distance);


        System.out.print("Do you want to continue? ");
        a = input.next();
    }while(a == "y");
}

}

  • Try a.equals("Y"). Doing an == on objects (which means including strings) is not a really good idea... unless you know what it actually does – Wietlol Mar 28 '17 at 15:12
  • while(a == "y") should be while(a.equals("y")) – johnII Mar 28 '17 at 15:12
  • thank you! and apologies for the duplicate..I found some that were close, but I’ve never seen “.equals” and I tried to use Char but could not figure out how to “find the next char” so I stuck with strings – Trey Howerton Mar 28 '17 at 15:45

0 Answers0