Frage

I'm having an issue getting the variables in the constructors to show up in my output in my main method. I can get the program to work just using methods, however, the problem arises when using constructors. Any help or tip in the right direction would be great!

public class Time {
    public static void main (String[] args) {
        TimeCalculations time1 = new TimeCalculations();
        System.out.println(time1.getCurrentTime());
        System.out.println(time1.getElaspedTime());

    public static long input() {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter a time");
        return TimeCalculations.elaspedTime = input.nextLong();}

class TimeCalculations {
    public long currentTime;
    public static long elaspedTime;


public TimeCalculations() {

    currentTime = System.currentTimeMillis();
    this.currentTime = currentTime;
    }

    public TimeCalculations(long currentTime, long elaspedTime) {
        elaspedTime = currentTime -Time.input();
    }

    public long getCurrentTime() {
    return this.currentTime;
    }

public long getElaspedTime() {      
    return TimeCalculations.elaspedTime;
    }
War es hilfreich?

Lösung

I am sorry! but I didn't get this! "however, the problem arises when using constructors"
Can you please give some more details!
Thanks!
Thanks! for your details. just call the input method in public static void main at first line

import java.util.*;
public class Time{
public static void main(String[] args) {
    input();
    TimeCalculations time1 = new TimeCalculations();
    System.out.println(time1.getCurrentTime());
    System.out.println(time1.getElaspedTime());
}
 public static long input() {
    Scanner input = new Scanner(System.in);
    System.out.println("Enter a time");
    return TimeCalculations.elaspedTime = input.nextLong();}

}

class TimeCalculations {
public long currentTime;
public static long elaspedTime;


public TimeCalculations() {
    currentTime = System.currentTimeMillis();
    this.currentTime = currentTime;
}

public long getCurrentTime(){
    return this.currentTime;
}

public long getElaspedTime() {      
    return TimeCalculations.elaspedTime;
}

}

Andere Tipps

In your second constructor, you are essentially not doing anything. Since you are using the same variable names as parameters, your global variables elaspedTime and currentTime are hidden. Change it to the following: (add the this.)

public TimeCalculations(long currentTime, long elaspedTime) {
    this.elaspedTime = currentTime -Time.input();
}

Secondly, it doesn't make sense what you are doing in this constructor. In this case, passing currentTime and elapsedTime as constructor params means you should just set them to the globals like:

public TimeCalculations(long currentTime, long elaspedTime) {
    this.elaspedTime = elapseTime;
    this.currentTime = currentTime;
}

In your first constructor, you are just setting currentTime twice. Change to something like:

public TimeCalculations() {
    // since there is no local var named currentTime, you don't need
    // to put this.
    currentTime = System.currentTimeMillis();
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top