Note No. 6 "Calculate the formula for the one-digit sum difference received as a character string" [Java]

Introduction

Recorded because I used the getNumericValue method for the first time.

What you want to do

-Receive the calculation formula with a character string of any length (all numbers used are 1 digit, operators are only'+' and'-') ・ Calculate the received character string as a numerical value ・ Output the calculation result

(Example) Input: "1 + 2 + 3" Output: 6

code

        Scanner sc = new Scanner(System.in);
        String line = sc.nextLine();
        sc.close();
        char [] c = line.toCharArray();

        int sum = 0;
        boolean add = true;

        for(int i = 0; i < line.length(); i++) {
            if(i % 2 == 0) {//Even numbers are calculated because numbers and operators alternate.,Odd numbers swap operators
                if(add) {
                    int n = c[i];
                    sum += n;
                } else {
                    int n = c[i];
                    sum -= n;
                }
            } else {
                if(c[i] == '+') {
                    add = true;
                } else {
                    add = false;
                }
            }
        }
        System.out.println(sum);

Input: "1 + 2 + 3" Output: 150

Problems with code 1

-When the numerical value put in the char type array is converted to int type, it is assigned by ASCII character code. → I want to take out the numbers in the array as they look!

Improved code

                if(add) {
                    int n = Character.getNumericValue(c[i]);
                    sum += n;
                } else {
                    int n = Character.getNumericValue(c[i]);
                    sum -= n;
                }

Input: "1 + 2 + 3" Output: 6

Summary

If you want to use the char type number as it is, use the Character type getNumericValue ().

Postscript

Before omission
                if(c[i] == '+') {
                    add = true;
                } else {
                    add = false;
                }

In the above part, the comparison operator returns a boolean type, so you can determine the truth by assigning it directly to add.

After omission

                add = c[i] == '+';

Recommended Posts

Note No. 6 "Calculate the formula for the one-digit sum difference received as a character string" [Java]
A note for Initializing Fields in the Java tutorial
Memorandum No.4 "Get a character string and decorate it" [Java]
[Ruby] Difference between symbol variables and character string variables. About the difference between [: a] and ['a'].
Read the file under the classpath as a character string with spring
How to check for the contents of a java fixed-length string
Invoke the character string passed as an argument as a method with send
Memorandum No. 3 "Replace the entered character string for each character depending on the conditions"
A review note for the class java.util.Scanner
A review note for the class java.util.Optional
A review note for the class java.util.Objects
A review note for the package java.time.temporal
[Java] Difference between equals and == in a character string that is a reference type
[Java] Note about the difference between equivalence judgment and equality judgment when comparing String classes
[Java] Cut out a part of the character string with Matcher and regular expression