zoukankan      html  css  js  c++  java
  • 算法笔记_028:字符串转换成整数(Java)

    1 问题描述

    输入一个由数字组成的字符串,请把它转换成整数并输出。例如,输入字符串123”,输出整数123

    请写出一个函数实现该功能,不能使用库函数。


    2 解决方案

    解答本问题的基本思路:从左至右扫描字符串中的每个字符,把之前扫描得到的数字乘以10,再加上当前字符表示的数字。

    但是,基本思路是这样,还要注意以下几点:

    (1)最好判断一下输入是否为空。

    (2)如果字符串的第一个字符是-’号,最终得到的整数必为负整数。

    (3)输入的字符串中不能含有不是数字的字符。

    (4)输入的字符串不能太长,否则转换成整数后会导致整数溢出。

    具体实现代码如下:

    package com.liuzhen.string_1;
    
    import java.util.Scanner;
    
    public class StringToInt {
        public static int Max_INT = Integer.MAX_VALUE;
        public static int Min_INT = Integer.MIN_VALUE;
        
        public int getStringToInt(String A){
            char[] arrayA = A.toCharArray();
            int n = 0;
            if(A.equals("") || A.equals(null))     //判断输入是否为空
                return 0;
            int i = 0;
            while(arrayA[i] == ' ')   //处理字符串首位的空格
                i++;
            int sign = 1;   //用于判定输入字符串数字的正负,初始化为1表示为正数
            if(arrayA[i] == '+' || arrayA[i] == '-'){
                if(arrayA[i] == '-')
                    sign = -1;
                i++;
            }
            while(i < arrayA.length && Character.isDigit(arrayA[i])){  //确定是数字0~9才执行循环
                int c = arrayA[i] - '0';
                //当输入字符串表示数为正数,且大于Max_INT
                if(sign > 0 && (n > Max_INT/10 || (n == Max_INT/10 && c > Max_INT%10))){
                    n = Max_INT;
                    break;
                }
                //当输入字符串表示数为负数,且小于Min_INT
                if(sign < 0 && (n + Min_INT/10 > 0 || (n + Min_INT/10 == 0 && c + Min_INT%10 > 0))){
                    n = Min_INT;
                    break;
                }
                //把之前得到的数字乘以10,再加上 当前字符表示的数字
                n = n*10 + c;
                i++;
            }
            
            return sign > 0 ? n : -n;
        }
        public static void main(String[] args){
            StringToInt test = new StringToInt();
            Scanner in = new Scanner(System.in);
            System.out.println("请输入一个由数字组成的字符串:");
            String A = in.nextLine();
            int result = test.getStringToInt(A);
            System.out.println("整数result = "+result);
        }
    }

    运行结果:

    请输入一个由数字组成的字符串:
      -1000
    整数result = -1000
    
    
    请输入一个由数字组成的字符串:
    +100000
    整数result = 100000
    
    
    请输入一个由数字组成的字符串:
    a1212
    整数result = 0
    
    
    请输入一个由数字组成的字符串:
    1000000000000
    整数result = 2147483647
    
    
    请输入一个由数字组成的字符串:
    -10000000000
    整数result = -2147483648
  • 相关阅读:
    Get distinct count of rows in the DataSet
    单引号双引号的html转义符
    PETS Public English Test System
    Code 39 basics (39条形码原理)
    Index was outside the bounds of the array ,LocalReport.Render
    Thread was being aborted Errors
    Reportviewer Error: ASP.NET session has expired
    ReportDataSource 值不在预期的范围内
    .NET/FCL 2.0在Serialization方面的增强
    Perl像C一样强大,像awk、sed等脚本描述语言一样方便。
  • 原文地址:https://www.cnblogs.com/liuzhen1995/p/6399531.html
Copyright © 2011-2022 走看看