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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:44:07+00:00 2026-06-17T20:44:07+00:00

I have been building a simple formula calculator and have gotten stuck with addition

  • 0

I have been building a simple formula calculator and have gotten stuck with addition and subtraction. As you should know, when calculating an equation, you follow the arithmetic rules of precedence, i.e. brackets, order: power functions, division, multiplication, addition and subtraction. The problem is that addition and subtraction are given equal priority, so therefore you can read it from left to right. Here is my code so far:

{
ArrayList<String> equation = java.util.Arrays.asList({"2","-","2","+","5"});

  while(equation.contains("+")){
          addMe(equation);
  }
  while(equation.contains("-")){
          minusMe(equation);            
  }
}

public static void addMe(ArrayList<String> numberList){
 for (int i = 0, n = numberList.size(); i < n; i++) { 
   String value = (String) numberList.get(i); 
   if(value.equals("+")){

    String wordBefore = (String) numberList.get(i-1);
    String wordAfter = (String) numberList.get(i+1);
    System.out.println("This is the word before " + wordBefore);
    System.out.println("This is the word after " + wordAfter);
    double doubleFromBefore = Double.parseDouble(wordBefore);
    double doubleFromAfter = Double.parseDouble(wordAfter);
    double answer = doubleFromBefore + doubleFromAfter;
    System.out.println("This is the answer: " + answer);
    String stringAnswer = String.valueOf(answer);
    String newNum2 = value.replace(value, stringAnswer);
    numberList.set(i,newNum2);
    numberList.remove(i-1);
    numberList.remove(i);
    break;
  }
 }   
}

The minusMe method is exactly the same as the addMe method except with “-” in relevant places. The problem I am having is getting the equation read from left to right one item at a time and either doing the add or subtract method. Ideally I think I need to combine my 2 while loops with an iterator, to solve the problem but my attempts haven’t worked. Any idea as to if this will solve my problem? If so please provide amended loop.

Regards

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

    Have a look at this

    java.uti.ArrayList<String> equation = java.util.Arrays.asList({"2","-","2","+","5"});
    java.util.Iterator<String> equIterator = equation.iterator();
    int result = 0;
    int multiplier = 1;
    while(equIterator.hasNext()){
         String operandOrOperator = equIterator.next();
         if(operandOrOperator.equals("+")){
              multiplier=1;
         }else if(operandOrOperator.equals("-")){
              multiplier=-1;
         }else if(operandOrOperator.equals("*")){
              result*=Integer.parseInt(equIterator.next()); // Assuming that next element will be there always after operator.
         }else{
              result+=(multiplier * Integer.parseInt(operandOrOperator));
         }
    }
    System.out.println("Final result : " + result);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am fairly new to Flotr2 and I have just been building some simple
I have been wrestling with what should be a very simple thing for weeks
I have a fairly simple board game i've been building in C++. Currently, players
I have been tasked with building a simple web based system for managing a
I have been building a new application using my current understanding of domain driven
I have been building a game for a while (nearly done) - But the
So i have been building an AR engine that makes use of the magnetic
I have been investigating building web parts for sharepoint 2010 and currently have a
I have been involved in building a custum QGIS application in which live data
I have been working on building my first CSS site using divs and am

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.