Hi everybody, I have been having trouble with the output of a set of points for my triangle program. When I run my program, it outputs in the end "triangle1 coordinates: Vertex A is java.awt.Point[x=12,y=13]" and I have no idea how to format it to hide the "java.awt.Point" part. Any advice would be greatly appreciated! Thanks in advance.

public class TriangleTester 
{
    public static void main(String[] args) 
    {
        Scanner scan = new Scanner(System.in);
        
        System.out.println("Enter the x- and y- coordinates of the first vertex: ");
        int a1 = scan.nextInt();
        int a2 = scan.nextInt();
        
        System.out.println("Enter the x- and y- coordinates of the second vertex: ");
        int b1 = scan.nextInt();
        int b2 = scan.nextInt();
                
        System.out.println("Enter the x- and y- coordinates of the third vertex: ");
        int c1 = scan.nextInt();
        int c2 = scan.nextInt();
        
        Triangle triangle1 = new Triangle(a1, a2, b1, b2, c1, c2);
        
        System.out.println(triangle1.toString());
    }
}
public class Triangle 
{
    private int xA;
    private int yA;
    private int xB;
    private int yB;
    private int xC;
    private int yC;
    
    Point vertexA;
    Point vertexB;
    Point vertexC;

    public Triangle(Point vertexA, Point vertexB, Point vertexC) 
    {
        vertexA = vertexA;
        vertexB = vertexB;
        vertexC = vertexC;
    }
    
    public Triangle(int xA, int yA, int xB, int yB, int xC, int yC)
    {
        vertexA = new Point(xA, yA);
        vertexB = new Point(xB, yB);
        vertexC = new Point(xC, yC);
    }
   
    public String toString()
    {
       return "Vertex A is " + vertexA.toString();
    }
}

Recommended Answers

All 2 Replies

You are calling default "toString()" method of "Point" class in your toString() method.

return "Vertex A is " + vertexA.toString();

If you want your own format, you must not use the toString() of Point class. You can simply format it whatever way you want. i.e. vertexA.x for "x" value.

Awesome!! Thanks, that's exactly what I needed!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.