Home > other >  How do I fix this issue for constructing an object, declaring a method in java?
How do I fix this issue for constructing an object, declaring a method in java?

Time:12-11

It states that there is an error for my construtor method for "public station", it says I need a return type but in my previous codes I've never had that issue when trying to construct an object. If i use public void then I am unable to create that object since something is wrong with my constructor.

enter image description here

this is when I use "public void station

enter image description here

public class StationReport{
//fields
/*# YOUR CODE HERE */
private String NAME;
private int yCoordinate;
private int xCoordinate;
private String dATE;
private int t;
private double temperature;
private double dewPoint;
private double pressure;

//Constructor
/*# YOUR CODE HERE */
public Station(String name, int x, int y, String date, int time, double temp, double dewpt, double press){
this.NAME = name;
this.xCoordinate = x;
this.yCoordinate = y;
this.dATE = date;
this.t = time;
this.temperature = temp;
this.dewPoint = dewpt;
this.pressure = press;
}


// Getter methods
/*# YOUR CODE HERE */
public String getName(){
return this.NAME;
}  

public int getX(){
return this.xCoordinate;
}

public int getY(){
return this.yCoordinate;
}

public String getDate(){
return this.dATE;
}

public int getTime(){
return this.t;
}

public double getTemperature(){
return this.temperature;
}

public double getDewPoint(){
return this.dewPoint;
}

public double getPressure(){
return this.pressure;
}

// toString method
/** YOUR DOCUMENTATION COMMENT */
public String toString(){
    /*# YOUR CODE HERE */
    return NAME   ": "   xCoordinate   ": "   yCoordinate   ": "   dATE   ": "   t   ": "   temperature   ": "   dewPoint   ": "   pressure   ": ";
}

}

this is the code for when I am trying to create the object but it says that something is wrong with my constructor.

/**  
     * Loads all the reports from the stations out of the file into
     * the list of all reports.
     */
    public void loadReports(){
        this.allReports.clear();
        try {
            List<String> lines = Files.readAllLines(Path.of("reports.txt"));
            for (String line: lines){
                Scanner sc = new Scanner(line);
                String name = sc.next();
                int x = sc.nextInt();
                int y = sc.nextInt();
                String date = sc.next();
                int time = sc.nextInt();
                double temp = sc.nextDouble();
                double dewpt = sc.nextDouble();
                double press = sc.nextDouble();
                StationReport report = new StationReport(name, x, y, date, time, temp, dewpt, press);
                this.allReports.add(report);
            }
            UI.printf("Loaded %d reports.\n", allReports.size());
        } catch(IOException e){UI.println("File reading failed");}    
    }

I tried everything i could but nothing worked.

CodePudding user response:

Constructor name must match the class name, and it cannot have a return type (like void).

Oracle Tutorial: constructors

CodePudding user response:

    /*# YOUR CODE HERE */
public Station(String name, int x, int y, String date, int time, double temp, double dewpt, double press){
this.NAME = name;
this.xCoordinate = x;
this.yCoordinate = y;
this.dATE = date;
this.t = time;
this.temperature = temp;
this.dewPoint = dewpt;
this.pressure = press;
}

There is a mistake in your constructor Always ClassName and constructor name should match Here replace the above methods name same as class name it will work

  • Related