Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is what I have so far. My program terminates after repeating the while loop

ID: 3575216 • Letter: T

Question

This is what I have so far. My program terminates after repeating the while loop once instead of keep getting input. I attached the output I am getting and what it is supposed to look like. Please let me know if you can help. Thank you.


public class DistanceBetweenPoints{
public void calculateDistance(){
   double x1, y1, x2, y2;
   String check;
Scanner input = new Scanner(System.in);
   System.out.print("Enter x1: ");
   x1 = input.nextDouble();
   System.out.print("Enter y1: ");
   y1 = input.nextDouble();
   System.out.print("Enter x2: ");
   x2 = input.nextDouble();
   System.out.print("Enter y2: ");
   y2 = input.nextDouble();
     
   double distance = caldis(x1, y1, x2, y2);
   System.out.println("The distance between points " + "(" + x1 + "," + y1 + ") and (" + x2 + "," + y2 + ") is " + distance);

   System.out.println("Enter 'y' if you want to enter another pair of points 'n' if you do not:");
   check = input.next();
  
while (check.equalsIgnoreCase("Y"))
{
   System.out.print("Enter x1: ");
   x1 = input.nextDouble();
   System.out.print("Enter y1: ");
   y1 = input.nextDouble();
   System.out.print("Enter x2: ");
   x2 = input.nextDouble();
   System.out.print("Enter y2: ");
   y2 = input.nextDouble();
   distance = caldis(x1, y1, x2, y2);
   System.out.println("The distance between points " + "(" + x1 + "," + y1 + ") and (" + x2 + "," + y2 + ") is " + distance);
   System.out.println("Enter 'y' if you want to enter another pair of points 'n' if you do not:");
   if (check.equalsIgnoreCase("N"))
   {
       break;
   }  
}
}
  

   public double caldis(double x1, double y1, double x2, double y2)
   {
       return Math.sqrt(Math.pow((x1-x2), 2) + Math.pow((y1-y2),2));
   }
  

   public static void main ( String args[] )
   {

   DistanceBetweenPoints distance = new DistanceBetweenPoints ();
distance.calculateDistance ();
   }
}

Given the following was entered from the keyboard: 0.0 0.0 0.0 9.0 9.0 0.0 9.0 9.0 displayed you Enter X 1: Enter y 1: Enter x2: Enter y 2: The distance between points (0.0,0.0) and (0.0,9.0) is 9.0 nter 'y' if you want to enter another pair of points 'n' if you do not: Enter X 1: Enter y 1: Enter x2: Enter y 2: The distance between points (9.0,0.0) and (9.0,9.0) is 9.0 nter 'y if you want to enter another pair of points 'n' if you do not: Enter X instead of: Enter x1:Enter y1:Enter x2:Enter y2:The distance between points (0.0,0.0) and is 9.0 Enter "y" if you want to enter another pair of points 'n' if you do not: Enter x1:Enter y 1 Enter x2:Enter y2:The distance between points (9.0,0.0) and (9.0,9.0) is 9.0 Enter 'y' if you want to enter another pair of points 'n' if you do not: Enter x1:Enter y 1 Enter x2:Enter y2:The distance between points (4.5,4.5) and (9.3,7.2) is 5.5 Enter 'y' if you want to enter another pair of points 'n' if you do not

Explanation / Answer

The code for calculateDistance method can be reduced like this by keeping check value to Y by default.. and coming to your query ...you missed to keep " check = input.next() " after System.out statement in the while loop hence you are unable to input yes or no. refer this code for that method:

public void calculateDistance(){
double x1, y1, x2, y2;
String check;
check="Y";
Scanner input = new Scanner(System.in);
double distance;
while (check.equalsIgnoreCase("Y"))
{
System.out.print("Enter x1: ");
x1 = input.nextDouble();
System.out.print("Enter y1: ");
y1 = input.nextDouble();
System.out.print("Enter x2: ");
x2 = input.nextDouble();
System.out.print("Enter y2: ");
y2 = input.nextDouble();
distance = caldis(x1, y1, x2, y2);
System.out.println("The distance between points " + "(" + x1 + "," + y1 + ") and (" + x2 + "," + y2 + ") is " + distance);

System.out.println("Enter 'y' if you want to enter another pair of points 'n' if you do not:");
check = input.next();
if (check.equalsIgnoreCase("N"))
{
break;
}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote