Liam Nassau Liam Nassau - 11 months ago 102
Java Question

Program not returning elements of array

below is a program where I need to store String time and int datum as objects in an array and then print them out. This is what I am up to currently. I believe it is an issue with my recordEvent method.

class RecordEvents4 {
public static void main(String[] args){
Recorder4 r1 = new Recorder4(100,100,"Wombat Detection");
r1.recordEvent("10:53",20);
r1.recordEvent("10:59",20);
r1.recordEvent("11:05",20);
r1.recordEvent("12:59",10);
r1.recordEvent("13:59",10);
r1.recordEvent("14:06",10);
r1.printEvents();
}
}

class Recorder4 {
int xPos, yPos;
String eventType;
EventInformation[] event = new EventInformation[10];

int xevent = 0;

final int EVENT_MAX = 10;

Recorder4(int xPos, int yPos, String eventType){
this.xPos = xPos;
this.yPos = yPos;
this.eventType = eventType;
}

public void recordEvent(String time, int datum){
/*for(int i = 0; i<event.length; i++){
event[i] = new EventInformation(time, datum);
}*/

event[xevent] = new EventInformation(time, datum);

xevent++;
if(xevent > EVENT_MAX){
System.out.println("Event log overflow - terminating");
System.exit(1);
}
}

void printEvents(){
System.out.println("Record of " + eventType + " events at [" + xPos + "," + yPos + "]");
for(int i = 0; i < xevent; i++){
System.out.println("Event number " + i + " was recorded at " + event[i] + " with datum = " + event[i]);
}
}
}

class EventInformation {
String eventTime;
int eventDatum;

EventInformation(String eventTime, int eventDatum){
this.eventTime = eventTime;
this.eventDatum = eventDatum;
}
}


This is what it is outputting:

Record of Wombat Detection events at [100,100]
Event number 0 was recorded at [email protected] with datum = [email protected]
Event number 1 was recorded at [email protected] with datum = [email protected]
Event number 2 was recorded at [email protected] with datum = [email protected]
Event number 3 was recorded at [email protected] with datum = [email protected]
Event number 4 was recorded at [email protected] with datum = [email protected]
Event number 5 was recorded at [email protected] with datum = [email protected]


This is what I am expecting:

Record of Wombat Detection events at [100,100]
Event number 0 was recorded at 10:53 with datum = 20
Event number 1 was recorded at 10:59 with datum = 20
Event number 2 was recorded at 11:05 with datum = 20
Event number 3 was recorded at 12:59 with datum = 10
Event number 4 was recorded at 13:59 with datum = 10
Event number 5 was recorded at 14:06 with datum = 10


I have been advised to use toString() but I am unsure whether I am even in the right direction with the recordEvent() where I am meant to be assigning the method arguments to to an EventInformation object, a reference to which is stored in an element of the event array.

Answer Source

You are effectively asking Java to print out an element of your events array. In your declaration of this array, you gave elements inside it the EventInformation type. Java, by default, obviously doesn't know how to print this, since you defined it yourself! To fix this, change your print statement:

    System.out.println("Event number " + i + " was recorded at " + event[i].eventTime + " with datum = " + event[i].eventDatum);

Now you are asking Java to print eventTime (a string) and eventDatum (an int). These are both things that Java knows how to print, since these are defined by Java itself.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download