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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:52:54+00:00 2026-06-12T23:52:54+00:00

This is the code I made: public static boolean isOperator(char op){ if (op ==

  • 0

This is the code I made:

public static boolean isOperator(char op){
    if (op == '+' || op == '-'
            || op == '*' || op == '/'
            || op == '^'
            || op == '(' || op == ')'){
        return true;
    }
    return false;
}

public static boolean isOperand(char op){
    String numbers = "0123456789.";
    int a = numbers.indexOf(op);
    return a >= 0;
}
public static void main(String []args){        
    String exp= "15+20+(3.84*25)*(78/3.8)";
    LinkedList a = new LinkedList();

    for (int i = 0; i < exp.length(); i++){
        if (isOperator(exp.charAt(i))){
            a.add(exp.charAt(i));
        } else if (isOperand(exp.charAt(i))){
            int k = i;
            while (k < exp.length()){//I see the length of the number
                if (isOperand(exp.charAt(k))){
                    k++;
                } else {
                    break;
                }
            }
            if (k != exp.length()-1){//if it's not ad the end
                a.add(exp.substring(i, k));
            } else {//if it's at the end I take all the remaining chars of the string
                a.add(exp.substring(i));
            }
            i = k-1;//i must go back since the subtring second parameter is exclusive
        } 
    }
    System.out.println(a);    
}//main

This is the output:

[15, +, 20, +, (, 3.84, *, 25, ), *, (, 78, /, 3.8), )]

Which is exactly what I wanted. As you can see I put operand and operators separately into a list maintaining the order of the string.
There is a way to do it in a simpler way?

  • 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-12T23:52:55+00:00Added an answer on June 12, 2026 at 11:52 pm

    Is there a way to do it in a simpler way?

    Yes, there is. Use regular expressions. Take a look at the following code and
    try to run it for your inputs.

    public static void main(String[] args) 
    {
        String exp = "15+20+(3.84*25)*(78/3.8)";
        String regex = "(\\d+\\.\\d+)|(\\d+)|([+-/*///^])|([/(/)])";
    
        Matcher m = Pattern.compile(regex).matcher(exp);
    
        LinkedList list = new LinkedList();
    
        while (m.find()) {
            list.add(m.group());
        }
    
        System.out.println(list);
    }
    

    Explanation of my regex used above:

    “(\d+\.\d+)|(\d+)|([+-/*///^])|([/(/)])”

    (double) or (integer) or (arithmetic operator) or (left/right paranthesis)

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

Sidebar

Related Questions

i have this code to create a local windows user public static bool CreateLocalWindowsAccount(string
Given this code: public class MainActivity extends FragmentActivity implements ActionBar.TabListener { public static final
I have made this code: var foo=document.createElement(div); var childs=foo.getElementsByTagName(*); console.log(childs.length);//0 OK var a=document.createElement(a); foo.appendChild(a);
I have made this code to open a database(created in SQLite browser) stored in
I am pretty new at C# and .NET, but I've made this code to
How can I improve this code? What has made this long winded is the
So here's what I see this code doing: An array is made A loop
i made a windows service & add project installer.in which only contain this code.
I have a line, which is a sprite made by using this code CGPoint
I have a problem, and I made this test code to show you my

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.