Can I multiply charAt in Java?

后端 未结 2 1060
渐次进展
渐次进展 2021-01-28 15:29

When I try to multiply charAt I received \"big\" number:

String s = \"25999993654\";
System.out.println(s.charAt(0)+s.charAt(1));

Result : 103<

2条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-28 16:11

    Yes - you should parse extracted digit or use ASCII chart feature and substract 48:

    public final class Test {
        public static void main(String[] a) {
            String s = "25999993654";
            System.out.println(intAt(s, 0) + intAt(s, 1));
        }
    
        public static int intAt(String s, int index) {
            return Integer.parseInt(""+s.charAt(index));
            //or
            //return (int) s.charAt(index) - 48;
        }
    }
    

提交回复
热议问题