+ 1

how can i print the second "FOR" or first "FOR" from the following wholeString using some loop not the substring method.

public class test{ public static void main(String[] args){ String wholeString ="ASK NOT WHAT YOUR COUNTRY CAN DO FOR YOU BUT WHAT YOU CAN DO FOR YOUR COUNTRY"; String[] justWords = wholeString.split(" ");String searchString ="FOR"; for(int i =0; i < justWords.length; i++){if(justWords[i].equals(searchString)){ System.out.println("The word "+ searchString +" is in the position: "+ i); } } } }

15th Nov 2017, 12:53 PM
mouri
2 Réponses
+ 9
Looks like u already have it figured, what do u want?
15th Nov 2017, 1:58 PM
David Akhihiero
David Akhihiero - avatar
0
it prints both of the position if there is any duplicate words like "for" is there.I only want the particular ones position. like print the first "for " position from the sentence
15th Nov 2017, 2:06 PM
mouri