2

I have a class UserFunction and it have two method getAudioFunction and getPromptFunction with returning String value, my problem is that i want to return both value in one method how can i able to do that

UserFunction.java

 public class UserFunction{ Map<String,PromptBean> promptObject=new HashMap<String,PromptBean>(); Map<String,AudioBean> audioObject = new HashMap<String,AudioBean>(); XmlReaderPrompt xrpObject=new XmlReaderPrompt(); public String getAudioFunction(String audioTag,String langMode ) { Map<String, AudioBean> audioObject=xrpObject.load_audio(langMode); AudioBean audioBean=(AudioBean)audioObject.get(audioTag); String av=StringEscapeUtils.escapeXml(audioBean.getAudio()); return av; } public String getPromptFunction(String promptTag,String langMode ) { Map<String, PromptBean> promptObject=xrpObject.load(langMode); PromptBean promptBean= (PromptBean)promptObject.get(promptTag); String pv=StringEscapeUtils.escapeXml(promptBean.getPrompt()); return pv; } } 
1
  • return custom class object instead of string Commented Oct 23, 2012 at 11:38

5 Answers 5

4

You need to return an object which holds both values. You could create a class for this purpose. The class can have two getter methods for retrieving the values.

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

Comments

3

It is not possible to return more than one value from a method in java. You can set multiple value into Map or List or create a custom class and can return that object.

public Map<String,String> getAudioAndPromptFunction(String audioTag,String langMode ) { Map<String,String> map =new HashMap(); ... map.put("audioBean",StringEscapeUtils.escapeXml(audioBean.getAudio())); map.put("promptBean",StringEscapeUtils.escapeXml(promptBean.getPrompt()); return map; } 

or you can create a custom bean class like.

public class AudioPrompt{ private String audioBean; private String promptBean; ... } public AudioPrompt getAudioAndPromptFunction(String audioTag,String langMode ) { AudioPrompt audioPrompt =new AudioPrompt(); ... audioPrompt.set(StringEscapeUtils.escapeXml(audioBean.getAudio())); audioPrompt.set(StringEscapeUtils.escapeXml(promptBean.getPrompt()); return audioPrompt ; } 

Comments

1

You'll need to return an object that includes both of the values. This could be an array with two elements, a Pair<A,B> class (which holds two generic values, typically from some pan-project utility library), or a method-specific class such as:

public class UserFunctionXmlPairing { public final String audioBeanXml; public final String promptBeanXml; } 

Comments

1

Create a new class that holds your two strings and return that.

class AudioPromptPair { private String audio; private String prompt; public AudioPromptPair(String audio, String prompt) { this.audio = audio; this.prompt = prompt; } // add getters and setters } 

Comments

1

You can wrap all the values you wish into a single object and return that:

public class Prompts { private Map<String, Object> prompts = new HashMap<String, Object>(); public void addPrompt(String name, Object prompt) { this.prompts.put(name, prompt); } public Object getPrompt(String name) { this.prompts.get(name); } } 

It's even easier if your AudioBean and PromptBean have a common super class or interface.

My preference would be to lose the "Bean" in your class names. AudioPrompt and TextPrompt would be preferred.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.