本文目錄一覽:
- 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]+” “);
}
}
}
原創文章,作者:VZFU5,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/128891.html