Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Calculator double zero button issue.

  1. #1
    Junior Member
    Join Date
    Apr 2021
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Calculator double zero button issue.

    hi members I am beginner in android development Here some issue in calculator with my Double zero button for example when i press btn 1 for one and btn 00 for double zero and again press btn 1 for one so my result should be 1001. but my calculator write 1010. it write the last 1 between the two zero. Please anyone help me.

    my code is below

    public void btndoublezero(View view) {
    updateText("00");
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Calculator double zero button issue.

    Can you post the code that is the problem?
    Be sure to wrap in code tags.

    If "00" is converted to a numeric value, it will print as 0.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    mtahirkn (April 26th, 2021)

  4. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Calculator double zero button issue.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.

    How are you trying to debug the code? To see what happens add some print statements that print out the values in the variables. The print out will be in the logcat.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #4
    Junior Member
    Join Date
    Apr 2021
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Calculator double zero button issue.

     
    package com.example.englishcalculator;
     
    import androidx.annotation.RequiresApi;
    import androidx.appcompat.app.AppCompatActivity;
    import org.mariuszgromada.math.mxparser.*;
    import android.os.Build;
    import android.os.Bundle;
    import android.text.SpannableStringBuilder;
    import android.view.Display;
    import android.view.View;
    import android.widget.EditText;
    import android.widget.ImageView;
     
    public class MainActivity extends AppCompatActivity {
     
        private EditText display;
     
     
        @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
     
            display = findViewById(R.id.input);
            display.setShowSoftInputOnFocus(false);
            display.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                 if (getString(R.string.display).equals(display.getText().toString())){
                     display.setText("");
                 }
                }
            });
        }
        private void updateText(String strToAdd){
            String OldStr = display.getText().toString();
            int cursorPos = display.getSelectionStart();
            String leftStr = OldStr.substring(0, cursorPos);
            String rightStr = OldStr.substring(cursorPos);
            if (getString(R.string.display).equals(display.getText().toString())){
                display.setText(strToAdd);
                display.setSelection(cursorPos + 1);
            }
            else {display.setText(String.format("%s%s%s", leftStr, strToAdd, rightStr));
                display.setSelection(cursorPos + 1);
     
            }
        }
     
        public void btnzero(View view) {
            updateText("0");
        }
     
        public void btnone(View view) {
            updateText("1");
     
        }
     
        public void btntwo(View view)
        { updateText("2");
        }
     
        public void btnthree(View view) {
            updateText("3");
        }
     
        public void btnfour(View view) {
            updateText("4");
     
        }
     
        public void btnfive(View view) {
            updateText("5");
        }
     
        public void btnsix(View view) {
            updateText("6");
        }
     
        public void btnseven(View view) {
            updateText("7");
        }
     
        public void btneight(View view) {
            updateText("8");
        }
     
        public void btnnine(View view) {
            updateText("9");
        }
     
     
        public void btnclear(View view){
            display.setText("0");
            }
     
     
        public void btnparenteses(View view) {
            int cursorPos = display.getSelectionStart();
            int openPar = 0;
            int closedPar = 0;
            int textLen = display.getText().length();
            for (int i = 0; i < cursorPos; i++){
            if ( display.getText().toString().substring(i, i+1) .equals("(")){
                openPar += 1;
            }
                if ( display.getText().toString().substring(i, i+1) .equals(")")){
                    closedPar += 1;
                }
            }
                if (openPar == closedPar || display.getText().toString().substring(textLen-1, textLen).equals("(")){
                     updateText("(");
     
                }
               else if (closedPar < openPar && !display.getText().toString().substring(textLen-1, textLen).equals(")")){
                updateText(")");
     
            }
            display.setSelection(cursorPos + 1);
        }
     
        public void btndevide(View view) {
            updateText("÷");
        }
     
        public void btnmultiply(View view) {
            updateText("×");
        }
     
        public void btnsubtract(View view) {
            updateText("-");
        }
     
        public void btnadd(View view) {
            updateText("+");
        }
     
        public void btndoublezero(View view) {
            updateText("00");
        }
     
        public void btnpoint(View view) {
            updateText(".");
        }
     
        public void btnexponent(View view) {
            updateText("^");
        }
     
     
        public void btnequal(View view) {
        String userExp = display.getText().toString();
     
        userExp = userExp.replaceAll("÷", "/");
        userExp = userExp.replaceAll("×", "*");
        Expression exp = new Expression(userExp);
        String result = String.valueOf(exp.calculate());
        display.setText(result);
        display.setSelection(result.length());
     
        }
        public void btnbackspace(View view) {
     
            int cursorPos = display.getSelectionStart();
            int textLen = display.getText().length();
            if (cursorPos != 0 && textLen !=0) {
                SpannableStringBuilder selection = (SpannableStringBuilder) display.getText();
                selection.replace(cursorPos -1, cursorPos, "" );
                display.setText(selection);
                display.setSelection(cursorPos - 1);
            }
        }
    }

  6. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Calculator double zero button issue.

    I don't see any print statements in the updateText method for debugging.
    You need to debug the code to see why it is doing what it is doing. Add some print statements to show the values of all the variables used in the updateText method. The print out in the logcat will show you what the code is doing when it executes.

    Please add some comments to the code in the updateText method that explain what it is trying to do.
    For example:
    what is leftStr and rightStr about?
    What is the use of the cursorPos variable?
    Why is cursorPos updated by 1?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #6
    Junior Member
    Join Date
    Apr 2021
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Calculator double zero button issue.

    Now I solve the issue. here is the code I add.

    public void btndoublezero(View view) {
            updateText("00");
            EditText editText = (EditText)findViewById(R.id.input);
            editText.setSelection(editText.getText().length());
        }

  8. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Calculator double zero button issue.

    The new code considers that double zero has 2 characters and all the others only 1 character.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. GUI calculator issue
    By pricklygoo33 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 27th, 2013, 12:07 AM
  2. Jar Embedded in Website: Double Buffering Issue?
    By Staticity in forum AWT / Java Swing
    Replies: 6
    Last Post: July 17th, 2012, 02:43 PM
  3. Double always == 0! And a Javadoc issue! (Using NetBeans)
    By CameronFaust in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 11th, 2011, 12:22 AM
  4. GUI Calculator: Adding Answer Button? Please Help
    By Staticity in forum What's Wrong With My Code?
    Replies: 29
    Last Post: July 27th, 2011, 03:18 PM
  5. need to add a Double Button to the code
    By jwb4291 in forum What's Wrong With My Code?
    Replies: 15
    Last Post: August 5th, 2010, 11:19 AM