Published in

Buzz Code

# Java 8 | StringTokenizer Practice 2

Hi y’all! It’s another day to practice the StringTokenizer with me!

You can check my last post out above, and let’s get this started!

# [Question 1] Man down!

Let’s say there is a game of the war. And this game records points with the “-”, “+” signs. When there is “-” in front of the number means the player got attacked, and “+” means the player attacked the others.
When there is a String msg that has player’s points, print each of the sum of points that they got and lost.

`String msg = "+1/-1/-1/+2/+1/+1/+2/-3/+3";`

I split the String with the StringTokenizer war and declared two different named attacked and attack to get the sum of each lost and gotten points. And Here I could use the ASCII code to get the int value from the Strings, but instead I used Integer.parseInt() method that gets the int value from String.
So if there is the String “365”, and you put it as a parameter in this method Integer.parseInt(“365”) like this, you can get the int number 365!

# [Question 2] Tongue Twister

`String tt = "I thought a thought. But the THOUGHT I Thought wasn't the ThOuGhT I thought I thought.";`

There is a tongue twister in the String named tt. Store all the words in the array, and count how many of each of them.
※ It’s the same words even their cases are different.
ex) i-1, thought-7, a-1, but-1, the-2, wasn’t-1

I used one of the String method equalsIgnoreCase() to check if they are the same words, and the split() method to divide the String into word and the count. If the word is not in the ArrayList, I put “word count” in the list and if there is the word I added 1 to the count part!

Well that’s all for today guys! Thank you for reading my post, see ya!

--

--

--

Happy coding!

## Student Kim

Learn something new everyday.