« MSNアカウントを削除する | メイン | [CSS] preタグ codeタグ ~ ソースコードでも折り返して表示 »

2006年03月01日

[Java]Integer.parseInt は全角文字でも適用可能

何年も前にも試した気がするが、もう一度。
全角数字でも Integer.parseInt できる。


public class ParseIntTest{
  public static void main(String[] args){
    for(int i=0; i<args.length; i++){
      try{
        System.out.println("Check:" + args[i]);
        int pi = Integer.parseInt(args[i]);
        System.out.println("OK:" + pi);
      }catch(Exception e){
        e.printStackTrace();
      }
    }
  }
}

C:\>java ParseIntTest 123 123 ①②③ ⅠⅡⅢ abc
Check:123
OK:123
Check:123
OK:123
Check:①②③
java.lang.NumberFormatException: For input string: "①②③"
Check:ⅠⅡⅢ
java.lang.NumberFormatException: For input string: "ⅠⅡⅢ"
Check:abc
java.lang.NumberFormatException: For input string: "abc"

丸数字やローマ数字は数値へ変換できない。また、文字列内に空白があっても NumberFormatException が発生する。

投稿者 NI-Lab. Twitter: @nilab : 2006年03月01日 19:31

コメント

すごい!

投稿者 みつもり : 2006年08月10日 18:40

Please post your comments -> Ido-Batarian BBS
コメントはこちらの Ido-Batarian BBS へどうぞ。

« MSNアカウントを削除する | メイン | [CSS] preタグ codeタグ ~ ソースコードでも折り返して表示 »

人気ブログランキング - NI-Lab.'s ヅラッシュドット [Valid RSS]