ppolymorphe wrote:
I wouldn't use a tree for this problem, it just complicate things.
Quote:
Then what should i do, can you help me
That is a matter of "problem analyze" : you
can build a tree,
but do you need to ?
As you have already found, you need to read lines 1 by 1
while ((line = reader.readLine()) != null) {
}
When you have this line: "Abcd - Tqrsp - Jumak - Lobte"
You are required to count how many times you have seen each substring as:
"Abcd - Tqrsp - Jumak - Lobte"
"Abcd - Tqrsp - Jumak"
"Abcd - Tqrsp"
"Abcd"
As you get strings, you need to remember each couples of string (key) and counter.
The most simple solution is an array for keys and an array to counters, it will not scale well, but it will do what you need.
For a more efficient solution,some languages have associative arrays, Java have
hashmaps
.
How to get each substring ?
you already use split:
String [] strings = str.split(" - ");
When you need something more sophisticated, go RegEx (Regular Expressions)
This RegEx will drop the last part of a string
"^(.+)\s-\s[^-]+$"
Just a few interesting links to help building and debugging RegEx.
Here is a link to RegEx documentation:
perlre - perldoc.perl.org[
^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[
^]
Expresso Regular Expression Tool[
^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx:
Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[
^]
Nota: I am not a Java programmer, I am just a programmer.