2

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

7
  • 2
    Without knowing what you have done, it is hard to fix the code. Commented Sep 28, 2012 at 3:34
  • please let us know what the situation exactly is. Commented Sep 28, 2012 at 3:35
  • you better post your code here, let us fix it .. :) Commented Sep 28, 2012 at 3:35
  • You could return an array (String[]) or a List<String> instead Commented Sep 28, 2012 at 3:37
  • You could concatenate the String's into a single String using something like StringBuilder through the loop Commented Sep 28, 2012 at 3:49

5 Answers 5

2

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
Sign up to request clarification or add additional context in comments.

7 Comments

It is telling me that I can not make a static reference to a non-static method when i try to do sb.append(String)
First line of example StringBuilder sb = new StringBuilder(25);. You need create an instance of StringBuilder before you can use it
I have the line StringBuilder choices = new StringBuilder(""); Now it's saying unreachable code again and i have exactly what you have minus the return statement
Works fine for me. I've added a working example (fudged the data)
Works fine for me, outputs A: one, B: two, C: three, D: four. Make sure you're recompile your code. Are you using an IDE? If so which one?
|
2

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: "[]".

Comments

0

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;
 }

2 Comments

You can also use ArrayList Class to add list of Strings to it
This is being used in a toString, so I can only return Strings
0

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.

Comments

0

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

11 Comments

This is being used in a toString so I can't return a list
@cheesery You could, you would just need to format the list of values once they return
I updated the pseudo code. Simplest way is to build up a long string in the loop. You would want to use StringBuffer instead if the String is really long.
@digidigo I see where you're going with this, but aren't Strings immutable
Yes they are. the code assigns a new object to returnString on every iteration of the loop. Use StringBuffer if you are going through the loop more than just a few time.
|

Your Answer

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