我必须用逗号分隔的一行中的用户输入。我已将该输入保存在数组列表中。现在我想以下面的方式搜索这些数据......怎么做?
我想以这种方式搜索:
输入A - Z中的任何字母:
<A>
请选择一个范围:
Aa-Ac |广告广告| Ae-Al | Am-Am | An-Ao | Ap-Ar | As-At | Au-Aw |斧-AZ
<Aa-Ac>
A-&GT; AA-AC:
现在将打印此范围内的数据。 这是我已经完成的事情
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;
import javax.lang.model.element.Element;
public class practice {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
System.out.println(" Welcome to Your Own A-Z Drug Guide");
System.out.println("Press 1 to enter new drugs");
System.out.println("Press 2 for browsing");
System.out.println("Press 3 for exiting");
int choice = sc.nextInt();
switch(choice){
case 1:
System.out.println("Please enter drugs name");
String drugsName = sc.next();
String[] drugs = drugsName.split("\\,");
Arrays.sort(drugs);
List<String> list = Arrays.asList(drugs);
System.out.println(list);
System.out.println(matches(list, "aa"));
break;
case 2:
System.out.println(" Enter any alphabet from A – Z:");
String alphabet = sc.nextLine();
System.out.println("Please select a range");
if(alphabet=="a"){
System.out.println("Aa-Ac | Ad-Ad | Ae-Al | Am-Am | An-Ao | Ap-Ar | As-At | Au-Aw | Ax-Az");
String range = sc.nextLine();
//System.out.println(matches(list, "aa"));
break;
}
}
}
public static List<String> matches (List<String> dataList, String matchStr){
List<String> result = new ArrayList<String>();
if (matchStr != null) {
matchStr = matchStr.toLowerCase();
for(String d : dataList){
if (d.toLowerCase().contains(matchStr)) {
result.add(d);
}
}
}
return result;
}
}