Buzz Code
Published in

Buzz Code

Java 8 | String Method Practice 2

Hi guys! It’s another String method practice today! Don’t forget to checkout my last posts below.

You can only use length() , charAt() , equals() method just like the last practice. Let’s do this!

[Question 1] Find all the “can” in the String quote, and print it out, and print other letters into under bar “_” Like the sample below.

String quote = "Whether you think you can or can't, you're right.";**The sample of the result on the console**

It’s a very famous quote by Henry Ford, and that is true. Think you can do anything! Yay! The answer is on below.

I declared the String can and int cnt . And I put the nested loop. Then I put the variable i as a parameter of the quote.charAt() , and j as a parameter of the can.charAt() .

So when the i is 0, the quote.charAT(i) will be the character ‘W’, and the j will be 0 as well so the can.charAt(j) will be the character ‘c’. Since those are not same, it will prints under bar “_” and ends the for loop of variable j . And it will be like that until the i is 21.

But when the i is 22, the quote.charAT(i) will be the character ‘c’, and the j will be 0 so the can.charAt(j) will be the character ‘c’. When the letters are same, I made i gets one bigger to compare quote.charAt(23) with the can.charAt(j) cause j will be 1 on the second round of the loop. And I made cnt gets one bigger as well to count how many letters are same. Then after the for loop of j finished another if statement will check if the cnt is same with the can.length() which is 3. Then I printed can out and made i gets one smaller, and cnt becomes 0 again.

What would be happened If I didn’t made the i gets one smaller?

It might seem like there’s nothing different, but If I change the under bars into the numbers of the index of quote it will be like below.

Do you see the i was 21 and after it printed out it became 26 but it should be 25. It’s because I made i gets one bigger to compare with the String can , but even after It’s done with the task i still got one bigger. That’s why i-- is necessary when it’s done with printing out can .

[Question 2] Secrete Love Message.

Use the String ILY print the sample below.

String ILY = "I Love You";**Sample**

Here we can use the ASCII Code and the concept that you can make the char data type into a number.

ASCII Code table

As you see, The word ‘I’ in the String ILY became ‘J’, and on the ASCII Code table it seems like decimal number of the word ‘I’ is 73, and it’s 74 for the word ‘J’. That’s right we need to make each letter’s decimal number gets one bigger! Try to solve it by yourself and the answer is on below.

I put the ILY.charAt(i) in the char variable c So I can use the increment operator on it. In this way, you can print the secret love message out! Well done guys!

That’s all for today, thank you for reading my post guys. On the next post I’ll talk about the Two Dimensional Array. Have a great weekend! See ya!



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store