Question

In: Computer Science

In Java, write a program that finds the first character to occur 3 times in a...

In Java, write a program that finds the first character to occur 3 times in a given string?

EX. Find the character that repeats 3 times in "COOOMMPUTERRRR"

Solutions

Expert Solution

/*If you any query do comment in the comment section else like the solution*/

import java.util.HashMap;
import java.util.Scanner;
public class FirstCharacterWithNFrequency {
        public static void main(String[]args) {
                Scanner sc = new Scanner(System.in);
                String str = sc.next();
                HashMap<Character, Integer> hm = new HashMap<Character, Integer>();
                for(int i=0;i<str.length();i++) {
                        if(hm.containsKey(str.charAt(i))) {
                                int currfreq = Integer.parseInt(hm.get(str.charAt(i)).toString());
                                hm.put(str.charAt(i), currfreq + 1);
                        } else {
                                hm.put(str.charAt(i), 1);
                        }
                        int freq = Integer.parseInt(hm.get(str.charAt(i)).toString());
                        if(freq == 3) {
                                System.out.print("First Character encountered with frequency three is: " + str.charAt(i));
                                break;
                        }
                }
        }
}


Related Solutions

Write JAVA program that finds 3 students with the best scores. The program asks users for...
Write JAVA program that finds 3 students with the best scores. The program asks users for scores of 5 students. The program prints the first, second, third place students and scores. You can assume that there is no two students with the same score. <EXAMPLE> enter the score of each student score of student 1: 50 score of student 2: 70 score of student 3: 30 score of student 4: 90 score of student 5: 40 1st place is student...
I need to write java program that do replace first, last, and remove nth character that...
I need to write java program that do replace first, last, and remove nth character that user wants by only length, concat, charAt, substring, and equals (or equalsIgnoreCase) methods. No replace, replaceFirst, last, remove, and indexOf methods and letter case is sensitive. if user's input was "be be be" and replace first 'b' replace first should do "e be be" replace last should do "be be e" remove nth character such as if user want to remove 2nd b it...
Write an LC-3 program that will repeatedly read a character from the keyboard. For each character...
Write an LC-3 program that will repeatedly read a character from the keyboard. For each character read in, your program will print a neat message that echoes the input value, and the ASCII character code of the input character in hexadecimal. It will run forever: no HALT or End of processing is required. For example: Please press a key: You pressed 'z' which is x7A Please press a key: You pressed '@' which is x40 Please press a key: You...
To begin, write a program to loop through a string character by character. If the character...
To begin, write a program to loop through a string character by character. If the character is a letter, print a question mark, otherwise print the character. Use the code below for the message string. This will be the first string that you will decode. Use the String class method .charAt(index) and the Character class method .isLetter(char). (You can cut and paste this line into your program.) String msg = "FIG PKWC OIE GJJCDVKLC MCVDFJEHIY BIDRHYO.\n"; String decryptKey = "QWERTYUIOPASDFGHJKLZXCVBNM";...
Java program: Greatest Common Divisor Write a program which finds the greatest common divisor of two...
Java program: Greatest Common Divisor Write a program which finds the greatest common divisor of two natural numbers a and b Input:  a and b are given in a line separated by a single space. Output: Output the greatest common divisor of a and b. Constraints: 1 ≤ a, b ≤ 109 Hint: You can use the following observation: For integers x and y, if x ≥ y, then gcd(x, y) = gcd(y, x%y) Sample Input 1 54 20 Sample Output...
Write a program whose input is a string which contains a character and a phrase, and whose output indicates the number of times the character appears in the phrase.
# PYTHONWrite a program whose input is a string which contains a character and a phrase, and whose output indicates the number of times the character appears in the phrase.Ex: If the input is:n Mondaythe output is:1Ex: If the input is:z Today is Mondaythe output is:0Ex: If the input is:n It's a sunny daythe output is:2Case matters.Ex: If the input is:n Nobodythe output is:0n is different than N.
*****Using Java Write a program that finds the standard deviation while also using a graphical user...
*****Using Java Write a program that finds the standard deviation while also using a graphical user interface.
Using python: Given a string x, write a program to check if its first character is...
Using python: Given a string x, write a program to check if its first character is the same as its last character. If yes, the code should output "True"
Write a program that finds and prints all of the prime numbers between 3 and X...
Write a program that finds and prints all of the prime numbers between 3 and X (X is input from the user). A prime number is a number such that 1 and itself are the only numbers that evenly divide it (for example, 3, 5, 7, 11, 13, 17, …). One way to solve this problem is to use a doubly nested loop (a loop inside another loop). The outer loop can iterate from 3 to N while the inner...
(Intro/Basic) JAVA Write a small program that gets some numbers as command-line arguments and finds the...
(Intro/Basic) JAVA Write a small program that gets some numbers as command-line arguments and finds the maximum of those numbers. You can assume that the user will provide some command-line arguments when running the program (so you don’t have to worry about what to do if the user doesn’t provide any arguments -- that won’t happen). Also, you can assume that all the command line arguments will be floating-point numbers, i.e., numbers with a decimal point, like 2.95.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT