在Java ArrayList中搜索逗号分隔值 - 字符串和整数组合。

时间:2017-03-23 19:18:54

标签: java

首先,谢谢你所做的一切。我是Java的新手,我正在创建一个程序来询问用户名称,票据类型和罚金额。我正在获取该信息并将其写入文本文件(project.txt)。我正在使用Scanner readArray读取txt文件的内容,然后将内容排序到'list'数组中。我按字母顺序对列表进行了排序,现在我想在数组中搜索从用户输入中输入的关键字。该列表存储为“名称,票证类型,罚款(例如:迈克,超速,250.00”。当用户输入要搜索的名称(例如:迈克)时,我找不到...即使迈克,速度,100.00在project.txt中。我觉得它与逗号分隔值有关,但是,我必须以这种方式格式化。代码如下(请很好!我会在工作后清理代码... )

import java.util.*;
import java.io.*;
import java.lang.reflect.Array;
import java.text.Collator;
public class Project {

    public static void main(String[] args) throws IOException {

    String name = "";
    String ticketType = "";
    double fine;
    char quit;
    boolean cont = true;

    while (cont){
    do {
        PrintStream out = new PrintStream(new FileOutputStream("Project.txt", true));
        Scanner input = new Scanner(System.in);

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

    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.next().toLowerCase());

    }
    readArray.close();
    Collections.sort(list, Collator.getInstance()); //sort array alphabetically
    list.forEach(System.out::println); //print array as list ',' separated
    Scanner search = new Scanner(System.in);
    System.out.println("search keyword: ");
    String keyword = search.next().toLowerCase();
    System.out.println("searching for: " + keyword);
    if (list.contains(keyword)){
        System.out.println("found");
    }
    else {
        System.out.println(" not found");
    }
    }
}

2 个答案:

答案 0 :(得分:0)

您只读取文件中的下一个标记,读取整行,如下所示:

while (readArray.hasNext()){
    list.add(readArray.nextLine().toLowerCase());

}

同样可以说:

String keyword = search.nextLine().toLowerCase();

如果您只搜索名称,Arkadly是正确的:

while (readArray.hasNext()){
    String name = readArray.nextLine().toLowerCase().split(",")[0];

    list.add(name);

}

顺便说一下,ArrayList的.contains与String的.contains的工作方式不同。它在ArrayList中查找特定元素: https://www.tutorialspoint.com/java/java_arraylist_class.htm

答案 1 :(得分:0)

正如Arkadiy正确提到的,该列表不包含“mike”,而是“mike,speeding,250.00”。

可能的天真搜索实现是:

  String keyword = search.next().toLowerCase();
  System.out.println("searching for: " + keyword);

  boolean found = false;
  for (String element : list) {
    if (element.contains(keyword)) {
      found = true;
    }
  }

  if (found) {
    System.out.println("found");
  }
  else {
    System.out.println(" not found");
  }
}