java-TransDemo

字符串转换成其他基本类型,以及包装类的自动装箱、拆箱

 1 package com.example;
 2 import java.util.Scanner;
 3 import java.lang.*;
 4 /**
 5  * GuessDemo.java Description:判断输入的字符串是整数?小数?
 6  * 
 7  * @author raizoo
 8  * Created on 17-7-18 下午10:23
 9  * @version 1.0
10  * @since JDK8.0
11  * 
12  * @thows Exception: 无
13  */
14 public class GuessDemo {
15     public static void main(String[] args){
16         //输入字符串
17         Scanner scan = new Scanner(System.in);
18         System.out.print("输入字符串:");
19         String target = scan.nextLine();  //输入字符串
20         System.out.println();
21 
22         /*
23          * parseInt/parseDouble Description: 包装类-装箱/拆箱
24          * 其中,Integer.parseInt(String source)    解析字符串,转换为int型
25          *     Double.parseDouble(String source)  解析字符串,转换为double型
26          * @param String source
27          * @return 转换成的对应类型,如例中int/double
28          * @thows Exception: 无
29          */
30         if(target.matches("\d+")){  //判断输入字符串是整数?
31             int integ = Integer.parseInt(target);
32             System.out.println("是整数:"+integ);
33         }else if(target.matches("\d+\.\d+")){  //判断输入字符串是小数
34             double doub = Double.parseDouble(target);
35             System.out.println("是小数:"+doub);
36         }else{
37             System.out.println("不是数字");
38         }
39     }
40 }
原文地址:https://www.cnblogs.com/DeRozan/p/7203444.html