Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Java to count the number of string occurrences

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the relevant knowledge of "how to use Java to count the number of string occurrences". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

The details are as follows

Demand:

The healthy disk enters a string, which requires statistics of the number of occurrences of each string in the string.

Example: keyboard entry "aababcabcdabcde"

Output in the console: "a (1) b (4) c (3) d (2) e (1)"

Train of thought:

① healthy disk input a string

② creates a HashMap collection with a key of Character and a value of Integer

③ traverses the character application to get each character

④ takes every character as a key to the HashMap collection to find the corresponding value and see the return value.

If the return value is null: indicates that the character does not exist in the HashMap collection, the character is stored as the key and 1 as the value

If the returned value is not null: indicates that the character exists in the HashMap collection, add the value by 1, and then re-store the character and the paired value

⑤ traverses the HashMap collection, gets the keys and values, and splices them according to the requirements.

⑥ output result

Public class StrCount {public static void main (String [] args) {/ / healthy disk enter a string Scanner sc = new Scanner (System.in); System.out.println ("clear input a string:"); String line = sc.nextLine (); / / create the HashMap collection with the key Character and the value Integer HashMap map = new HashMap () / / traverses the character application to get each character for (int I = 0; I < line.length (); iSum +) {char key = line.charAt (I); / / take each character as a key to find the corresponding value in the HashMap collection and see its return value Integer value = map.get (key) If (value = = null) {/ / if the return value is null: indicates that the character does not exist in the HashMap collection, use the character as the key and 1 as the value to store map.put (key, 1) } else {/ / if the returned value is not ull: this means that the character exists in the HashMap collection, add the value by 1, and then re-store the character and the paired value value++; map.put (key, value) }} / / traverse the HashMap collection to get keys and values, splicing as required StringBuilder sb = new StringBuilder (); Set keySet = map.keySet (); for (Character key: keySet) {Integer value = map.get (key); sb.append (key) .append ("(") .append (value) .append (")") } / / output result String result = sb.toString (); System.out.println (result);}}

The last traversal output was written with the teacher, but I didn't post the following one because I think the teacher's one is more in line with the meaning of the topic, using stitching, and the results are all the same.

/ / iterate through the HashMap collection, get keys and values, and splice as required Set keySet = map.keySet (); for (Character key: keySet) {Integer value = map.get (key); / / output result System.out.print (key + "(" + value + ")") } "how to use Java to count the number of string occurrences" ends here. Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report