2

I am using textbook Murach's java programming, and in one of the exercises, it is asking me to do the following:

add this method (given by the book):

private static String displayMultiple(Displayable d, int count)

write the code for this method so it returns a String that contains the Displayable parameter the number of times specified by the int parameter.

Displayable is an interface that implements getDisplayText(). And this method just returns a String with instance variables of an object, i.e. for an Employee, it returns first name, last name, department, and salary.

Everything works, except for the "returns a String".

2
  • ...That description makes absolutely no sense. Aside from that, could you provide the code you have so far? It may or may not help us answer. Commented May 21, 2015 at 22:12
  • 4
    I understand the exercise like this: Let d be an instance of Displayable such that d.getDisplayText() would return "ho". Then displayMultiple(d, 3) is supposed to return the string "hohoho". Commented May 21, 2015 at 22:15

3 Answers 3

6

This is probably an exercise about loops:

  • You have a way to convert d to a string: getDisplayText. This yields, say, "ABCD"
  • You want to return count times that string "ABCD". If count == 3, that means "ABCDABCDABCD".

Useful keywords: for loop, StringBuilder. Here is a template that you can use to get started:

String text = ;// Use getDisplayText here
StringBuilder ret = new StringBuilder();
/* Loop from 0 to count - 1 */ {
    // Append `text` to `ret` 
}
return ret.toString();

You don't actually need to return multiple values.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you, this also makes sense! Although the book hasn't taught us about StringBuilder class yet. It's coming up in two chapters though!
The problem with using += is that it may behave badly for large values of count; in this simple case you may be fine, but using += in a loop is a bit of an anti-pattern (+= creates a new string every time, so in the ABCD example you end up creating ABCD, then ABCDABCD, then ABCDABCDABCD, and so on; this is wasteful)
How is that different than appending in a loop to a StringBuilder object?
StringBuilder does it efficiently (you only create one stringbuilder that grows, no count distinct stringbuilders) :)
3

As I understand it:

private static String displayMultiple(Displayable d, int count){
   String s = "";
   String ss = d.getDisplayText();
   for(int i=0; i<count; i++){
      s += ss;
   }
   return s;
}

2 Comments

I think this is it! I am retarded for thinking it was asking to return that String three times. That's not possible, right? Thank you!
Avoid concatenating strings inside loops using + operator since it each times has to create new instance of string which by copying old string and adding new one. Use StringBuilder instead and its append method to store data you want to be in string. Then when you are done simply call toString on builder to get your result.
2

If you want to return multiple values with out using collection then you can create an Class -

public class MultipleValue{

   String firstValue;
   String secondValue;

   //other fields

}  

Then from someMethod() where you want to return multiple value (that is firstValue, secondValue) you can do this -

public MultipleValue someMethod(){

   MultipleValue mulVal = new MultipleValue();
   mulVal.setFirstValue("firstValue");
   mulVal.setSecondValue("secondVAlue");

   return mulVal;
}  

Then from the calling class of someMethod() you can extract multiple values (that is firstValue and secondValue) like this -

//from some calling method
MultipleValue mulVals = someMethod();

String firstValue = mulVals.getFirstValue();
String secondValue = mulVals.getSecondValue();

2 Comments

Could I return an object of class MultipleValue multiple times this way?
In this case you need to call someMethod() multiple time

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.