Fizzix
Fizzix

Reputation: 24375

How to return a string of arrays from another method?

This is homework for a class I am taking. So I do not expect anyone to write code for me, just guidance for pseudo code is fantastic :)

So I have been given a main method by my teacher. This method creates an array of objects, sorts them into alphabetical order, then calls another method with "System.out.println(loans[0].Schedule());". This method should print out a schedule of that loan. What it is is kinda irrelevant, so I won't go into detail about it, but the method has a 2D array in it.

As you may know, the Schedule method cannot be void (I don't no why, it just gives me an error). So I tried making it "public String[][] Schedule()", but my teacher said that will not work since the way it is being called, "System.out.println(loans[0].Schedule());", println cannot handle printing a full 2D array.

Does anyone have any alternatives? All help is appreciated.

Upvotes: 3

Views: 236

Answers (2)

Fizzix
Fizzix

Reputation: 24375

Very helpful answer! Thank you!

In my schedule method, I must actually generate a schedule, which can be lines and lines long, with calculations. Do you suggest I keep concatenating these to a single sting?

For example:

String schedule = ""
for(int i = 1; i <= term + 1; i++)
{
  schedule += i + ":";
  schedule += payment + "";
  //And so on so on for the rest of the schedule
}

Upvotes: 0

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285403

If you are calling it like you note above, System.out.println(loans[0].schedule());, then the schedule() method must return a String, not an array of String, not a 2-D array of String, but a single String since that is what println is expecting (either that or an object that has a decent toString() method override, but that's the subject of another discussion).

Note that the schedule() method should begin with a lower-case letter to comply with Java naming conventions.

Note also that the reason that schedule() cannot return void is because it has to return an object for the println(...) method to print.

For more details regarding this answer, you may want to give more details about your problem.

Upvotes: 3

Related Questions