Hello great programmers at this website. Hopefully your day has been good so far. I was wondering if you could help me with some Java errors related to the header of my program that I am doing for an assignment. Thank you millions for your help. Here is the code:

import java.Math.*;
import java.swing.JOptionPane;

public class EqTri
{ 
private double sideLength;
private String identifier;
{
public EqTri(double x,String s)
{
sideLength= x;
identifier=s;

}
public setsideLength(double x)
{
x= sideLength;

}
public setIdentifier(String s)
{
s= identifier;

}
public double getsideLength()
{
return x;
}
public String getIdentifier()
{
return s;

}
public double getPerimeter()
{
return 3*s;
}
public double getArea()
{
return (Math.sqrt(3)/4)*a*a;

}
}

Here are the errors that came, for your convenience:

EqTri.java:9: illegal start of expression
public EqTri(double x,String s)
^
EqTri.java:9: '.class' expected
public EqTri(double x,String s)
                    ^
EqTri.java:9: ';' expected
public EqTri(double x,String s)
                     ^
EqTri.java:9: ';' expected
public EqTri(double x,String s)
                              ^
EqTri.java:15: illegal start of expression
public setsideLength(double x)
^
EqTri.java:15: '.class' expected
public setsideLength(double x)
                            ^
EqTri.java:15: ';' expected
public setsideLength(double x)
                             ^
EqTri.java:20: illegal start of expression
public setIdentifier(String s)
^
EqTri.java:20: ')' expected
public setIdentifier(String s)
                           ^
EqTri.java:20: illegal start of expression
public setIdentifier(String s)
                             ^
EqTri.java:20: ';' expected
public setIdentifier(String s)

Thank you again.

Dani AI

Generated

A few focused fixes that will clear most of those compiler errors.

As noticed, remove the stray { after the field declarations β€” that opens an instance-initializer block, and Java does not allow method or constructor declarations inside it, so the compiler reports "illegal start of expression." 's reformatting makes the problems easier to see, but some logic/syntax issues remain; and is right that many of the reported lines are compile-time scope errors, not NullPointerExceptions.

Key problems to fix (in order of importance)

  • Imports: java.Math.* and java.swing.JOptionPane are incorrect. Math is in java.lang (no import needed) and JOptionPane is in javax.swing.
  • Extra brace: remove the { that appears before the constructor.
  • Method signatures: setters must declare a return type (void), e.g. public void setSideLength(...).
  • Field assignments and returns: assign to the field (this.sideLength = x;), and have getters return the field (return sideLength;), not undefined parameter names.
  • Calculations: perimeter should use 3 * sideLength; area should be (Math.sqrt(3) / 4) * sideLength * sideLength.

Minimal corrected structure (illustrates correct signatures and field use):

import javax.swing.JOptionPane;

public class EqTri {
    private double sideLength;
    private String identifier;

    public EqTri(double sideLength, String identifier) {
        this.sideLength = sideLength;
        this.identifier = identifier;
    }

    public void setSideLength(double sideLength) { this.sideLength = sideLength; }
    public void setIdentifier(String identifier) { this.identifier = identifier; }
    public double getSideLength() { return sideLength; }
    public String getIdentifier() { return identifier; }
    public double getPerimeter() { return 3 * sideLength; }
    public double getArea() { return (Math.sqrt(3) / 4) * sideLength * sideLength; }
}

Tip: compile with javac EqTri.java and fix the first error the compiler shows β€” later errors are often cascading. Using an IDE (or javac output) will highlight the exact lines and make these fixes straightforward.

Recommended Answers

All 3 Replies

I think lots of your problems are stemming from an unnecessary curly brace on line 8.

Some padding helps identify the errors.
Try something along the lines of the following:

import java.Math.*;
import java.swing.JOptionPane;
public class EqTri {
    private double sideLength;
    private String identifier;
    public EqTri(double x,String s) {
        sideLength = x;
        identifier = s;
        //note here "s" is not global to the class nor static to the project.
        }
    public setsideLength(double x) {
        x = sideLength;
        }
    public setIdentifier(String s) {
        s = identifier;
        }
    public double getsideLength() {
        //x is undefined. Null Pointer Exception
        return x;
    }
    public String getIdentifier() {
        //s is undefined. Null Pointer Exception
        return s;
    }
    public double getPerimeter() {
        //s is undefined. Null Pointer Exception
        return 3*s;
    }
    public double getArea() {
        //a is undefined. Null Pointer Exception
        return (Math.sqrt(3)/4)*a*a;
    }
}
public double getsideLength() {
        //x is undefined. Null Pointer Exception
        return x;
    }

what do you mean, NullPointerException? this won't compile, how do you expect a RuntimeException on this ?

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.