简体   繁体   中英

How do I allow a decimal point only in certain circumstance for my calculator app in android studio?

I am trying to figure out on how to allow only a decimal point for every decimal number.

I have been able to allow only 1 decimal on the output when the user is inputting his equation, but I can't figure out how to allow the user to input a decimal point whenever he wants to have more than 1 decimal number on the screen. At this moment, my code only allows only 1 decimal point on screen.

How do I change this to allow it for every number without adding decimal points in incorrect places?

I am assuming this is a string you are trying to manipulate. Ex "10.5+6*7.0". I will assume you are storing the user input in a StringBuilder.
Every time the user types a character you append it to the StringBuilder. Always keep track of the rightmost "number delimiter" in the string. Delimiter Examples: +-*/%()[]. You know the current number being typed begins after the rightmost delimiter. Check the chars from delimiter to string length to see if there is already a decimal in the number.

import java.util.*;
import javax.script.ScriptEngineManager;
import javax.script.ScriptEngine;
import javax.script.ScriptException;

class Foo {
    int rightmostDelimiterIdx = 0;
    List<Character> delimiters = Arrays.asList('+', '-', '*', '/', '%', '(', ')', '[', ']');
    
    public static void main(String[] args){
        Foo foo = new Foo();
        try {
            foo.run();
        } catch (ScriptException e) {
            e.printStackTrace();
        }
    }
    
    public void run() throws ScriptException
    {
        StringBuilder sb = new StringBuilder(); // store user input
        ScriptEngineManager mgr = new ScriptEngineManager(); // evaluate expression with JavaScript
        ScriptEngine engine = mgr.getEngineByName("JavaScript");
        
        // User types:5.0*7
        charTyped(sb, '5');
        charTyped(sb,'.');
        charTyped(sb,'0');
        charTyped(sb,'*');
        charTyped(sb,'7');
        System.out.println(engine.eval(sb.toString()));
        sb.setLength(0);
        // User types:8+2.3.3
        charTyped(sb, '8');
        charTyped(sb,'+');
        charTyped(sb,'2');
        charTyped(sb,'.');
        charTyped(sb,'3');
        charTyped(sb,'.'); // second decimal will be rejected, thus string will be "8+2.33"
        charTyped(sb,'3');
        System.out.println(engine.eval(sb.toString()));
    }
    
    // appends c to sb if valid operation
    public void charTyped(StringBuilder sb, char c)
    {
        if(delimiters.contains(c)) {
            rightmostDelimiterIdx = sb.length();
        } else if(c == '.' && sb.indexOf(".", rightmostDelimiterIdx) != -1) {
            return; // do not add '.' b/c already exists in number
        }
        
        sb.append(c);
    }
    
    public void charDeleted(StringBuilder sb, char c) {
        // I leave implementation up to you
    }
}

Output:
35.0
10.33

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM