Can u guys please help me understand the code below? Why is the output 1 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can u guys please help me understand the code below? Why is the output 1

public class Program { public static void main(String[] args) { String txt="please locate where 'locate' occours"; System.out.println(txt.indexOf("locate")); } }

11th Aug 2020, 8:57 AM
Suczess Bhankz
Suczess Bhankz - avatar
9 Answers
+ 2
~ swim ~ sorry what? My result was 7......
11th Aug 2020, 9:11 AM
Alexander Thiem
Alexander Thiem - avatar
+ 2
p 0 l 1 e 2 a 3 s 4 e 5 6 l 7 o 8 c 9 a 10 t 11 e 12 .... locate occurs on index 7 the first time!
11th Aug 2020, 9:46 AM
Alexander Thiem
Alexander Thiem - avatar
+ 1
Its definitely 7
11th Aug 2020, 9:35 AM
D_Stark
D_Stark - avatar
+ 1
thanks so much,i understand now
11th Aug 2020, 9:53 AM
Suczess Bhankz
Suczess Bhankz - avatar
0
When I run the programm I get the result 7. Are you sure you got 1 as output?
11th Aug 2020, 9:07 AM
Alexander Thiem
Alexander Thiem - avatar
0
why is the result 7?
11th Aug 2020, 9:43 AM
Suczess Bhankz
Suczess Bhankz - avatar
0
you are welcome
11th Aug 2020, 10:44 AM
Alexander Thiem
Alexander Thiem - avatar
0
indexOf return 1/-1 if true then 1 if false then -1
12th Aug 2020, 6:28 AM
Amit
Amit - avatar
0
The result is 7
13th Aug 2020, 8:35 AM
Adepoju sheriffdeen
Adepoju sheriffdeen - avatar