(面接問題)String string=「12,df,45,gf,45.4,er,121」の文字列を与える.その中の数字の和を求めます.

689 ワード

package zking;

public class Admin {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String str="12,df,45,gf,45.4,er,121";//       
		String[] sta=str.split(",");//  ,   ,       
		float sum=0;//               
		for (int i = 0; i < sta.length; i++) {//  sta  
			try {
				sum+=Float.valueOf(sta[i]);//         ,       try    
				System.out.println(Float.valueOf(sta[i]));
			} catch (Exception e) {
				System.out.println("    ,    ");
			}
		}
		System.out.println("___________________________________");
		System.out.println("  :"+sum);//    sum  
		
	}

}