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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:46:26+00:00 2026-06-13T11:46:26+00:00

i have no idea why i am not getting the expected results! I have

  • 0

i have no idea why i am not getting the expected results! I have written the expected result in the end. Alright, i have two classes: CalculatorEngine and CalculatorInput, the former calculates while the latter gives a line mode interface. The code for CalculatorEngine goes like this, nothing fancy:

public class CalculatorEngine {
    int value;
    int keep;
    int toDo;

    void binaryOperation(char op){
        keep = value;
        value = 0;
        toDo = op;
    }

    void add() {binaryOperation('+');}
    void subtract() {binaryOperation('-');}
    void multiply() {binaryOperation('*');}
    void divide() {binaryOperation('/');}

    void compute() {
        if (toDo == '+')
            value = keep + value;
        else if (toDo == '-')
            value = keep - value;
        else if (toDo == '*')
            value = keep * value;
        else if (toDo == '/')
            value = keep/value;
        keep = 0;
    }

    void clear(){
        value = 0;
        keep = 0;
    }

    void digit(int x){
        value = value*10 + x;
    }

    int display(){
        return (value);
    }

    CalculatorEngine() {clear();} //CONSTRUCTOR METHOD!

}

And the code for CalculatorInput goes like this:

import java.io.*;

public class CalculatorInput {
    BufferedReader stream;
    CalculatorEngine engine;

    CalculatorInput(CalculatorEngine e) {
        InputStreamReader input = new InputStreamReader(System.in);
        stream = new BufferedReader(input);
        engine = e;
    }

    void run() throws Exception {
        for (;;) {
            System.out.print("[" +engine.display()+ "]");
            String m = stream.readLine();
            if (m==null) break;
            if (m.length() > 0) {
                char c = m.charAt(0);
                if (c == '+') engine.add();
                else if (c == '*') engine.multiply();
                else if (c == '/') engine.divide();
                else if (c == '-') engine.subtract();
                else if (c == '=') engine.compute();
                else if (c == '0' && c <= '9') engine.digit(c - '0');
                else if (c == 'c' || c == 'C') engine.clear();
            }
        }
    }

    public static void main(String arg[]) throws Exception{
        CalculatorEngine e = new CalculatorEngine();
        CalculatorInput x = new CalculatorInput(e);
        x.run();
    }
}

I expect the answer to be like this:

[0]1
[1]3
[13]+
[0]1
[1]1
[11]=
[24]

BUT i am getting this:

[0]1
[0]3
[0]+
[0]1
[0]1
[0]+
[0]

Seems like digit function isn’t working properly. Help!

  • 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-13T11:46:27+00:00Added an answer on June 13, 2026 at 11:46 am

    Change this:

    if (c == '0' && c <= '9')
    

    To this:

    if (c >= '0' && c <= '9')
    

    Otherwise, it will only be true when c is '0'.

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

Sidebar

Related Questions

we've been getting this error lately and have no idea why, we are not
I'm getting two problems in my FooterSelectorView.h and I have no idea why. One
I get the following Nhiberante error, but have no idea why: could not execute
I have no idea why but the contentherewrap div does not show fully when
We have developed a set of crystal reports (not my idea) that are to
I have some basic idea on how to do this task, but I'm not
I've not read the Modern C++ Design book but have found the idea of
Any idea why the following code is not working mysql credentials are correct, have
So my UIPickerView does not get connected to the DataSource. I Have no idea
I'm getting the following error in my Rails application and I have no idea

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.