我一直在編寫一個程序,要求用戶輸入名稱,票據類型和罰款。用戶輸入被存儲到文本文件中。我正在將文本文件讀入名爲「list」的數組中,並提示用戶搜索「關鍵字」。我將關鍵字存儲爲String =關鍵字。當我使用println(list.indexOf(關鍵字))時,即使正在搜索的關鍵字正在返回,我也會得到一個「-1」輸出!這就是我被卡住的地方......我不確定爲什麼indexOf()方法找不到該元素,但是我的搜索關鍵字能夠。任何幫助將不勝感激!注意:我的最終目標是搜索「關鍵字」,找到元素位置並返回true。然後,我會問用戶一個新的名稱,票類型和罰款。我將替換文本文件中的信息並將新信息寫入前面提到的文本文件。預先感謝您的意見。包含代碼(我是Java新手,所以請保持良好:0)完成程序後,我將清理代碼。搜索Java ArrayList並返回元素位置 - indexOf()不起作用。
import java.util.*;
import java.io.*;
import java.text.Collator;
public class Project {
public static void main(String[] args) throws IOException {
String name = "";
String ticketType = "";
double fine = 0;
char quit;
boolean cont = true;
while (cont){
do {
//Create file Project.txt
PrintStream out = new PrintStream(new FileOutputStream("Project.txt", true));
Scanner input = new Scanner(System.in);
//Prompt user to enter name, ticket type, fine or 'q' if done
System.out.println("Name:");
name = input.nextLine();
System.out.println("Ticket Type:");
ticketType = input.nextLine();
System.out.println("fine:");
fine = input.nextDouble();
System.out.println("press 'q' if you are done entering or 'c' to continue entering");
quit = input.next().toLowerCase().charAt(0);
//Write user input to Project.txt file
out.print(name + ", ");
out.print(ticketType + ", ");
out.printf("%.2f",fine);
out.println();
out.close();
}
while (!(quit == 'q'));{
System.out.println("done");
}
Scanner input = new Scanner(System.in);
System.out.println("are you sure youre done");
System.out.println("enter y to if your done, n to enter more");
char Continue = input.next().toLowerCase().charAt(0);
//Prompt user in the are done entering
//If 'y', program moves on. If 'n' loop back to beginning
if (Continue == ('y')){
cont = false;
}
//Done entering names to list
//Now read in file to array and sort alphabetically
}
Scanner readArray = new Scanner(new File("Project.txt"));
ArrayList<String> list = new ArrayList<String>();
while (readArray.hasNext()){
list.add(readArray.nextLine().toLowerCase());
}
readArray.close();
Collections.sort(list, Collator.getInstance()); //sort array alphabetically
//print array as list comma separated
for (String value : list){
System.out.println(value);
}
//Prompt user to enter a name they want to search for
Scanner search = new Scanner(System.in);
System.out.println("search keyword: ");
String keyword = search.next().toLowerCase();
System.out.println("searching for: " + keyword);
//Search array 'list' for user input
boolean found = false;
for (String value : list) {
if (value.contains(keyword)) {
found = true;
}
}
if (found) {
System.out.println("found");
System.out.println(list.indexOf(keyword));
}
else {
System.out.println(" not found");
}
}
}
你AREN不檢查您的列表是否包含關鍵字。您正在檢查列表中的任何字符串是否包含關鍵字。因此,如果你的List有一個String''Something Test'',你會搜索''Something''作爲關鍵字'indexOf(「Something」)'將返回-1,但是你的搜索會發現一些東西,你會檢查' 「Something Test」.contains(「Something」)'返回true。 –
此外,不需要單獨的循環來查找列表是否包含所請求的值。你可以調用indexOf(),如果返回的值是-1,那麼你知道搜索失敗 –
感謝您的反饋!我正在尋找「Something Test」.contains(「Something」),我正在得到正確的回報。我正在搜索的字符串確實返回找到。我正在努力的是返回數組中的元素位置。我想返回元素,然後使用set(int index,element)。我只需要知道如何獲取元素位置,因爲indexOf()不工作(返回位置爲-1) –