搜索Java ArrayList并返回元素位置 - indexOf()无效。

时间:2017-03-27 12:48:56

标签: java arraylist

我一直在编写一个程序,要求用户输入名称,票据类型和罚款。用户输入存储在文本文件中。我正在将文本文件读入名为“list”的数组中,并提示用户搜索“关键字”。我将关键字存储为String =关键字。当我使用println(list.indexOf(keyword))时,即使正在搜索的关键字返回找到,我也会得到“-1”输出!这就是我被困住的地方......我不确定为什么indexOf()方法找不到元素,但我对关键字的搜索能够。任何帮助将不胜感激!注意:我的最终目标是搜索“关键字”,找到元素位置并返回true。然后,我将要求用户提供新名称,票据类型和罚款。我将替换文本文件中的信息,并将新信息写入前面提到的文本文件中。提前感谢您的意见。包含代码(我是Java的新手,所以请好一点:0)一旦完成程序,我将清理代码。

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");
    }

  }

}

2 个答案:

答案 0 :(得分:1)

如果你想获得你通过搜索找到的元素的索引,你可以在同一点获得该索引,如果你当前将你的布尔值设置为true:

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 {
  //....
}

答案 1 :(得分: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不会在元素中搜索关键字。它搜索元素的索引,该索引等于传递的参数。