[英]Frequency counter for Strings in Java
I have a string
that a user will type in representing time. 我有一个
string
,用户可以输入它来表示时间。 ie "10:05" 即“ 10:05”
I've been searching for a while, and cannot find a way to search this string
to find a specific number. 我已经搜索了一段时间,无法找到一种方法来搜索此
string
以找到特定的数字。 The reason I need this number, is because I need to take each number and use an algorithm
to discover how much power it takes to display this number on a digital clock. 之所以需要这个数字,是因为我需要取每个数字,并使用一种
algorithm
来发现在数字时钟上显示该数字需要多少功率。 To do so, I need the frequency of each number. 为此,我需要每个数字的频率。 For example, if the time was 10:05, the number 0 would occur 2 times, and I can take that 2 and multiply it by the necessary numbers to discover the power needed.
例如,如果时间是10:05,则数字0会出现2次,我可以将其乘以2并乘以必要的数字,以发现所需的功效。
I'm sure there's a more efficient way to do this, but: 我敢肯定有一种更有效的方法可以做到这一点,但是:
String test = "10:05";
char[] array = test.toCharArray();
int counter = 0;
for(char c : array) {
if (c.Equals('0')) {
counter++;
}
}
System.out.println("The number '0' appears " + counter + " times."); //2
Create a map containing the number you want as key and the frequency that each number that appears as value. 创建一个包含要用作键的数字以及每个数字作为值出现的频率的映射。 Iterate through the string, character by character, disregarding non-digit characters increment the digit-frequency mapping as you go.
逐个字符地遍历字符串,不管非数字字符如何,都将随着您的执行而增加数字频率映射。
Example: 例:
HashMap<Integer, Integer> num_freq = new HashMap<>();
String input = "10:05"; // Example input
char[] input_chars = input.toCharArray();
for(char c : input_chars){
// Accept only characters that are digits
if(Character.isDigit(c)){
// Grabs digit from character
int num = Character.digit(c, 10);
// Put 1 into map if no entry exists, else increment existing value
num_freq.compute(num, (k_num, freq) -> freq == null ? 1 : freq + 1);
}
}
// Print result out
num_freq.forEach((num, freq) -> {
System.out.println("Digit " + num + " appears " + freq + " time(s)");
});
If I got your question, then below code will work, I hope - 如果我有您的问题,那么下面的代码将起作用,我希望-
int[] powerArr = {2,3,2,3,2,3,2,3,2,3};
String input = "10:05";
int powerConsumption = 0;
for(char c : input.replace(":", "").toCharArray()) {
powerConsumption = powerConsumption + powerArr[Integer.parseInt(String.valueOf(c))];
}
powerConsumption : Total power consumption power消耗:总功耗
powerArr : array of power to display 0, then power to display 1 ... power to display 9 powerArr:显示0的电源数组,然后显示1的电源...显示9的电源
If you are interested on number of occurance only then - 如果您仅对出现次数感兴趣,则-
int[] counterArr = {0,0,0,0,0,0,0,0,0,0};
String input = "10:05";
int tmpVal = 0;
for(char c : input.replace(":", "").toCharArray()) {
tmpVal = Integer.parseInt(String.valueOf(c));
counterArr[tmpVal] = counterArr[tmpVal]+1;
}
First value of counterArr will represent occurance of 0, second one of 1 and so on.. counterArr的第一个值表示出现的次数为0,第二个为1,依此类推。
This worked for me: (Assuming that time is entered always in xx:yy format) 这对我有用:(假定始终以xx:yy格式输入时间)
public static void main(String args[])
{
String time = "10:07"; //User Input
time = time.replace(":", "");
char digit[] = {time.charAt(0), time.charAt(1), time.charAt(2), time.charAt(3)};
int[] count = new int[digit.length];
Arrays.sort(digit);
for (int i = 0; i < digit.length; i++)
{
count[i]++;
if (i + 1 < digit.length)
{
if (digit[i] == digit[i + 1])
{
count[i]++;
i++;
}
}
}
for (int i = 0; i < digit.length; i++)
{
if (count[i] > 0)
{
System.out.println(digit[i] + " appears " + count[i]+" time(s)");
}
}
}
Output: 输出:
0 appears 2 time(s)
1 appears 1 time(s)
7 appears 1 time(s)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.