stringtonumber.java

来自「Java 入门书的源码」· Java 代码 · 共 22 行

JAVA
22
字号
//Copyright (c) 1998, Arthur Gittleman
//This example is provided WITHOUT ANY WARRANTY either expressed or implied.

/* Illustrates wrapper classes used to convert
 * a string to an int or a double, and the number format
 * exception when the string has an invalid format.
 */

public class StringToNumber {
  public static void main(String [] args) {
    try {
      int i = new Integer("435").intValue();
      System.out.println("i = " + i);
      int j = new Integer("45.2").intValue();
      System.out.println("j = " + j);
    }catch(NumberFormatException e) {
      e.printStackTrace();
    }
    double d = new Double("3.14").doubleValue();
    System.out.println("d = " + d); 
  }
}   

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?