简体   繁体   English

包含不适用于(“ \\\\ +”)的方法

[英]contains method not working for (“\\+”)

I`m trying to write a Calculator app for android. 我正在尝试为Android编写计算器应用。 for 4 main operators (-*/+) I used this method and for 3 of them it work without out any problems but for + operator it cannot recognize the + sign in the string i through at it. 对于4个主要运算符(-* / +),我使用了此方法;对于其中3个运算符,它没有任何问题,但是对于+运算符,它无法识别字符串i中的+号。 he is my code for / and + operators : 他是我的/和+运算符的代码:

case R.id.buttonEql:
            current=show.getText().toString();
           if(current.contains("/"))
           { current+=input.getText().toString();
               show.setText(current);
              parts  = current.split("/");
           firstNumber = Integer.parseInt(parts[0]);
           secondNumber = Integer.parseInt(parts[1]);
              operationResult = firstNumber/secondNumber;
               show.setText("");
               show.setText(String.valueOf(operationResult));
               input.setText("0");
           }
           else if (current.contains("\\+"))
           {current=show.getText().toString();
               current+=input.getText().toString();
               show.setText(current);
               parts  = current.split("\\+");
               firstNumber = Integer.parseInt(parts[0]);
               secondNumber = Integer.parseInt(parts[1]);
               operationResult = firstNumber+secondNumber;
               show.setText("");
               show.setText(String.valueOf(operationResult));
               input.setText("0");}

In else if condition, what about doing just current.contains("+"). 在其他情况下,仅执行current.contains("+").

you are passing a regular expression (escaping it) which you should not for String contains function. 您正在传递一个正则表达式(转义它),您不应该为String包含函数。 Check on Java API Doc for String 检查Java API文档中的字符串

Please, use contains() without escaping and split() with escaping 请使用不带转义的contains()和带转义的split()

current.contains("+")

current.split("\\+")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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