3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

標準入力から受け取った文字列を数値に変換する

Last updated at Posted at 2016-01-12

標準入力をデリミタで区切り数値型へ変換する
数値型から文字列型の変換方法、
forと拡張forの構文も記載する

StdInNPrs.java
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class stdInNcng {
    public static void main(String args[] ) throws Exception {
    	final String strDlmt=",";	//定数として区切り文字(デリミタ)を指定する
    	
    	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    	String strLine = br.readLine();
    	
    	//デリミタで文字列を区切る
    	String strSplit[] = strLine.split(strDlmt);
    	
    	//int型配列を宣言する
    	int intStdInLen = strSplit.length;
    	int intSplit[] = new int[intStdInLen];
    	
    	//配列を一つずつ文字列から数値へ変換する
    	for(int i=0;i<intStdInLen;i++){
    		intSplit[i]=Integer.parseInt(strSplit[i]);
    		//Long型に変換する場合は、下記を使う
    		//lngSplit[i]=Long.parseLong(strSplit[i]);
    	}
    	//for構文
    	for (int i=0;i<intStdInLen;i++){
    		  System.out.println(intSplit[i]);
    	}
    	
    	
    	//配列を一つずつ数値から文字列へ変換する
    	for(int i=0;i<intStdInLen;i++){
    		strSplit[i] = String.valueOf(intSplit[i]);
    	}
    	//拡張for構文
    	for (String strData: strSplit){
    		  System.out.println(strData);
    	}
    	
    }
}
3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?