利用java代碼做到統計數據(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]+” “);

}

}

}

原創文章,作者:VZFU5,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/128891.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
VZFU5的頭像VZFU5
上一篇 2024-10-03 23:25
下一篇 2024-10-03 23:25

相關推薦

  • Java JsonPath 效率優化指南

    本篇文章將深入探討Java JsonPath的效率問題,並提供一些優化方案。 一、JsonPath 簡介 JsonPath是一個可用於從JSON數據中獲取信息的庫。它提供了一種DS…

    編程 2025-04-29
  • java client.getacsresponse 編譯報錯解決方法

    java client.getacsresponse 編譯報錯是Java編程過程中常見的錯誤,常見的原因是代碼的語法錯誤、類庫依賴問題和編譯環境的配置問題。下面將從多個方面進行分析…

    編程 2025-04-29
  • Python周杰倫代碼用法介紹

    本文將從多個方面對Python周杰倫代碼進行詳細的闡述。 一、代碼介紹 from urllib.request import urlopen from bs4 import Bea…

    編程 2025-04-29
  • Java騰訊雲音視頻對接

    本文旨在從多個方面詳細闡述Java騰訊雲音視頻對接,提供完整的代碼示例。 一、騰訊雲音視頻介紹 騰訊雲音視頻服務(Cloud Tencent Real-Time Communica…

    編程 2025-04-29
  • Python字符串寬度不限制怎麼打代碼

    本文將為大家詳細介紹Python字符串寬度不限制時如何打代碼的幾個方面。 一、保持代碼風格的統一 在Python字符串寬度不限制的情況下,我們可以寫出很長很長的一行代碼。但是,為了…

    編程 2025-04-29
  • Java Bean加載過程

    Java Bean加載過程涉及到類加載器、反射機制和Java虛擬機的執行過程。在本文中,將從這三個方面詳細闡述Java Bean加載的過程。 一、類加載器 類加載器是Java虛擬機…

    編程 2025-04-29
  • Java Milvus SearchParam withoutFields用法介紹

    本文將詳細介紹Java Milvus SearchParam withoutFields的相關知識和用法。 一、什麼是Java Milvus SearchParam without…

    編程 2025-04-29
  • Python基礎代碼用法介紹

    本文將從多個方面對Python基礎代碼進行解析和詳細闡述,力求讓讀者深刻理解Python基礎代碼。通過本文的學習,相信大家對Python的學習和應用會更加輕鬆和高效。 一、變量和數…

    編程 2025-04-29
  • Java 8中某一周的周一

    Java 8是Java語言中的一個版本,於2014年3月18日發佈。本文將從多個方面對Java 8中某一周的周一進行詳細的闡述。 一、數組處理 Java 8新特性之一是Stream…

    編程 2025-04-29
  • Java判斷字符串是否存在多個

    本文將從以下幾個方面詳細闡述如何使用Java判斷一個字符串中是否存在多個指定字符: 一、字符串遍歷 字符串是Java編程中非常重要的一種數據類型。要判斷字符串中是否存在多個指定字符…

    編程 2025-04-29

發表回復

登錄後才能評論