Find Text
Completed
Read a sentence (full line) and a word (next line). Print the index of the first occurrence, the index of the last occurrence, and true/false for contains.
Input
Two lines.
Output
Three lines (-1 when not found).
Example
Input
the cat and the hat the
Output
0 12 true
Editor
Java
Hints
indexOf returns -1 when the text isn't found.
Solution explanation
indexOf, lastIndexOf and contains cover most searching needs.
Show solution
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String text = sc.nextLine();
String word = sc.nextLine();
System.out.println(text.indexOf(word));
System.out.println(text.lastIndexOf(word));
System.out.println(text.contains(word));
}
}