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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T12:05:01+00:00 2026-06-03T12:05:01+00:00

This is a calculator for simple arithmetic operations where only + and – is

  • 0

This is a calculator for simple arithmetic operations where only + and – is used. The logic part is pretty easy, but I am embarrassed to say the code is not working. The input is with unknown digits. eg : “4/2” or “42+34” should both should be acceptable. I am not so comfortable with strings and their functions, so please help out. Thank you! 🙂

/*
 *Calculator with only + and - 
 */
import java.util.Scanner;

class Calculator {
    public static String strng;

    public static void main(String[]args){
        Calculator calc = new Calculator();
        calc.Calculator();
    }

    public void Calculator()
    {
        int op1=0,op2=0,flag=0,index=0;
        String Operator=null;
        System.out.println("java calculator");
        Scanner sc=new Scanner(System.in);

        strng= sc.nextLine();

        if(strng.indexOf("+")>0) {
            Operator = "+";
            index=strng.indexOf("+");
            if(strng.lastIndexOf("+")!=index){
              Operator="++";
              flag=1;
            }
        }
        else if(strng.indexOf("-")>0) {
            Operator="-";
            index=strng.indexOf("-");
            if(strng.lastIndexOf("-")!=index){
              Operator="--";
              flag=1;
            }
        } else if(strng.indexOf("*")>0) {
            Operator="*";
            index=strng.indexOf("*");
        } else if(strng.indexOf("/")>0) {
            Operator="/";
            index=strng.indexOf("/");
        } 

        if((index>0)&&(flag==0)) {
           op1=Integer.parseInt(strng.substring(0,index-1));
           op2=Integer.parseInt(strng.substring(index+1));
           Operation(Operator,op1,op2);
        }

        if(flag==1) {
           op1=Integer.parseInt(strng.substring(0,index-1));
           op2=Integer.parseInt(strng.substring(index+2)); 
           Operation(Operator,op1,op2);
        } else { //to separate the operator and operands string functions.
           System.out.println("Invalid choice");
        }
     }  


     public void Operation(String Operator, int operand1, int operand2) {
        if(Operator.equals("+")){ Add(operand1,operand2); }
        if(Operator.equals("-")){ Subtract(operand1,operand2); }
        if(Operator.equals("*")){ Multiply(operand1,operand2); }
        if(Operator.equals("/")){ Divide(operand1,operand2); }
        if(Operator.equals("++")){ Increment(operand1); }
        if(Operator.equals("--")){
            Decrement(operand1);
        } else {
            System.out.println("Invalid entry");
        }
    } 


    void Add(int op1, int op2) {
        System.out.println(strng+"="+(op1+op2));
    }

    void Subtract(int op1, int op2) {
        System.out.println(strng+"="+(op1-op2));
    }  

    void Increment(int op1) {
        System.out.println(strng+"="+(op1+1));
    }

    void Decrement(int op1) {
        System.out.println(strng+"="+(op1-1));
    }

    void Multiply(int op1, int op2) {
        int ans=0;
        int  x = op1<op2?op1:op2;
        int y = op1>op2?op1:op2;
        for(int i=1;i<=x;i++){
            ans=ans+y;
        } 
        System.out.println(strng+"="+ans);
    }

    void Divide(int op1, int op2) {
        int count=0,rem=op1;

        while(rem>=op2){
            rem=rem-op2;
            count=count+1;
        } 
        System.out.println(strng+"="+count); //return rem for modulo operand
    }

}
  • 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-03T12:05:03+00:00Added an answer on June 3, 2026 at 12:05 pm

    I’m not comfortable with the way you’re looking for the symbols in the string. I would advise that you look into String.split(String regex), which will allow you to split your symbols and your numerals.

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

Sidebar

Related Questions

I have made a calculator for simple operations but I cant figure out how
Problably an easy questions but this calculation bothers me.. why is double test =
I am trying to build a simple calculator to learn javascript/jquery but seem to
Let's say I would like to create a simple calculator. It consists of 3
When adding this code to a simple calculator program I receive the error message
This should be simple, but I'm at a complete loss. I'm working through a
In this program I am trying to create a simple calculator. However, I can't
I am working on getting a simple calculator working as part of my adventure
I built a very simple addition calculator in python: #This program will add two
Well I am developing a simple Tip Calculator app as a part of a

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.