0

Does this code is right or need to some changes? It basically search the Given word by user in a String and print the word inde

I am new learner of java. Kindly help to improve ny logic and its a humble request to see my code. And guide me. What possible Our teachers give task related to this code? What modification should be done to this code? How can i search word if it happend twice or thrice in string? Any help and advice would be greatly appreciated Thankyou All #HappyProgramming https://code.sololearn.com/cMFoyJMk17xd/?ref=app

15th Nov 2019, 11:56 AM
Abdul Wahab
Abdul Wahab - avatar
11 odpowiedzi
+ 5
Haha, I am certainly not a magician and can't guess what your teacher might be thinking in the future :) It would make sense to ask for the inputs at runtime, so the user can type in any sentence and search word. For that, you would probably use the Scanner class.
15th Nov 2019, 12:36 PM
Tibor Santa
Tibor Santa - avatar
+ 1
First you have this loop for (int i =0;i<sent.length();i++) And then you have these: sent.charAt(i+1) so if the word is at the end, i will exceed the string length and you will get an error. So you should adjust your loop like i<sent.length()-word.length() Also your program works only if the word is exactly 3 characters long. You may want to make another loop inside the first one, to extract each character, same length as the word, starting from index i
15th Nov 2019, 12:20 PM
Tibor Santa
Tibor Santa - avatar
+ 1
Yeah. Got. It. Man thankew soo much. I was getting this error now. I understand. One. More question how can i make this program generic so it can work for every input. Thanks
15th Nov 2019, 12:23 PM
Abdul Wahab
Abdul Wahab - avatar
0
Lothar chek this when u got free Thanks
15th Nov 2019, 12:07 PM
Abdul Wahab
Abdul Wahab - avatar
0
It is better if you do not change either the string nor the search phrase. You could adjust the for loop to consider the size of 'sent' and 'word' . The index should check only until sent.length()-word.length() This way you can avoid ArrayOutOfBoundsException, when the word is at the very end. I recommend to use the substring() method to capture parts of the sent string and compare them with word. https://www.javatpoint.com/substring
15th Nov 2019, 12:08 PM
Tibor Santa
Tibor Santa - avatar
0
Tibor Santa Our teacher have strictly forbid to use any builtin function in program except charAt(i)
15th Nov 2019, 12:10 PM
Abdul Wahab
Abdul Wahab - avatar
0
Sent.length-word.length? Can u explain little bit more Thanks
15th Nov 2019, 12:11 PM
Abdul Wahab
Abdul Wahab - avatar
0
It will work for any input, if you do the changes I suggested. It will be able to detect multiple occurrences of the word too, just don't use break when you find the first one.
15th Nov 2019, 12:25 PM
Tibor Santa
Tibor Santa - avatar
0
Ok Thanks Allot Man Do u have any idea what could possible Our teacher asks to some more Modification in this code?
15th Nov 2019, 12:27 PM
Abdul Wahab
Abdul Wahab - avatar
0
Wht could possible our Sir can give more tasl in this program. Any Task in ur mind so i will try to solve it now
15th Nov 2019, 12:28 PM
Abdul Wahab
Abdul Wahab - avatar
0
Hahaha thanks for the help
15th Nov 2019, 12:50 PM
Abdul Wahab
Abdul Wahab - avatar