Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

array needs to be returned instead of string

 
vin Hari
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi experts,

i am in a strange situation, i have a class which is returning one value at a time and returning the string finally one at a time,
ex:

but here on one condition the list has to return 2 values so i can change that code but the return value will be array,but return type is just string
how to handle this. the sample code i want to change as.

please suggest me how to handle this.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Before we go into how to address your question as asked, let's take a step back and get the big picture. Why do you think you need to sometimes return a String and sometimes an array? What is this method's job--what does it do and what does it produce--in non-Java terms?

I can think of at least 3 different ways to do what you want, but they're basically just band-aids on a broken design.
 
Manoj Kumar Jain
Ranch Hand
Posts: 198
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I understand you query, I think you want to return 2 values from the list back to the calling function but you want to keep the return type of function as String anyways.

you can just append 2 values by some separator in a String and can return the same back. You need to parse this String containing both of the value in the calling function.

Hope I answered your query
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Manoj Kumar Jain wrote:As I understand you query, I think you want to return 2 values from the list back to the calling function but you want to keep the return type of function as String anyways.

you can just append 2 values by some separator in a String and can return the same back. You need to parse this String containing both of the value in the calling function.

Hope I answered your query


Yeah, that would be one of the band-aids that I'd like to avoid.

I'd prefer the OP tell us what he's trying to do, so an appropriate approach can be suggested.
 
Manoj Kumar Jain
Ranch Hand
Posts: 198
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:
I can think of at least 3 different ways to do what you want, but they're basically just band-aids on a broken design.

Hey Jeff could you please tell us about the rest of 2 band-aids...

I'd prefer the OP tell us what he's trying to do, so an appropriate approach can be suggested.

Couldn't get OP??

Yes it will be better to know first what he is trying to do..
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Manoj Kumar Jain wrote:
Jeff Verdegan wrote:
I can think of at least 3 different ways to do what you want, but they're basically just band-aids on a broken design.

Hey Jeff could you please tell us about the rest of 2 band-aids...


Nope. I'm not going to promote bad practices.


 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Manoj Kumar Jain wrote:
Couldn't get OP??


Original Poster (the person who started the thread).

Or sometimes, Original Post (the first post in the thread).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic