user1212818
user1212818

Reputation:

How do I return multiple Strings from a for loop in Java?

I'm using a for loop, and it needs a return a String every time through the loop, but using "return" breaks the loop and Eclipse throws up an "Unreachable Code" error. Any suggestions

Upvotes: 2

Views: 9521

Answers (5)

MadProgrammer
MadProgrammer

Reputation: 347184

I think your logic is broken

for (int counter = 0; counter < possibleAnswers.length; counter++){
    // This condition will be meet immediately because 0 is less then 25...
    if (counter < 25){
        return alpha[counter] + ": " + possibleAnswers[counter] + "\n";
    }
    // Meaning it is impossible for the program to ever reach this line...
    if (counter >= 26){
        return alpha[26] + a + ": " + possibleAnswers[counter] + "\n";
        a++;
    }
}

I think you might be better of trying something like...

StringBuilder sb = new StringBuilder(25);
for (int counter = 0; counter < possibleAnswers.length; counter++){
    if (counter < 25){
        sb.append(alpha[counter] + ": " + possibleAnswers[counter] + "\n");
    }
    if (counter >= 26){
        sb.append(alpha[26] + a + ": " + possibleAnswers[counter] + "\n");
        a++;
    }
}
return sb.toString();

UPDATE working example

String possibleAnswers[] = new String[30];
String alpha[] = new String[30];

for (int index = 0; index < 30; index++) {
    possibleAnswers[index] = "Happy " + index;
    alpha[index] = Integer.toString(index);
}

int a = 0;

StringBuilder sb = new StringBuilder(25);
for (int counter = 0; counter < possibleAnswers.length; counter++) {
    if (counter < 25) {
        sb.append(alpha[counter]).append(": ").append(possibleAnswers[counter]).append("\n");
    }
    if (counter >= 26) {
        sb.append(alpha[26]).append(a).append(": ").append(possibleAnswers[counter]).append("\n");
        a++;
    }
}

System.out.println(sb);

Which outputs

0: Happy 0
1: Happy 1
2: Happy 2
3: Happy 3
4: Happy 4
5: Happy 5
6: Happy 6
7: Happy 7
8: Happy 8
9: Happy 9
10: Happy 10
11: Happy 11
12: Happy 12
13: Happy 13
14: Happy 14
15: Happy 15
16: Happy 16
17: Happy 17
18: Happy 18
19: Happy 19
20: Happy 20
21: Happy 21
22: Happy 22
23: Happy 23
24: Happy 24
260: Happy 26
261: Happy 27
262: Happy 28
263: Happy 29

Upvotes: 2

Ted Hopp
Ted Hopp

Reputation: 234795

If returning an ArrayList<String>, isn't right for your application, this sounds like a job for a call-back function. Define a call-back interface:

public interface StringDelivery {
    public void processString(String aString);
}

Then in your loop you can call back:

public void loopThroughStrings(StringDelivery callback) {
    for (. . .) {
        String nextString = . . .
        callback.processString(nextString);
    }
}

You can then call this with any object that implements the interface.

EDIT:

If you're computing a bunch of strings but need to return them as a single string, then you can put them in an array and then use Arrays.toString(Object[] array) to convert the entire array to a single String:

int n = <number of strings>
String[] strings = new String[n];
for (int i = 0; i < n; ++i) {
    strings[i] = <i-th string>
}
return Arrays.toString(strings);

The return value will be formatted with the list elements separated by ", " and enclosed in square brackets: "[]".

Upvotes: 2

digidigo
digidigo

Reputation: 2584

You will want to use an ArrayList to store all your strings.

And then return the ArrayList after your for loop has completed. Something like.

String returnString = "";

for loop ....{
   returnString = returnString + " " + someNewString;
 }


return returnString

Upvotes: 0

Subir Kumar Sao
Subir Kumar Sao

Reputation: 8401

You should consider returning an array of String from the method.

Just to correct a for loop does not return anything. Its the function that is returning a value.

Lastly its difficult to comment as its not clear what you are trying to achieve.

Upvotes: 0

Sanket
Sanket

Reputation: 393

Why to complicate matters .. just return an array of string

 public String[] function1()
 {
    String[] str1 new String(10);

    for(int i=0;i<10;i++)
    { 
          str1[i] ="string"+i;
    }
    return str1;
 }

Upvotes: 0

Related Questions