I have a string
that a user will type in representing time. ie "10:05"
I've been searching for a while, and cannot find a way to search this string
to find a specific number. 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. 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.
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
powerArr : array of power to display 0, then power to display 1 ... power to display 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..
This worked for me: (Assuming that time is entered always in xx:yy format)
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.