简体   繁体   中英

How do I convert a String of digits into separate digits and add them together in Java?

I am kind of new to coding, and I need some help. What I'm trying to do is to make the user input an expression of this type: +(1 2 3). It should return (1+2+3) --> 6. However, for some reason when I do this, it returns "150".

The code is in spanish, however, it's pretty simple to understand. Ignore every other declared variable.

public void evaluar(){
    // TODO Arreglar suma
    Visualizador v = new Visualizador();
    Node n = new Node();
    String criterio = "";
    criterio = v.DarExp("Ingrese la expresion");
    int total = 0;
    int suma = 0;
    int resta = 0;
    int multiplicacion = 1;
    int division = 1;
    if (criterio.charAt(0) == '+'){
        criterio = criterio.replace("(", "");
        criterio = criterio.replace(")", "");
        criterio = criterio.replace("+", "");
        criterio = criterio.replace(" ", "");
        total = Integer.parseInt(criterio);
        System.out.println(total);
        for (int i = 0; i < criterio.length(); i++){
            suma += criterio.charAt(i);
        }
        System.out.println(suma);
    }

try suma += criterio.charAt(1) - '0';
The reason is: '1' , the character, is different from 1 , the integer. Check ASCII table and you will see what I'm talking about.

It is adding the ascii value of the char

consider

   String criterio = "+123";

    if (criterio.charAt(0) == '+'){
        criterio = criterio.replace("(", "");
        criterio = criterio.replace(")", "");
        criterio = criterio.replace("+", "");
        criterio = criterio.replace(" ", "");
        int suma = 0;

        for (int i = 0; i < criterio.length(); i++){
            suma += criterio.charAt(i) - '0';
        }
        System.out.println(suma);
    }

In Java, a char is an integer, which means it has an integral value, and can be converted to int in arithmetic expressions.

  • '1' equals 49
  • '2' equals 50
  • '3' equals 51

And the sume of 49, 50 and 51 is 150!

You can use Character.getNumericValue() to get the correct value for these characters.

suma += Character.getNumericValue(criterio.charAt(i));

Your solution will fail when having a digit greater than 10.

Try split by " " and sum of tokens:

    String criterio = "+(1 2 3)";
    int suma = 0;
    if (criterio.charAt(0) == '+'){
        criterio = criterio.replace("(", "");
        criterio = criterio.replace(")", "");
        criterio = criterio.replace("+", "");
        String[] tokens = criterio.split(" ");
        for (String s : tokens) {
            suma += Integer.parseInt(s);
        }
        System.out.println(suma);
    }

Or better with java 8:

suma = Arrays.stream(tokens).mapToInt(Integer::parseInt).sum();

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