I need help with the if else statement below. I am trying to use branching to determine and drawString to find the longest lines between the points. Everything I try with the branching method seems to give me errors so I have no idea what to do and it is making my code look horrible. Any help is appreciated.

thank you

package program1;

import java.awt.Color;
import javax.swing.JFrame;
import java.awt.Graphics;
import javax.swing.JOptionPane;

public class Program1 extends JFrame {

    private static final int FRAME_SIZE = 400;
    private int x1,y1,x2,y2,x3,y3;
    double Distance1;
    double Distance2;
    double Distance3;

    public static void main(String[] args) {
        Program1 guiWindow = new Program1 ();
        guiWindow.setSize(FRAME_SIZE, FRAME_SIZE);
        guiWindow.setDefaultCloseOperation(EXIT_ON_CLOSE);
        String valueString;
        valueString = JOptionPane.showInputDialog("Enter the X-coordinate for point A");
        guiWindow.x1 = Integer.parseInt(valueString);
        valueString = JOptionPane.showInputDialog("Enter the Y-coordinate for point A");
        guiWindow.y1 = Integer.parseInt(valueString);
        valueString = JOptionPane.showInputDialog("Enter the X-coordinate for point B");
        guiWindow.x2 = Integer.parseInt(valueString);
        valueString = JOptionPane.showInputDialog("Enter the Y-coordinate for point B");
        guiWindow.y2 = Integer.parseInt(valueString);
        valueString = JOptionPane.showInputDialog("Enter the X-coordinate for point C");
        guiWindow.x3 = Integer.parseInt(valueString);
        valueString = JOptionPane.showInputDialog("Enter the Y-coordinate for point C");
        guiWindow.y3 = Integer.parseInt(valueString);
        guiWindow.setVisible(true);              
        }
    @Override
    public void paint (Graphics g) {
        super.paint(g);
        Graphics canvas = getGraphics();
        canvas.drawLine(x1, y1, x2, y2);
        canvas.drawLine(x1, y1, x3, y3);
        canvas.drawLine(x2, y2, x3, y3);
        g.drawString("A", x1, y1);
        g.drawString("B", x2, y2);
        g.drawString("C", x3, y3);
        Distance1 = Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
        Distance2 = Math.sqrt((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2));
        Distance3 = Math.sqrt((x3-x1)*(x3-x1)+(y3-y2)*(y3-y2));
    }     
    if (Distance1 > Distance2) && (Distance1 > Distance3)
       {
        drawString("Line AB is the longest");
        }
    else if (Distance2 > Distance3) && (Distance2 > Distance1)
       {
        drawString("Line BC is the longest");
    }
    else if (Distance3 > Distance2) && (Distance3 > Distance1)
    {
        drawString("Line AC is the longest");
    }
    else if (Distance1 == Distance2) && (Distance1 > Distance3)
    {
        drawString("Lines AB and BC are the longest");        
    }
    else if (Distance2 == Distance3) && (Distance2 > Distance1)
    {
        drawString("Lines BC and AC are the longest");
        }
    else if (Distance1 == Distance3) && (Distance1 > Distance3)
    {
        drawString("Lines AB and AC are the longest");
    }
}

Recommended Answers

All 6 Replies

give me errors

Please copy the full text of the error messages and post them here.

Its always a good idea to add an ending else statement to catch the case where none of the above if tests were true. Print out a message .

The error was illegal start of type. And the requirement for this project was to display with graphics which line was the longest. I think I have most of it right except I dont have a correct beginning for the if else and thats basically where I'm stumped. This was the error list.

Thanks for you help as well.

java.lang.ClassFormatError: Duplicate field name&signature in class file program1/Program1
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:791)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
    at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:480)
Exception in thread "main" Java Result: 1

java.lang.ClassFormatError: Duplicate field name&signature in class file program1/Program1

I've never seen that error before. How are you compiling and executing the program?
Try deleteing the class file, creating a new one and executing the new one.

The error was illegal start of type

What does that mean? Are there compiler errors? Please copy and paste them here.

Basically I followed my instructors instructions to the T until I got to the branching part. I'll brief you on the program. I used graphics to obtain a Triangle with lables "A", "B", and "C". After that I put in the distance forumula shown there. The part I'm stuck with and which is causing the errors (if else) is using branching to determine which line is longest and display the correct message in the output window. Once I entered the if else part I got all those errors and now I'm stuck here.

You have an error at line 49.The } on line 48 closes the paint method, so the following lines are not part of any method. EXecutable statements like those must be inside a method.
Don't worry about any runtime errors yet. Runtime results will be unpredictable and meaningless until you have fixed all the compile errors.

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.