Java Integer.parseIntメソッドで文字列から数値への変換サンプル

環境
JavaSE 1.8
Eclipse 4.14.0

構文
public static int parseInt(String s)throws NumberFormatException
文字列の引数を符号付き10進数の整数型として構文解析します。
パラメータ:
s – 解析対象のint表現を含むString
戻り値:10進数の引数で表される整数値。

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.arkgame.study;
public class ArkgDemo {
public static void main(String[] args) {
String strA = "2005";
int num = Integer.parseInt(strA);
System.out.println("文字列から数値への変換結果1: " + num);
String strB = "2005";
int num2 = Integer.parseInt(strB);
System.out.println("文字列から数値への変換結果2: " + num);
if (num == num2) {
System.out.print("OK");
}
}
}
package com.arkgame.study; public class ArkgDemo { public static void main(String[] args) { String strA = "2005"; int num = Integer.parseInt(strA); System.out.println("文字列から数値への変換結果1: " + num); String strB = "2005"; int num2 = Integer.parseInt(strB); System.out.println("文字列から数値への変換結果2: " + num); if (num == num2) { System.out.print("OK"); } } }
package com.arkgame.study;

public class ArkgDemo {
      
      public static void main(String[] args) {
            String strA = "2005";
            int num = Integer.parseInt(strA);
            System.out.println("文字列から数値への変換結果1: " + num);

            String strB = "2005";
            int num2 = Integer.parseInt(strB);
            System.out.println("文字列から数値への変換結果2: " + num);
            
            if (num == num2) {
                  System.out.print("OK");
            }

      }
}

実行結果
文字列から数値への変換結果1: 2005
文字列から数値への変換結果2: 2005
OK

Java

Posted by arkgame