Fairly new to Java, so I hope the wording makes sense.
I have some Java code that needs to be called a few times within a public method, how can I reuse that code by placing it inside the public method and calling it?
Maybe I am making things hard for myself, so here’s the code sample.
public static final String parseText( final String text )
{
StringBuffer parsedText = new StringBuffer();
private static void appendParsedText( String snippet )
{
if ( parsedText.length() > 0 )
{
parsedText.append( ", " + snippet );
}
parsedText.append( snippet );
}
if ( text.contains( "string1" ) )
{
parsedText.appendParsedText("string1");
}
if ( text.contains( "string2" ) )
{
parsedText.appendParsedText("string2");
}
if ( text.contains( "string3" ) )
{
parsedText.appendParsedText("string3");
}
return parsedText.toString();
}
Although the code is invalid, hopefully it makes sense with what I’m trying to achieve. I know there’s a join() method in Apache Commons StringUtils but it seems like overkill, this method is the only place where this needs to happen.
I would assume that the following works, but I’m defining a method in my class that is only being used in one other method of the class. To me this seems a little wasteful.