Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8491061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:18:21+00:00 2026-06-10T22:18:21+00:00

Fairly new to Java, so I hope the wording makes sense. I have some

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T22:18:23+00:00Added an answer on June 10, 2026 at 10:18 pm

    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.

    private static final StringBuffer appendParsedText( StringBuffer parsedText, String snippet )
    {
        if ( parsedText.length() > 0 )
        {
            return parsedText.append( ", " + snippet );
        }
        return parsedText.append( snippet );
    }
    
    public static final String parseAccess( final String text )
    {
        StringBuffer parsedText = new StringBuffer();
    
        if ( text.contains( "string1" ) )
        {
            parsedText = appendParsedText( parsedText, "string1" );
        }
    
        if ( text.contains( "string2" ) )
        {
            parsedText = appendParsedText( parsedText, "string2" );
        }
    
        if ( text.contains( "string3" ) )
        {
            parsedText = appendParsedText( parsedText, "string3" );
        }
    
        return parsedText.toString();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm fairly new to Java and have a doubt that needs to be cleared.
I'm fairly new to Java and am writing an app that needs an XML
I am fairly new to Java, and recently I was reading some material about
I have been working on Eclipse recently. I am fairly new to java programming,
I'm fairly new to Java. Currently trying to have the filename given in args[]
I should start out by saying that I am fairly new to Java EE
I'm fairly new to Java and I've been having some difficulties with Swing. I'm
I'm fairly new to Java development, and people have been suggesting I use Eclipse
I am fairly new to Android and Java and hope you can help me
I'm fairly new to Java & Android, so I have little idea what I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.