2015年10月2日 星期五

Scanner 切割字串的應用


import java.util.Scanner;

public class ScannerDemo {
    public static void main(String[] args){
        StringBuilder sb = new StringBuilder(64);
        String test = "1.1, 2.2, 3.3";
        float fsum = 0.0f;
        
        Scanner s = new Scanner(test).useDelimiter(", ");
        while(s.hasNextFloat()){
            float f = s.nextFloat();
            fsum += f;
            sb.append(f).append(" ");
        }
        System.out.println("Values for: " + sb.toString());
        System.out.println("FSum: " + fsum);
    }
}