0

I'm stuck with a error I repedeatly got in Eclipse. I think that this error shouldnt exist. Given ist the following nested loop:

String conditionlogic;
if(matcher.find())
{               
    do
        conditionogic += matcher.group();
    while(matcher.find());
}
else
    conditionogic = null;

The error is

The local variable conditionlogic may not have been initialized.

It is my understanding, that since do-while will be executed at least once conditionlogic is actually initialized with one or more values of matcher.group() or else null. How can I fix this?

1
  • use { } with do and while too Commented Jul 23, 2014 at 10:01

4 Answers 4

3

What does this line means:

conditionogic += matcher.group();

This is the same as:

conditionogic = conditionogic + matcher.group();

i.e. it means: take the value of conditionogic and concatenate to it matcher.group(). But value of conditionogic indeed is unknown when you enter the loop first time. You have to assign something to this variable, for example empty string:

String conditionlogic = "";
Sign up to request clarification or add additional context in comments.

4 Comments

+1. I'd like to have a fairly robust conversation with the guys who allowed += to be used on a string in Java. A kludge in a language that does not allow operator overloading; in my opinion.
@Bathsheba, be lenient to beginners. You cannot expect every person that starts learning java to use StringBulder or guava Joiner for string concatenation.
Thank you. I realized that shortly after -.- Why shouldn't I use += on a String? What are the advantages of other methods mentioned?
String is immutable, i.e. its content is never changed. When you write res = s1 + s2 you create additional instance of String. When you are doing this in loop number of additional objects is like number of your iterations. StringBuilder solves this problem by accumulating chunks and creating the final string when all concatenations are done.
0

Since you are using conditionogic += matcher.group(); (a += operator), So you have to initialize your conditionlogic with empth String "", like this :

String conditionlogic = "";

Comments

0

The problem in your case is the += operator. What do you += on if the variable is not initialized?

You probably want to initialize the value to "".

Comments

0

If you look at your code carefully, you're trying to concatenate the result of matcher.group() to null. The error is perfectly valid, you should initialize conditionLogic to "".

However, this at the moment isn't really good practice, concatenating strings like that in a loop. The right way to do it is this:

String conditionLogic = null;
if(matcher.find())
{
    StringBuffer sb = new StringBuffer();
    do
    {
        sb.append(matcher.group());
    }
    while(matcher.find());
    conditionLogic = sb.toString();
}

Comments

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.