首页 > 编程知识 正文

利用java代码做到统计数据(Java统计)

时间:2023-12-08 14:00:29 阅读:313309 作者:QGKO

本文目录一览:

  • 1、怎么用java编写统计文件中的字符数、单词数和行数?
  • 2、#java如何实现数据统计#用Java实现店铺的数据统计,PV,UV等信息?
  • 3、用Java编个程序,统计文件中的信息,具体要求如下
  • 4、Java 统计数字 【循环】【数组】

怎么用java编写统计文件中的字符数、单词数和行数?

在C盘新建文件1.txt,输入任意字符,如下图:

编写java代码。如下:

import java.io.BufferedReader;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.IOException;

import java.util.TreeMap;

public class Test {

// 统计数字或者字符出现的次数

public static TreeMapCharacter, Integer Pross(String str) {

char[] charArray = str.toCharArray();

TreeMapCharacter, Integer tm = new TreeMapCharacter, Integer();

for (int x = 0; x  charArray.length; x++) {

if (!tm.containsKey(charArray[x])) {

tm.put(charArray[x], 1);

} else {

int count = tm.get(charArray[x]) + 1;

tm.put(charArray[x], count);

}

}

return tm;

}

public static void main(String[] args) {

BufferedReader br = null;

int line = 0;

String str = "";

StringBuffer sb  = new StringBuffer();

try {

br = new BufferedReader(new FileReader("c:\1.txt"));

while ((str = br.readLine()) != null) {

sb.append(str);

++line;

}

System.out.println("n文件行数: " + line);

System.out.println("n文件内容: " + sb.toString());

TreeMapCharacter, Integer tm = Pross(sb.toString());

System.out.println("n字符统计结果为:" + tm);

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} finally {

if (br != null) {

try {

br.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

}运行结果如下图:

#java如何实现数据统计#用Java实现店铺的数据统计,PV,UV等信息?

1、在第三方网站中加入统计脚本;

2、当网站被访问时候,脚本会发送当前浏览器的信息、访问者的信息及当前页面信息提交到统计的服务器;

3、统计服务器定期对提交上来的数据进行分析和汇总;

用Java编个程序,统计文件中的信息,具体要求如下

public static void test6(){

FileInputStream inputStream = null;

Scanner sc = null;

int countBuyIn = 0;//买入条数

int countBuyOut = 0;//卖出条数

BigDecimal bdIn = new BigDecimal(0);//买入价格汇总

BigDecimal bdOut = new BigDecimal(0);//买出价格汇总

try {

    inputStream = new FileInputStream("D:\test.txt");

    sc = new Scanner(inputStream, "gbk");

    while (sc.hasNextLine()) {

        String line = sc.nextLine();

//         System.out.println(line);

        String[] arr = line.split(",");

        if(arr.length==8){

         if(line.indexOf("买入")-1){

         countBuyIn++;

         bdIn = bdIn.add(new BigDecimal(arr[7]));

         }else if(line.indexOf("卖出")-1){

         countBuyOut++;

         bdOut = bdOut.add(new BigDecimal(arr[7]));

         }

        }

    }

    // note that Scanner suppresses exceptions

    if (sc.ioException() != null) {

        throw sc.ioException();

    }

}catch(Exception e){

e.printStackTrace();

} finally {

    if (inputStream != null) {

        try {

inputStream.close();

} catch (IOException e) {

e.printStackTrace();

}

    }

    if (sc != null) {

        sc.close();

    }

}

System.out.println("买入条数:"+countBuyIn+",买入价格汇总:"+bdIn+";卖出条数:"+countBuyOut+",卖出价格汇总"+bdOut);

}

Java 统计数字 【循环】【数组】

public class TotalNums {

public static void main(String[] args) {

int N=10;//N的值

//一个大小为10的数据存放,0~9数字出现的个数,下标就是数字

int[] nums=new int[10];

for (int i = 0; i nums.length; i++) {//对计数器全部初始化为0

nums[i]=0;

}

for (int i = 1; i = N; i++) {//循环开始

String[] strs=String.valueOf(i).split("");//将i转换为字符串数组

for (int j = 0; j strs.length; j++) {//循环计数累加

if(null!=strs[j] !"".equals(strs[j])){

nums[Integer.parseInt(strs[j])]+=1;

}

}

}

//输出技术器

for (int i = 0; i nums.length; i++) {

System.out.print(nums[i]+" ");

}

}

}

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。