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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T23:17:24+00:00 2026-06-08T23:17:24+00:00

I have a simple issue but it’s been driving me nuts lately. I’m making

  • 0

I have a simple issue but it’s been driving me nuts lately.
I’m making a Chemistry program (Android app, to be more specific) and I have a method that separates elements that the user inputs. For example, if someone where to enter “FeZnKPb”, it would be separated into “Fe”, “Zn”, “K”, and “Pb”.

To do so, I’m using a few nested loops and variables to control it. I have all the elements defined in the Chem[] array. The separated elements are then stored in an array called savedChem[]. It basically just loops through all the element constants (Chem[]) and copies the name and formula of those elements into savedChem[] if it matches the input.

Here is my code for this:

public void separateElements(String Input)
{
    boolean found = false;
    int start = 0;
    int end = 2;
    int length = Input.length();

    while(length >= end)
    {
        for(int x = 0; x < numChemicals; x++)
        {
            if((end + 0) > length)
            {
                end += 5;
                break;
            }
            if(Input.substring(start, end).equals(Chem[x].getFormula()))
            {
                savedChem[numSaved].setName(Chem[x].getName());
                savedChem[numSaved].setFormula(Chem[x].getFormula());
                numSaved++;
                start += 2;
                end += 2;
                found = true;
            }
            else
            {
                found = false;
            }
        }
        if(!found)
        {
            start += 2;
            end += 2;
        }
    }
}

My problem is that it only works with 2-character elements such as “Fe” or “Zn”. I want it to also recognize others like “K”.
Another issue is that it sometimes skips some other elements. For example, if I were to input “FeZnHg” it would separate them into “Fe”, “Zn”, and “Hg”. However, if I enter them in a different order like “ZnFeHg”, it only detects “Zn” and “Hg” but not “Fe” for some reason.

What other ways am I able to solve this to get it to work correctly?

  • 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-08T23:17:25+00:00Added an answer on June 8, 2026 at 11:17 pm

    Given that elements are always either an upper case character and one or two lower case characters (and two only in the case of some very short lived elements), you can just use a regex with some lookahead to split your input into short strings.

    You can split your string up into element strings with the split method and some lookahead. Taking Nathaniel Ford’s comments into account:

    public enum ChemicalElement {
        F, Fe, Zn, K, Pb, Umm, //and so on...
    }
    
    public List<ChemicalElement> separateElements(String input) {
        String[] inputParts = input.split("(?=[A-Z]{1,1}[a-z]{0,2})");
    
        List<ChemicalElement> elementList = new LinkedList<ChemicalElement>();
        for (int i = 1; i < inputParts.length; i++) {
            String inputPart = inputParts[i];
    
            // note: throws IllegalArgumentException for unknown elements
            ChemicalElement element = ChemicalElement.valueOf(inputPart);
            if (null != element) {
                elementList.add(element);
            }
    
        }
        return elementList;
    }
    

    So a test input like this:

    String input = "FeZnKPbUmmK";
    List<ChemicalElement> elements = this.separateElements(input);
    

    will give you the following list:

    [Fe, Zn, K, Pb, Umm, K]

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

Sidebar

Related Questions

This should be a simple issue but I can't figure this out. I have
I have a simple issue, but the solution appears to be tricky. I want
Yall: I have a simple question, it might be a simple configuration issue, but
I have what I think should be a fairly simple mapping issue, but not
I have a very simple issue, but I am not able to find the
I'm sure this is a simple issue, but I have noticed that when I
This seems like an incredibly simple issue but I have no idea why I'm
I'm using the js cart simpleCart but am having a simple issue.. I have
I have a simple issue but I don't know how to fix it. I
Seems like a simple issue but I'm obviously missing something. I have a ViewBag

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.