-2

So the method is called:

private void print()
{
    System.out.println("The list of Devices is:");
    for(int i = 0; i < numDevices; i++)
    {
        System.out.println(list[i].toString());
    }
}

When it gets to toString method it brings it here

public String toString()
{   
     String temp = name + ": " + readings[0];
     return temp;
}

My issue is when I call on the method print its supposed to out put all data values inside of readings instead I can only return one data value. Any help would be appreciated and if more information is needed let me know as this is two of 3 files.

My output:

The list of Devices is:

Speedometer: 3

Alitimeter: 1

there output is

The list of Devices is:

Speedometer: 3,39.7,93.2,193.2,0.0,0.0

Altimeter: 1,422.6,98.7,340.5
Puffin GDI
  • 1,702
  • 5
  • 27
  • 37
  • You should look at this: http://stackoverflow.com/questions/2832472/how-to-return-2-values-from-a-java-function it looks similar to your question. – BitNinja Feb 13 '14 at 02:31
  • yes it does seem very similar the issue is I cant create another class. additionally no new objects may be created in the class where tostring is located at. – user3304256 Feb 13 '14 at 02:34

2 Answers2

2
  public String toString() {
        String str = name + ": ";
        for (int i = 0; i < readings.length; i++) {
            if (i < readings.length - 1) {
                str += readings[i] + ", ";
            } else {
                str += readings[i];
            }
        }
        return str;
    }

Try replace your toString method like this.

Drogba
  • 4,186
  • 4
  • 23
  • 31
0

You have the line

 String temp = name + ": " + readings[0];

The "[0]" means only do the first item in the readings array. You need to find a way to do ALL the items in the array. For example, if there were 3 items in the array, you could do

 String temp = name + ": " + readings[0] + "," + readings[1] + "," + readings[2];

Of course, since the number of items in the array can vary, you'll want to put it into a loop. Which is what Drogba's example does.

Good luck!

Roy
  • 974
  • 6
  • 11