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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:53:30+00:00 2026-05-23T19:53:30+00:00

I am trying to calculate an arithmetic expression, which is entered as a string

  • 0

I am trying to calculate an arithmetic expression, which is entered as a string (for example, ( 5+4*5-1/8 ), which will give the result 3). I enter an expression and convert it into an array. First; the result will start with the first element and it will change in the loop. But the problem is operator precedence. How can I use the operator presedence in a loop? Here is my code:

import java.util.Scanner;


public class HesapMakinesi {

    private char value[];

    private int count;

    private Scanner str = new Scanner(System.in);

    private String process;

    HesapMakinesi() {

        System.out.print("Enter the process ");

        process = str.next();

        //System.out.println(islem);

        Initializer(process);

    }

    private void Initializer(String process) {

        count = process.toCharArray().length;

        value = new char [count];

        int i;

        System.arraycopy(process.toCharArray(), 0, value, 0, count);

        //System.out.println(value);

        if(value[0]=='-' || value[0]=='+' || value[0]=='/' || value[0]=='*' ||  // A process cannot start with an operator
                value[count-1]=='-' || value[count-1]=='+' || value[count-1]=='/' || value[count-1]=='*') {

            System.out.println("You have entered a wrong process.Please enter again!!!");

            System.out.print("Enter the process: ");

            process = str.next();

            Initializer(process);

        }



        for(i=0; i<count; i++) { // A process cannot include a character except operators

            if( value[i]!='+' && value[i]!='-' && value[i]!='*' && value[i]!='/' && value[i]!='(' && value[i]!=')' && !Character.isDigit(value[i]) ) {

                System.out.println("You have entered a wrong process.Please enter again!!!");

                System.out.print("Enter the process: ");

                process = str.next();

                Initializer(process);

            }


        }


        for(i=0; i<count-1; i++) { // A process cannot have operators sequantially

            if( !Character.isDigit(value[i]) && !Character.isDigit(value[i+1]) ) {

                if( (value[i] == '+' && value[i+1] == '+' ) || (value[i] == '+' && value[i+1] == '-' ) || (value[i] == '+' && value[i+1] == '*' ) || 
                        (value[i] == '+' && value[i+1] == '/' ) ) {

                    System.out.println("You have entered a wrong process.Please enter again!!!");

                    System.out.print("Enter the process: ");

                    process = str.next();

                    Initializer(process);

                }

                else if( (value[i] == '-' && value[i+1] == '+' ) || (value[i] == '-' && value[i+1] == '-' ) || (value[i] == '-' && value[i+1] == '*' ) || 
                        (value[i] == '-' && value[i+1] == '/' ) ) {

                    System.out.println("You have entered a wrong process.Please enter again!!!");

                    System.out.print("Enter the process: ");

                    process = str.next();

                    Initializer(process);

                }

                else if( (value[i] == '*' && value[i+1] == '+' ) || (value[i] == '*' && value[i+1] == '-' ) || (value[i] == '*' && value[i+1] == '*' ) || 
                        (value[i] == '*' && value[i+1] == '/' ) ) {

                    System.out.println("You have entered a wrong process.Please enter again!!!");

                    System.out.print("Enter the process: ");

                    process = str.next();

                    Initializer(process);

                }

                else if( (value[i] == '/' && value[i+1] == '+' ) || (value[i] == '/' && value[i+1] == '-' ) || (value[i] == '/' && value[i+1] == '*' ) || 
                        (value[i] == '/' && value[i+1] == '/' ) ) {

                    System.out.println("You have entered a wrong process.Please enter again!!!");

                    System.out.print("Enter the process: ");

                    process = str.next();

                    Initializer(process);

                }


            }

        }

        //sCount();

    }

    /*private void Count(){

        double result,temp;

        int i;

        for(i=0; i<count; i++) {

            if( value[i]!= )

        }


    }*/

}
  • 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-23T19:53:31+00:00Added an answer on May 23, 2026 at 7:53 pm

    Following on from my comment… If you’re dealing with a simple expression where you can only have numbers and signs +-/* then you can have a simple approach:

    1. split your expression by lowest precedence operators first (+-) remembering the signs.
    2. Compute each piece – as now the precedence isn’t important, since everything is of the same level
    3. sum those computed pieces taking into account the sign of the piece from step 1.

    In your example, you’ll end up with something like this:

    1. (Split by +-) three pieces: (1) 5; (2) 4*5 with sign +; (3) 1/8 with sign –
    2. Compute each of the three pieces: (1) 5; (2) 20; (3) 0.125
    3. Sum the three pieces with their respective signs: 5+20-0.125 = 24.875
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to calculate SHA1 hash of a unicode string using T-SQL. The below
I am trying to calculate difference between two date strings both of which are
I have a the following scenarios. I am trying to calculate throughput of the
I'm trying to create a Crystal Reports formula field (to calculate the percentage change
I'm trying to write a simple game/utility to calculate poker odds. I know there's
i'm trying to use the GeoKit plugin to calculate the distance between 2 points.
I'm trying to make a calculator that will take inputs from users and estimate
I'm trying to center a rotated image into a destination buffer using GDI+. The
Im trying to calculate with of different list items my HTML: <ul> <li style=disply:block;
Im trying to calculate a subtotal and a total from a series of values

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.