1.String转出int型,判断能不能转?如何转?
答:可以转,但需判断NumberFormatException异常
public static void main(String[] args) { try{ Scanner sc = new Scanner(System.in); String s1 = sc.nextLine(); int number1 = Integer.parseInt(s1); Long number2 = Long.parseLong("123"); System.out.println(number1); System.out.println(number2); } catch(NumberFormatException ex){ //当String转出int型时,如果该字符串的内容含字母或者含空格或者输入超出int上限,则会抛出异常 System.out.println("NumberFormatException"); } }
----1.14