我有一個字符串集(字符串1,字符串2,字符串3,字符串4),我想在一個文本文件(input.txt)使用java編碼,我搜索已嘗試使用下面的命令,但它不工作,請幫助。需要Java代碼導入文本文件比較一組字符串
package string;
import java.io.File; //Required for input file
import java.io.FileNotFoundException; //Required for exceptioenter code heren throw
import java.util.Scanner; //required for scanner
public class strng {
public static void main(String[] args) throws FileNotFoundException // throws clause added
{
//ask the the for the string to be searched
Scanner keyboard = new Scanner(System.in);
System.out.print("Please enter part of the string: ");
String searchString = keyboard.next().toLowerCase();
// open the data file
File file = new File("APPD_01_15_16_01.txt");
// create a scanner from the file
Scanner inputFile = new Scanner (file);
// While there is another line to read.
while(inputFile.hasNext())
{
// read the lines
//Read string
//Check if user input is a match and if true print out info.
if(searchString.contains("samplemachine")
{
System.out.println("Yup!");
}
else
{
System.out.println("Fail!");
}
}
// be polite and close the file
inputFile.close();
}
}
從inputFile中讀取字符串的位置在哪裏? – suguspnk
您的示例代碼有點令人困惑,因爲您似乎將用戶鍵盤輸入與搜索「samplemachine」的字符串進行比較,而不是來自文件的輸入。 – munyul
我試圖從用戶輸入搜索,但我的要求是從字符串集 –