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

  • Home
  • SEARCH
  • 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 6128179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:35:52+00:00 2026-05-23T16:35:52+00:00

I created a program which will parse the firstName, middleName and lastName. Here is

  • 0

I created a program which will parse the firstName, middleName and lastName. Here is the program and output. This program can definitely be improved and need some input on reducing my cumbersome ugly code and replace it with a better one. Any suggestions or example ?

public class Test {

  public static void main(String[] args) {

    String fullName = "John King IV. Cena";
    String[] tokens = fullName.split(" ");
    String firstName = "";
    String middleName = "";
    String lastName = "";
    if(tokens.length > 0) {
        firstName = tokens[0];
        middleName = tokens.length > 2 ? getMiddleName(tokens) : "";
        lastName = tokens[tokens.length -1];
    }
    System.out.println(firstName);
    System.out.println(middleName);
    System.out.println(lastName);
  }

  public static String getMiddleName(String[] middleName){
      StringBuilder builder = new StringBuilder();
      for (int i = 1; i < middleName.length-1; i++) {
          builder.append(middleName[i] + " ");
      }

      return builder.toString();
  }
}

John
King IV.
Cena

  • 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-05-23T16:35:52+00:00Added an answer on May 23, 2026 at 4:35 pm

    This code does the same, but doesn’t keep a trailing space in the middle name. This is one of several possible cleaner implementations.

    public class Test {
    
        public static void main(String[] args) {
    
            String name = "John King IV. Cena";
    
            int start = name.indexOf(' ');
            int end = name.lastIndexOf(' ');
    
            String firstName = "";
            String middleName = "";
            String lastName = "";
    
            if (start >= 0) {
                firstName = name.substring(0, start);
                if (end > start)
                    middleName = name.substring(start + 1, end);
                lastName = name.substring(end + 1, name.length());
            }    
    
            System.out.println(firstName);
            System.out.println(middleName);
            System.out.println(lastName);
        }
    }
    

    As the guys said, next time go directly to https://codereview.stackexchange.com/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I created a program using dev-cpp and wxwidgets which solves a puzzle. The user
I need to solve the following question which i can't get to work by
I'm trying to create a program which will visualize different sorting algorithms by drawing
I have created a program which takes a text file full of 3 letter
I have an AppleScript program which creates XML tags and elements within an Adobe
I am looking to create a video training program which records videos - via
If i create a program, which in one small out of the way area,
I've created a program in Delphi that uses Google's AJAX Search API to evaluate
I have a web service I'm trying to load test. I created a program
I'm trying to make a program in Visual C# that has my one created

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.