如何计算文本文件中的数字? [英] How do I count numbers inside a text file?

查看:147
本文介绍了如何计算文本文件中的数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这里有一个源代码,用于计算字母字符和非字母字符的频率(请参阅下面的源代码)。



I have a source code here that counts the frequency of alphabetic characters and non-alphabetic characters (see the source code below).

import java.io.*;

public class letterfrequency {
	public static void main (String [] args) throws IOException {
		File file1 = new File ("letternumberfrequency.txt");
		BufferedReader in = new BufferedReader (new FileReader (file1));
		
		int nextChar;
		int other = 0;
		char ch;
		
		int [] count = new int [26];
		
		while ((nextChar = in.read())!= -1) {
			ch = ((char)nextChar);
			ch = Character.toLowerCase (ch);
			if (ch >= 'a' && ch <= 'z')
			  count [ch- 'a']++;
			else
			  other ++;
		}
		
		for (int i=0; i<26; i++){
			System.out.printf ("%c = %d \n", i+ 'A', count [i]);
		}
		
		System.out.println ("Non-alphabetic characters: " + other);
		in.close ();
	}
}





但是,我们只是说现在我在文本文件中有以下字符, letternumberfrequency.txt:

71只鹅 - 83辆汽车 - 58头奶牛 - 64头摩托车 - 100辆蚂蚁 - 69个手镯 - 90个小丘 - 87个鼻子



该文本文件中的数字将被视为字符串,我是对的吗?

但是我想提取数字以便我也可以计算他们的频率 - 不是个别数字,而是整数(即多少71,83,58,64等......)。

使用Double.parseDouble()帮助?



But, let's just say that now I have the following characters in the text file, "letternumberfrequency.txt":
71 geese - 83 cars - 58 cows- 64 mooses- 100 ants- 69 bangles- 90 molehills - 87 noses

The numbers inside that text file would be considered as strings, am I right?
But I want to extract the numbers so that I can also be able to count their frequency - not as individual digits but as whole numbers (that is how many "71", "83", "58", "64", etc. are there...).
Would using "Double.parseDouble ()" help?

推荐答案

1。首先,使用非数字作为分隔符将整个字符串拆分为数字字符串数组。

2.接下来,计算重复数字字符串的数组并将它们存储为Map中的键值对

3.用地图做任何你喜欢的事情

参见示例:

1. First, split the whole string into array of number strings using non-digit as delimiters.
2. Next, count the array for repeated number strings and store them as key value pairs in a Map
3. Do whatever you like with the Map
See example:
String str = "71 geese - 83 cars - 58 cows- 64 mooses";
// use regex to split by any non-digit delimiters
String numberArray[] = str.split("[^\\d]+");
// count and store the number word and its count in a Map
Map<String, Integer> map = new HashMap<String, Integer>();
for ( String numberWord : numberArray) {
   Integer counter = map.get(numberWord);
   if ( counter == null ) {
            counter = 0;
   }
   map.put(numberWord, counter + 1);
}



查找地图中每个数字的计数:


to find out the count of each number word in the Map:

for ( String numberWord : map.keySet() ) {
  System.out.println("%s => %i", numberWord, map.get(numberWord));
}



我没有测试过它的任何错误,只是从我的头上,但想法就在那里,祝你好运。



+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ $ b我的观察:

1.错字和语法错误,例如String,Integer,记住Java区分大小写;

2.缺少地图等的进口。

3.逻辑错误,为什么关闭while循环中的bufferedreader?

4.其他一些小错误

请参阅以下工作示例并与您的比较:


I have not tested it for any bugs, just from my head, but the idea is there, good luck.

++++++++++++++[Added upon request]++++++++++++++++
My observation:
1. Typo and syntax errors, e.g. String, Integer, remember Java is case sensitive;
2. Missing imports for map etc.
3. logical error, why close the bufferedreader in the while loop?
4. Some other minor mistakes
See the following working example and compare it with yours:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class Letternumberfrequency {
	public static void main(String[] args) throws IOException {
		
	    FileReader fr = new FileReader("letternumberfrequency.txt");
	    BufferedReader in = new BufferedReader(fr);
	
		String line;
		while ((line = in.readLine()) != null) {
		 
			String numberArray[] = line.split("[^\\d]+");
			Map<String, Integer> map = new HashMap<String, Integer>();
			for (String numberWord : numberArray) {
				Integer counter = map.get(numberWord);
				if (counter == null) {
					counter = 0;
				}
				map.put(numberWord, counter + 1);
			}
			
			for (String numberWord : map.keySet()) {
				System.out.println(numberWord + " => " +  map.get(numberWord));
			}

		}
		in.close();
	}
}



教程结束。


End of Tutorial.


文本文件中的单词数量 [ ^ ]。


这篇关于如何计算文本文件中的数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆