自己实现parseInt方法的效果,将字符串形式的数据转成整数
- 要求:
- 字符串只能是数字不能有其他字符
- 最少1位最多10位
- 0不能开头
public class test04 {
public static void main(String[] args) {
//1.定义一个字符串
String str = "123456789";
//2.校验字符串
//习惯:会先把异常数据进行过滤,剩下的就是正常的数据
if(!str.matches("[1-9]\\d{0,9}")){
//错误的数据
System.out.println("数据格式不正确");
}else{
//正确的数据
System.out.println("数据格式正确");
//3.定义一个变量展示最终的结果
int number = 0;
//4. 遍历字符串得到里面的每一个字符
//1 2 3 4 5..
for (int i = 0; i < str.length(); i++) {
int c = str.charAt(i) - '0';
//把每一位数字放到number中
number = number * 10 + c;
}
System.out.println(number);
}
}
}