我对Java相当陌生,至今只用了几个月的时间编程。使用toString函数返回时的Java错误
我有两个类,TimeSlot
和LabGroup
。
在TimeSlot
类有在LabGroup
类的代码 -
private Time start;
private Time end;
private String day;
public TimeSlot(String spec) {
//splits given string on each space
String[] splitSpec = spec.split(" ");
day = splitSpec[0];
//uses the class Time, and passes in the hour and the minute of the time the lab begins.
this.start = new Time(splitSpec[1]);
//uses the class Time, and passes in the hour and the minute of the time the lab finishes.
this.end = new Time(splitSpec[2]);
}
然后是代码 -
public String charLabel;
public TimeSlot timeSpec;
public String lineTime;
public LabGroup(String line) {
String[] lineSplit = line.split(" ");
charLabel = lineSplit[0];
//string a = "Day StartTime EndTime"
String a = lineSplit[1] + " " + lineSplit[2] + " " + lineSplit[3];
timeSpec = new TimeSlot(a);
}
与toString
method--
public String toString() {
return "Group "+ charLabel + timeSpec+ "\n";
}
沿
输入到LabGroup
的示例将是"A Mon 13:00 15:00"
然后应该给输出,通过toString
,中 -
Group A Mon 13:00 - 15:00
Group B Mon 15:00 - 17:00
Group C Tue 13:00 - 15:00
Group D Tue 15:00 - 17:00
但是,相反我在班级LabGroup getting--
Group [email protected]
, Group [email protected]
, Group [email protected]
, Group [email protected]