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 839737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T05:30:48+00:00 2026-05-15T05:30:48+00:00

Consider the following String : 5|12345|value1|value2|value3|value4+5|777|value1|value2|value3|value4?5|777|value1|value2|value3|value4+ Here is how I want to split string,

  • 0

Consider the following String :

5|12345|value1|value2|value3|value4+5|777|value1|value2|value3|value4?5|777|value1|value2|value3|value4+

Here is how I want to split string, split it with + so I get this result :

myArray[0] = "5|12345|value1|value2|value3|value4";

myArray[1] = "5|777|value1|value2|value3|value4?5|777|value1|value2|value3|value4";

if string has doesn’t contain char "?" split it with "|" and continue to part II, if string does contain "?" split it and for each part split it with "|" and continue to part II.

Here is part II :

myObject.setAttribute1(newString[0]);
...
myObject.setAttribute4(newString[3]);

Here what I’ve got so far :

private static String input = "5|12345|value1|value2|value3|value4+5|777|value1|value2|value3|value4?5|777|value1|value2|value3|value4+";

public void mapObject(String input){

String[] myArray = null;

    if (input.contains("+")) {
        myArray = input.split("+");
    } else {
        myArray = new String[1];
        myArray[0] = input;
    }

    for (int i = 0; i < myArray.length; i++) {

        String[] secondaryArray = null;
        String[] myObjectAttribute = null;

        if (myArray[i].contains("?")) {
            secondaryArray = temporaryString.myArray[i].split("?");

            for (String string : secondaryArray) {
                myObjectAttribute = string.split("\\|");
            }
        } else {
            myObjectAttribute = myArray[i].toString().split("\\|");
        }

        myObject.setAttribute1(myObjectAttribute[0]);
        ...
        myObject.setAttribute4(myObjectAttribute[3]);
                    System.out.println(myObject.toString());
}

Problem :

When I split myArray, going trough for with myArray[0], everything set up nice as it should.

Then comes the myArray[1], its split into two parts then the second part overrides the value of the first(how do I know that?). I’ve overridden toString() method of myObject, when I finish I print the set values so I know that it overrides it, does anybody know how can I fix this?

  • 1 1 Answer
  • 2 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-15T05:30:49+00:00Added an answer on May 15, 2026 at 5:30 am

    The code doesn’t seem to be able to handle the possible expansion of strings in the secondary case. To make the code clearer, I would use a List rather than array.

    private static String input = "5|12345|value1|value2|value3|value4+5|777|value1|value2|value3|value4?5|777|value1|value2|value3|value4+";
    
    private void split(List<String> input, List<String> output, String split) {
        for (String s: input) {
              if (s.contains(split))
              {
                 output.addAll(Arrays.asList(s.split(Pattern.quote(split)));
              }
              else
                  output.add(s);
        }
    }
    

    public void mapObject(String input) {

    List<String> inputSrings = new ArrayList<String>();
    List<String> splitPlus = new ArrayList<String>();
    
    inputStrings.add(input);
    
    split(inputStrings, splitPlus);
    
    List<String> splitQuest = new ArrayList<String>();
    split(splitPlus, splitQuest, "?");
    
    for (String s: splitQuest) {   
       // you can now set the attributes from the values in the list
       // splitPipe
    
       String[] attributes = s.split("\\|");
       myObject.setAttribute1(attributes[0]);
       ....
       myObject.setAttribute4(attributes[3]);
       System.out.println(myObject);
    }
    

    }

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

Sidebar

Ask A Question

Stats

  • Questions 459k
  • Answers 459k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer A quick solution that I'd use in this case is… May 15, 2026 at 11:22 pm
  • Editorial Team
    Editorial Team added an answer I wouldn't install an app that did this. If all… May 15, 2026 at 11:21 pm
  • Editorial Team
    Editorial Team added an answer printf printf "%-11s %-11s %-11s %-11s %-11s %-11s %-11s %-11s\n",… May 15, 2026 at 11:21 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.