2017-03-27 230 views
-1

我一直在編寫一個程序,要求用戶輸入名稱,票據類型和罰款。用戶輸入被存儲到文本文件中。我正在將文本文件讀入名爲「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"); 
    } 

    } 

} 
+1

你AREN不檢查您的列表是否包含關鍵字。您正在檢查列表中的任何字符串是否包含關鍵字。因此,如果你的List有一個String''Something Test'',你會搜索''Something''作爲關鍵字'indexOf(「Something」)'將返回-1,但是你的搜索會發現一些東西,你會檢查' 「Something Test」.contains(「Something」)'返回true。 –

+0

此外,不需要單獨的循環來查找列表是否包含所請求的值。你可以調用indexOf(),如果返回的值是-1,那麼你知道搜索失敗 –

+0

感謝您的反饋!我正在尋找「Something Test」.contains(「Something」),我正在得到正確的回報。我正在搜索的字符串確實返回找到。我正在努力的是返回數組中的元素位置。我想返回元素,然後使用set(int index,element)。我只需要知道如何獲取元素位置,因爲indexOf()不工作(返回位置爲-1) –

回答

1

如果你想通過你搜索你可以得到該指數在相同的點是你當前設置的布爾發現,真正讓您找到的元素的索引:

Integer foundIndex = null; 
for (String value : list) { 
    if (value.contains(keyword)) { 
     foundIndex = list.indexOf(value); 
     break; // we stop iteration because we got our result 
    } 
} 

boolean found = (foundIndex!=null); 
if (found) { 
    //.... 
} else { 
    //.... 
} 
+0

感謝您的建議,但我仍然沒有獲得元素位置。我將上面的代碼添加到我的程序中,並添加了System.out.println(「found」)和System.out.println(foundIndex)。如果我運行該程序並輸入名稱:mike,票據類型:速度和罰款:25,我搜索mike(關鍵字),在我的輸出中找到,但它返回-1 foundIndex值。 –

+0

Integer foundElement = null; \t for(String value:list){ \t if(value.contains(keyword)){ \t foundElement = list.indexOf(keyword); \t break; \t} \t} \t boolean found =(foundElement!= null); \t if(found){ \t System.out.println(「found」); \t System.out。的println(foundElement); \t \t } \t否則{ \t的System.out.println( 「未找到」); \t} \t \t} \t \t} –

+0

@ M.Luke變化'list.indexOf(關鍵字);''來list.indexOf(值);' –

0

如果正確地理解你的問題,那麼下面的代碼將幫助您

list.add("for example") 
list.add("for") 
list.add("example") 
System.out.println(list.indexOf("for example")) 
System.out.println(list.indexOf("for")) 
System.out.println(list.indexOf("example")) 

的indexOf不搜索在元素的關鍵字。它搜索等於傳入參數的元素索引。