Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3357210
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:36:22+00:00 2026-05-18T02:36:22+00:00

I’m using the Point Class to manage a list of (x,y) coordinates and I

  • 0

I’m using the Point Class to manage a list of (x,y) coordinates and I need to sort them in order of X.

I read online to make a new class PointCompare that implements Comparator, however I’m not sure how this works and therefore I have a compiler error in the sortByXCoordinates method.

Help would be appreciated a lot, and any comments are welcome, thanks in advance.
Here is some of my code:

import javax.swing.JOptionPane;
import java.awt.Point;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
//import java.util.Iterator;

public class ConvexHullMain {

 private Point coordinates = new Point(0, 0);
 private final int MAX_POINTS = 3;
 private ArrayList<Point> coordinateList = new ArrayList<Point>();

 public void inputCoordinates() {

  String tempString; // temp string for JOptionPane
  int tempx = 0;
  int tempy = 0;

  for (int i = 0; i < MAX_POINTS; i++) {
   try {
    // input x coordinates
    tempString = JOptionPane.showInputDialog(null,
      "Enter X coordinate:");
    tempx = Integer.parseInt(tempString);

    // input y coordinates
    tempString = JOptionPane.showInputDialog(null,
      "Enter Y coordinate:");
    tempy = Integer.parseInt(tempString);

    coordinates.setLocation(tempx, tempy);// set input data into
              // coordinates object
    coordinateList.add(coordinates.getLocation()); // put in
                // arrayList

   } // end Try
   catch (NumberFormatException e) {
    System.err.println("ERROR!");
    main(null);

   } // end catch

  }// end for loop

 }

 public void displayPoints() {

  for (int i = 0; i < MAX_POINTS; i++) {

   JOptionPane.showMessageDialog(null, "Point number " + (i + 1)
     + " is: " + coordinateList.get(i));

  }

  // alt method
  // Iterator i = coordinateList.iterator();
  // String outputTemp;
  // while (i.hasNext()) {
  // outputTemp = i.next().toString();
  // JOptionPane.showMessageDialog(null, "Point number " + " is: "
  // + outputTemp);
  // }

 }


 /**
  * This sorts the points by the X coordinates
  */
  public void sortByXCoordinates(){

   coordinateList.sort(coordinates, new PointCompare());
  }

   public class PointCompare implements Comparator<Point> {

   public int compare(Point a, Point b) {
    if (a.x < b.x) {
     return -1;
    } else if (a.x > b.x) {
     return 1;
    } else {
     return 0;
    }
   }
   }

   public static void main(String[] args) {
  ConvexHullMain main = new ConvexHullMain();

  main.inputCoordinates();
  main.displayPoints();


 }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T02:36:22+00:00Added an answer on May 18, 2026 at 2:36 am

    You were close. The problem you had was simply that you invoked

      public void sortByXCoordinates(){
    
       coordinateList.sort(coordinates, new PointCompare());
    
      }
    

    What you want is this:

    import java.awt.Point;
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;
    
    import javax.swing.JOptionPane;
    
    public class MainClass {
    
        private final Point coordinates = new Point(0, 0);
        private final int MAX_POINTS = 3;
        private final ArrayList<Point> coordinateList = new ArrayList<Point>();
    
        public void inputCoordinates() {
    
            String tempString;
            int tempx = 0;
            int tempy = 0;
    
            for (int i = 0; i < this.MAX_POINTS; i++) {
                try {
                    tempString = JOptionPane.showInputDialog(null, "Enter X coordinate:");
                    tempx = Integer.parseInt(tempString);
                    tempString = JOptionPane.showInputDialog(null, "Enter Y coordinate:");
                    tempy = Integer.parseInt(tempString);
                    this.coordinates.setLocation(tempx, tempy);// set input data into
                    this.coordinateList.add(this.coordinates.getLocation()); // put in
                }
                catch (final NumberFormatException e) {
                    System.err.println("ERROR!");
                    main(null);
    
                }
            }
        }
    
        public void displayPoints() {
    
            for (int i = 0; i < this.MAX_POINTS; i++) {
    
                JOptionPane.showMessageDialog(null, "Point number " + (i + 1) + " is: " + this.coordinateList.get(i));
    
            }
    
        }
    
        /**
         * This sorts the points by the X coordinates
         */
        public void sortByXCoordinates() {
    
            Collections.sort(this.coordinateList, new PointCompare());
    
        }
    
        public class PointCompare
            implements Comparator<Point> {
    
            public int compare(final Point a, final Point b) {
                if (a.x < b.x) {
                    return -1;
                }
                else if (a.x > b.x) {
                    return 1;
                }
                else {
                    return 0;
                }
            }
        }
    
        public static void main(final String[] args) {
            final MainClass main = new MainClass();
    
            main.inputCoordinates();
            main.displayPoints();
    
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have thousands of HTML files to process using Groovy/Java and I need to
I am using JSon response to parse title,date content and thumbnail images and place
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I am using the SimpleRSS gem to parse a WordPress RSS feed. The only
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I want use html5's new tag to play a wav file (currently only supported
I am doing a simple coin flipping experiment for class that involves flipping a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.