java二进制搜索使用正则表达式在arraylist中多次出现

时间:2014-11-06 17:24:11

标签: java regex arraylist

下面你可以看到我的代码。它从sort.txt读取长度为3的单词,并将它们保存到arraylist中。然后要求用户输入他正在搜索的单词。用户还可以输入匹配来自a-z

的任何可能字母的“*”

例如:

My list: [AKP, ALU, ALU, AMD, AMZ, APP, APZ, ARD, AZS, AZS, Ada, Adi, Amy, Ana, Arh, Arh,]

用户输入:ALU

程序返回在1

处找到的ALU

所以这部分工作,这是问题所在。

列表是一样的。 用户输入:A **

所以程序应该返回:

AKP found at 0

ALU found at 1

ALU found at 2

AMD found at 3 

等等

基本上所有单词都以A开头。

如果有人可以帮助我,那么在这里过去2小时会很棒。

public class proba{


private final static int NOT_FOUND = -1;

public static void main(String[] args) {
    String izbira;
    int dolzina=0;
    Scanner in = new Scanner(System.in);
    String user_input;
    Scanner input = new Scanner(System.in);

    List<String> list3 = new ArrayList<String>();


    try {

        File file = new File("sort.txt");
        FileReader fileReader = new FileReader(file);
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        String vrstica;

        while ((vrstica = bufferedReader.readLine()) != null) {

            if (vrstica.length() == 3) {
                list3.add(vrstica);

            }
        }
        System.out.println(list3);
        do{
            do {
                System.out.println("Enter lenght of word:");
                if (in.hasNextInt()) {
                    dolzina = in.nextInt();
                } else if (in.hasNextLine()) {
                    System.out.printf("Wrong entry!%n ",
                            in.nextLine());
                } 
            } while (dolzina <= 0);

        Collections.sort(list3);

        System.out.println("Enter a word for unknown character enter * :");
        user_input = input.nextLine();
        user_input = user_input.replace("*", ".");

        System.out.println("Sorted list: [length: " + list3.size() + "]");
        System.out.println(list3);


        if (dolzina == 3) {
            if(user_input.charAt(0) != '*') {
                int index = binarySearch(list3, user_input);
                System.out.println("Found" + user_input +" at " + index);
            }
            else 
            for (int i = 0; i < list3.size(); i++) {
                String s = (String) list3.get(i);
                if (s.matches(user_input))
                    System.out.println(s);
            }

        }

        dolzina=-1;
        System.out.println("Ponovni vnos (da/ne):");
        Scanner inn= new Scanner (System.in);
        izbira = inn.next();

    }while (izbira.equalsIgnoreCase("da"));
        bufferedReader.close();
    } catch (IOException e) {
        e.printStackTrace();

    }
}


public static int binarySearch(List<String> integerList, String searchValue) {

int low = 0;
int high = integerList.size() - 1;
int mid = (low + high) / 2;

while (low <= high && !integerList.get(mid).equalsIgnoreCase(searchValue)) {

    if (integerList.get(mid).compareTo(searchValue) < 0) {
        low = mid + 1;
    } else {
        high = mid - 1;
    }

    mid = (low + high) / 2;

    if (low > high) {
        mid = NOT_FOUND;
    }

}
return mid;

 }

 }

0 个答案:

没有答案