編輯:問題解決了!我只是瞎了:)在java程序上查找詞典文件中兩個詞的距離
正如標題所說,我一直在努力尋找兩個輸入單詞之間的距離。字典文件只是一個文字,由空格分隔。每次我運行該程序時,都會說輸入的兩個字之間有0個單詞。我不知道我做錯了什麼。
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class wordDistance {
public static void main(String[] args) throws FileNotFoundException {
Scanner s = new Scanner(System.in);
Scanner sfile = new Scanner(new File("C:/Users/name/Desktop/Eclipse/APCS/src/dictionary.txt"));
int count = 0;
System.out.print("Type two words: ");
String start = s.next();
String end = s.next();
while (sfile.hasNextLine()) {
String line = sfile.nextLine();
String[] words = line.split(" ");
for (int i = 0; i < words.length; i++) {
if (words[i] == start) {
for (int j = i + 1; j < words.length; j++) {
if (!(words[j] == end)) {
count++;
}
if (words[j] == end) {
break;
}
}
}
}
}
System.out.println("There are " + count + " words between " + start + " and " + end);
}
}
的[我如何在Java中比較字符串?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java)可能的複製 –