'Output from method keeps repeating when using switch menu

I'm trying to get my switch menu to work properly. Although, I think my switch menu is okay. When I call the methods, the outputs of those methods keep repeating infinitely. The outputs from my methods keep repeating. It's something I'm missing, but I can't figure out how to get it to stop. Usually, I only have this issue with for-loops. Any suggestions are greatly appreciated.

import java.util.Scanner;

class PointList {

    double points[][];
    
    public PointList(int len){
        points = new double[len][2];
        Scanner input = new Scanner(System.in);
        
        for (int i = 0; i < len; i++) {
            System.out.println("What are the coordinate of a point?");
            System.out.println("Enter the x value: ");
            points[i][0] = input.nextDouble();
            System.out.println("Enter the y value: ");
            points[i][1] = input.nextDouble();
        }
    }//close constructor
    
    //method to find distance between two points 
    public double distance(double x1, double y1, double x2, double y2){
        return Math.sqrt(((x2-x1)*(x2-x1)) + ((y1-y2)*(y1-y2)));
    } 
   
    //find the pair of points that has the smallest distance between them
    public void findClosestPair(){
        int p1 = 0; int p2 = 1;
        double shortDist = distance(points[p1][0], points[p1][1], points[p2][0],points[p2][1]);
        
        for (int i = 0; i < points.length; i++) {
            for (int j = i + 1; j < points.length; j++) {
                double dist = distance(points[i][0],points[i][1],points[j][0],points[j][1]);
                if(shortDist > dist){
                    p1 = i;
                    p2 = j;
                    shortDist = dist;
                }
            }
        }
        System.out.println("The closest two points are (" + points[p1][0]+", "+points[p1][1]+") and ("
                +points[p2][0]+", "+points[p2][1]+")");
    }
    
    //find the pair of points that has the largest distance between them
    public void findFarthestPair(){
        int p1 = 0; int p2 = 1;
        double longDist = distance(points[p1][0], points[p1][1], points[p2][0],points[p2][1]);
        
        for (int i = 0; i < points.length; i++) {
            for (int j = i + 1; j < points.length; j++) {
                double dist = distance(points[i][0],points[i][1],points[j][0],points[j][1]);
                if(longDist < dist){
                    p1 =i;
                    p2 = j;
                    longDist = dist;
                }
            }
        }
        System.out.println("The farthest two points are (" + points[p1][0]+", "+points[p1][1]+") and ("
                +points[p2][0]+", "+points[p2][1]+")");
    }//close findfarthestpair()
    
    //find the most central point
    public void findCentralPoint(){
        double sumX = 0, sumY = 0;
        for (int i = 0; i < points.length; i++) {
            for (int j = i + 1; j < points.length; j++) {
                sumX =+ points[i][0];
                sumY =+ points[j][0];
            }
        }
        System.out.println("The midpoints are (" + sumX+", "+sumY+")");
    }//close findCentralPoing90
}

public class Points {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        System.out.println("How many points do you want to compare?");
        int size = input.nextInt();
        
        PointList p1 = new PointList(size);
        
        double x1 = p1.points[0][0];
        double y1 = p1.points[0][1];
        double x2 = p1.points[1][0];
        double y2 = p1.points[1][1];
        
        
        System.out.println("Menu:");
        System.out.println("1 - Find the two points that are closest apart");
        System.out.println("2 - Find the two points that are farthest apart");
        System.out.println("3 - Determine the most central point");
        System.out.println("4 - Quit");
        int choice = input.nextInt();
        boolean mainLoop = true;
        while (true) {
        switch (choice){
            case 1:
                p1.findClosestPair();
                break;
            case 2:
                p1.findFarthestPair();
                break;
            case 3:
                p1.findCentralPoint();
                break; 
            case 4: 
                System.out.println("Exiting Program...");
                System.exit(0);
                 break;
            default :
                System.out.println("This is not a valid Menu Option! Please Select Another");
                break;                
        }
        
    }
    
}
}


Solution 1:[1]

You never exit the while loop...

  1. Change boolean mainLoop = true; to boolean waitingForAcceptableChoice = true; (just for clarity).
  2. Change while(true) to while(waitingForAcceptableChoice);.
  3. Add waitingForAcceptableChoice = false; before break; in cases from 1 to 4 (acceptable choices) to exit the while loop.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1