将输入的数字字符串转化为int类型

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class StringToInt {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		try {
			String string = br.readLine();
			char[] charStr = string.toCharArray();
			int[] intStr = new int[string.length()];
			for(int i = 0 ; i < charStr.length ; i++){
				intStr[i] = Integer.valueOf(charStr[i] - 48);//数字的ASCII码比实际大48
			}
			int k = 0;
			for(int i = 0 ; i < intStr.length ; i++){
				k += intStr[i]*getPower(intStr.length - 1 - i);
			}
			System.out.println(k);
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	/*
	 * 计算10的幂
	 */
	public static int getPower(int n){
		int result = 1;
		for(int i = 0 ; i < n ; i++){
			result *= 10;
		}
		return result;
	}

}

你可能感兴趣的:(java)