首页 > 其他 > 详细

简单利用Scanner对文件进行解析

时间:2016-11-30 03:27:40      阅读:120      评论:0      收藏:0      [点我收藏+]

public class AvPrice
{
    static int count = 0;
    static int sum = 0;
    public static void main(String[] args)
    {
        try
        {

    //定义并实例化Scanner对象
            Scanner in = new Scanner(new File("d:/abc.txt"));

    //如果有下一行就返回true
            while (in.hasNextLine())
            {

      //获取下一行
                String str = in.nextLine();

      //调用spilitt方法进行分割
                splitt(str);
            }
            System.out.println("sum:" + sum + " " + "average:" + sum / count);

        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
        }
    }

    public static String[] splitt(String str)
    {

   //去空格
        String strr = str.trim();

    //以逗号为分隔符
        String[] abc = strr.split(",");

   //以汉字元为分隔符
        String[] ab = abc[1].split("元");

   //把String类型数据转化成Double类型
        Double temp = new Double(ab[0]);

   //求和
        sum += temp;

   //计数
        count++;

   //返回
        return abc;
    }
}

简单利用Scanner对文件进行解析

原文:http://www.cnblogs.com/qingtianBKY/p/6115564.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!