我无法弄清楚为什么我的程序会显示用户输入单词的第一个索引,但我似乎无法获得第二个单词来显示正确的索引,另外还有一个问题,如果我想要显示索引用户输入的单词中的每个字母都是最好的方法吗?
import java.util.Scanner;
public class Lab3practice
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
String simplePhrase;
String word;
System.out.println("Enter 6 lines of text");
simplePhrase = keyboard.nextLine();
System.out.println("Enter a word from text to see it's index:");
word = keyboard.nextLine();
int wordFirstIndex = simplePhrase.indexOf(" ");
System.out.println("Begginng index of word:"+wordFirstIndex);
int wordLastIndex = simplePhrase.lastIndexOf(wordFirstIndex);
System.out.println("End of index word:"+wordLastIndex);
}
}