[英]Need help figuring out why I am getting a java.lang.StackOverflowError on my TestClock.java program
Hello and thanks for any help you can offer,您好,感谢您提供的任何帮助,
I am still pretty new to Java and I need some help figuring out why my program doesn't work.我对 Java 还是很陌生,我需要一些帮助来弄清楚为什么我的程序不起作用。 Everything looks good when I compile and I used two command line args (11:45:12 11:48:13).编译时一切看起来都很好,我使用了两个命令行参数 (11:45:12 11:48:13)。 When I run the program it kicks back this error:当我运行该程序时,它会返回此错误:
Exception in thread "main" java.lang.StackOverflowError at Clock.toString(Clock.java:37)
What am i forgetting to do?我忘了做什么? Any idea what i need to fix?知道我需要修复什么吗?
Here is the code:这是代码:
For my Clock Class:对于我的时钟类:
//header files
import java.time.LocalTime;
import static java.lang.System.out;
// creating class clock
public class Clock {
// private data fields
private LocalTime startTime;
private LocalTime stopTime;
// no argument cosntructor to initilize startTime to current time
protected Clock() {
startTime = LocalTime.now();
}
//method start() resets the startTime to the given time
protected LocalTime start() {
startTime = LocalTime.now();
return startTime;
}
//method stop() sets the endTime to given time
protected LocalTime stop() {
stopTime = LocalTime.now();
return stopTime;
}
//getElapsedTime() method returns elapsed time in sconds
private void geElapsedTime() {
long elapsedTime = stopTime.getSecond() - startTime.getSecond();
out.println("Elapsed time is seconds: " + elapsedTime);
}
public String toString() {
return toString();
}
}
For my TestClock Class:对于我的 TestClock 类:
// header files
import java.time.LocalTime;
import static java.lang.System.err;
import static java.lang.System.out;
// creating class of TestClock
class TestClock {
// construct a clock instance and return elapsed time
public static void main(String[] args) {
// creating object
Clock newClock = new Clock();
// checking the condition using loop
if (args.length == 2) {
LocalTime startTime = LocalTime.parse(args[0]);
LocalTime endTime = LocalTime.parse(args[1]);
}
else {
err.println("Application requires 2 command line arguments");
System.exit(1);
}
// display new clock value
out.println(newClock);
}
}
Your toString()
method in the Clock class is recursively calling itself. Clock 类中的toString()
方法正在递归调用自身。 I think you probably wanted super.toString()
, although, in that case overriding the method in the first place is unnecessary.我认为您可能想要super.toString()
,不过,在这种情况下,首先覆盖该方法是不必要的。 If you wanted to print the times, you would use startTime.toString()
or stopTime.toString()
.如果你想打印时间,你可以使用startTime.toString()
或stopTime.toString()
。
You are returning toString() to itself basically, which looks to me to be a recursive call.您基本上将 toString() 返回到自身,这在我看来是一个递归调用。 This is over-flowing the stack and giving you the error.这是溢出堆栈并给您错误。 toString() is a method in the Object class, which all objects inherit from. toString() 是 Object 类中的一个方法,所有对象都继承自该方法。 You need to over-ride it with your own String interpretation.您需要使用自己的字符串解释来覆盖它。 You should do something like你应该做类似的事情
@Override
public String toString()
{
return "The starttime is: " + startTime " and endtime is: " + endTime";
}
Your toString method calls itself infinitely.您的 toString 方法无限地调用自己。 You should just remove it,especially since it does not output anything special.您应该删除它,特别是因为它不会输出任何特殊内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.