「Java」Integer.parseIntで文字列を数値に変換するサンプル

書式
Integer.parseInt(String型文字列);
使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.arkgame.info;
public class InterNumberExceptioDemo {
private static String target[] = { "98", "55", "44", "2", "6", "5" };
public static void main(String[] args) {
int sum = 0;
int resStr = 0;
for (int i = 0; i < target.length; i++) {
//String型をint型に変換する
resStr = Integer.parseInt(target[i]);
sum += resStr;
}
System.out.println("計算結果: " + sum);
}
}
package com.arkgame.info; public class InterNumberExceptioDemo { private static String target[] = { "98", "55", "44", "2", "6", "5" }; public static void main(String[] args) { int sum = 0; int resStr = 0; for (int i = 0; i < target.length; i++) { //String型をint型に変換する resStr = Integer.parseInt(target[i]); sum += resStr; } System.out.println("計算結果: " + sum); } }
package com.arkgame.info;

public class InterNumberExceptioDemo {

      private static String target[] = { "98", "55", "44", "2", "6", "5" };

      public static void main(String[] args) {

            int sum = 0;
            int resStr = 0;

            for (int i = 0; i < target.length; i++) {
                  //String型をint型に変換する
                  resStr = Integer.parseInt(target[i]);
                  sum += resStr;
            }
            System.out.println("計算結果: " + sum);
      }
}

計算結果: 210

Java

Posted by arkgame