1

I’m learning java and I’m just trying to figure how to go about solving this problem. I have a string ex: The <object> <verb>, on the <object>.

Every string that contained in <> (<> are only for clarification and not in the original string) are keys to a hash map that will return a random value.
I then break the string into an array of strings, and then loop through the array and search the hash map if the key exist return a value, here is where I encounter a problem, in the example above the <verb> is a key but not <verb>, (with the comma)
How can I break away from the comma but then return a value with it.

So the end result I don’t need the full code for this, just ideas on how to solve this particular problem.

The dog sat, on the cat.

leonardkraemer
  • 6,573
  • 1
  • 31
  • 54
Luis
  • 41
  • 4
  • I would iterate over the map instead: `for (var entry : map.entrySet()) str = str.replace(entry.getKey(), entry.getValue());` – shmosel Dec 05 '18 at 23:13
  • Btw, how do you have a "hash map that will return a random value"? – shmosel Dec 05 '18 at 23:14
  • 1
    use str.replaceAll(", ", " "); to remove tariling comma – Mohsen Dec 05 '18 at 23:17
  • The value to each key is an array list of words. I select a word from the list. To your first comment, why would you iterate through the map? The string sentence is what you’re trying to solve. – Luis Dec 05 '18 at 23:17
  • if my idea help you tell me to make it as an answer for you bro :) – Mohsen Dec 05 '18 at 23:19
  • What is your expected output? – Tim Biegeleisen Dec 05 '18 at 23:20
  • see https://stackoverflow.com/questions/4576352/remove-all-occurrences-of-char-from-string – leonardkraemer Dec 05 '18 at 23:21
  • What do you mean by "but then return a value with it."? Also, what about the period at the end of the sentece? is "dog." not a problem for you? – Lev M. Dec 05 '18 at 23:29
  • Hi Spara would that comma just be replaced with a space?. Because I would need the comma back in it’s original place once I returned the value – Luis Dec 05 '18 at 23:44
  • The original `str` will not change and the result of `str.replaceAll(", ", " ")` has no more commas – Mohsen Dec 05 '18 at 23:47
  • What about replacing all the commas for a comma with a space before doing `str.replaceAll(",", " ,")`, do the same you are doing now (should work now because there is space now between the word and the comma) and, at the end, replace again all the " ," for "," doing `str.replaceAll(" ,", ",")`. I know it is kind of dirty, but it works. If you want a more detailed answer, you should upload your code. If it works, let me know and I can put it as an answer. – dglozano Dec 05 '18 at 23:50
  • Lev M, the return value would be what I return from the hashmap for example the word verb is a key and it would return a string in its place with the comma. The period at the end is a easier fix because I can replace the period as a space suggested above and then add the period at the end. Not the best solution, which is why I was trying to figure this out. – Luis Dec 05 '18 at 23:59
  • dglozano I think that would work, would I then have to do the same for each ?, !, :, ;. Character? – Luis Dec 06 '18 at 00:03
  • You can use a regular expression to match all the things that are not a letter, and do the replacement. If you want this solution I can make a code sample and post an answer. – dglozano Dec 06 '18 at 00:45
  • @Luis If my answer has helped you please mark it as answered – nemanja228 Dec 08 '18 at 16:06

2 Answers2

0

You can use regular expressions to extract all words (consisting only of letters), and then search the map as you wish. I know that you ask only for comma, but I assume this is the use-case.

List<String> allMatches = new ArrayList<String>();
Matcher m = Pattern.compile("[a-zA-Z]+") //regex for letter-strings only
     .matcher(yourString); // e.g. "The dog sat, on the cat."
 while (m.find()) {
    allMatches.add(m.group());
 }

Result will be a following list:

{"The", "dog", "sat", "on", "the", "cat"}

Then you can iterate allMatches to find appropriate results from your data structure.

P.S. When you use regex, try to compile the pattern only once and reuse it if required again, as it's not that cheap operation.

nemanja228
  • 556
  • 2
  • 16
0

The split method of String class in Java takes regular expressions, and regular expressions in Java have the or operator similar to if.

So instead of splitting on a single character like a space, you can split on several different things like coma and space, just a space, and a period.

String sentence = "The dog sat, on the cat.";
String [] words = sentence.split(", | |\\.");

The pipe character | is the or operator for regex. Note that I added the \\. which will remove the '.' from 'cat'. In regex, the dot means "any letter" so to match and actual dot (period, end of sentence) you need to escape it with a \. And in a Java string literals (anything between "") \\ means put an actual \ in the string so \\. will become \. when split receives it as a parameter.

There is even a more general way:

String [] words = sentence.split("\\W+");

\W means "any non-word character" - anything other then a letter, a digit or an underscore, and + means "appearing one or more times in a row".

So this will split the string on anything that is not a word.

Remember that in Java, String class is immutable - its contents can not be changed once it is created.

So no matter which solution you use - replaceAll suggested by Spara, word search using Matcher suggested by nemanja228, or the split in my answer, there will always be copies of the original string created, and the original will not be changed, so you just need to keep a reference to it to preserve it for future use (don't change the variable that holds it).

Lev M.
  • 6,088
  • 1
  • 10
  • 23