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

正在搜索Java ArrayList并返回元素位置-indexOf()无效。,java,arraylist,Java,Arraylist,我一直在写一个程序,要求用户输入姓名、票证类型和罚款。用户输入被存储到文本文件中。我正在将文本文件读入名为“list”的数组,并提示用户搜索“关键字”。我将关键字存储为字符串=关键字。当我使用println(list.indexOf(keyword))时,即使正在搜索的关键字返回find,我也会得到一个“-1”输出!这就是我被卡住的地方…我不知道为什么indexOf()方法找不到元素,但是我对关键字的搜索能够找到。任何帮助都将不胜感激!注意:我的最终目标是搜索“关键字”,找到元素位置并返回tru

我一直在写一个程序,要求用户输入姓名、票证类型和罚款。用户输入被存储到文本文件中。我正在将文本文件读入名为“list”的数组,并提示用户搜索“关键字”。我将关键字存储为字符串=关键字。当我使用println(list.indexOf(keyword))时,即使正在搜索的关键字返回find,我也会得到一个“-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");
    }

  }

}
import java.util.*;
导入java.io.*;
导入java.text.Collator;
公共类项目{
公共静态void main(字符串[]args)引发IOException{
字符串名称=”;
字符串ticketType=“”;
双倍罚款=0;
退出;
布尔控制=真;
while(续){
做{
//创建Project.txt文件
PrintStream out=新的PrintStream(新的FileOutputStream(“Project.txt”,true));
扫描仪输入=新扫描仪(System.in);
//提示用户输入名称、票证类型、罚款或“q”(如果完成)
System.out.println(“名称:”);
name=input.nextLine();
System.out.println(“票证类型:”);
ticketType=input.nextLine();
System.out.println(“精细:”);
fine=input.nextDouble();
System.out.println(“如果输入完毕,请按'q'或'c'继续输入”);
quit=input.next().toLowerCase().charAt(0);
//将用户输入写入Project.txt文件
打印(名称+“,”);
打印(票号类型+“,”);
out.printf(“%.2f”,罚款);
out.println();
out.close();
}
而(!(quit='q'){
系统输出打印项次(“完成”);
}
扫描仪输入=新扫描仪(System.in);
System.out.println(“您确定完成了吗”);
System.out.println(“输入y表示完成,输入n表示输入更多”);
char Continue=input.next().toLowerCase().charAt(0);
//在完成输入后,在中提示用户
//若为“y”,程序继续运行。若为“n”,则循环回到开始
如果(Continue==('y')){
cont=假;
}
//完成输入要列出的名称
//现在读入文件以按字母顺序排列和排序
}
Scanner readArray=new Scanner(新文件(“Project.txt”);
ArrayList=新建ArrayList();
while(readArray.hasNext()){
add(readArray.nextLine().toLowerCase());
}
readArray.close();
Collections.sort(list,Collator.getInstance());//按字母顺序对数组排序
//以逗号分隔的列表形式打印数组
for(字符串值:列表){
系统输出打印项次(值);
}
//提示用户输入要搜索的名称
扫描仪搜索=新扫描仪(System.in);
System.out.println(“搜索关键字:”);
字符串关键字=search.next().toLowerCase();
System.out.println(“搜索:”+关键字);
//搜索用户输入的数组“列表”
布尔值=false;
for(字符串值:列表){
if(value.contains(关键字)){
发现=真;
}
}
如果(找到){
System.out.println(“找到”);
System.out.println(list.indexOf(关键字));
}
否则{
System.out.println(“未找到”);
}
}
}

如果正确理解您的问题,下面的代码将对您有所帮助

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不在元素中搜索关键字。它搜索元素的索引,该索引等于传递的参数。

如果您想获取通过搜索找到的元素的索引,您可以在当前将布尔查找设置为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 {
  //....
}

您没有检查列表是否包含关键字。您正在检查列表中是否有任何字符串包含关键字。因此,如果您的列表中有一个字符串
“Something Test”
,您将搜索
“Something”
,作为关键字
indexOf(“Something”)
将返回-1,但您的搜索将找到一些内容,因为您将选中
“Something Test”。包含(“Something”)
,返回true。此外,不需要单独的循环来查找列表是否包含请求的值。您可以调用indexOf(),如果返回值为-1,则您知道搜索失败感谢您的反馈!我正在搜索“某物测试”。包含(“某物”),并且我得到了正确的返回。我正在搜索的字符串没有返回find。我要做的是返回数组中的元素位置。我想返回元素,然后使用set(int-index,element)。我只需要知道如何获取元素位置,因为indexOf()不起作用(返回-1表示位置)提示:您希望我们花时间来帮助您。因此,请您花5分钟的时间将您的输入正确格式化/导入我们。除此之外:阅读;你写了很多代码;我觉得这只需要其中的一部分。谢谢你的建议,但我仍然没有得到元素的位置。我将上述代码添加到我的程序中,并添加了System.out.println(“find”)和System.out.println(foundIndex)。如果我运行程序并输入name:mike,票证类型:speed和fine:25,我搜索mike(关键字),在我的输出中找到,但是它返回-1作为foundIndex值。Integer foundElement=null;对于(字符串值:列表){if(value.contains(关键字)){foundElement=list.indexOf(关键字);break;}}}boolean found=(foundElement!=null);if(find){System.out.println(“find”);System.out.prin