Rok Ivartnik
Rok Ivartnik

Reputation: 149

java binary search from array list, lexicon words

My code reads a file called sort.txt, in which there are lexicon words ordered alphabetically and by length. There is one word in each line. The program works fine and please don't comment how it is written. The user then inputs a word he is searching for, e.g. "C**", and program returns all possible matches (Car, Cat, Cam, etc.). My question is how to search the array using binary search to speed things up. But the binary search would only be used if the first or first two or first 3 characters were input by the user, for instance "Ca*" or "Mou**". If the user inputs "***se" for instance, then the program would skip the binary search and would search the entire array.

package test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;

public class main{

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

    ArrayList list1 = new ArrayList();
    ArrayList list2 = new ArrayList();
    ArrayList list3 = new ArrayList();
    ArrayList list4 = new ArrayList();
    ArrayList list5 = new ArrayList();
    ArrayList list6 = new ArrayList();
    ArrayList list7 = new ArrayList();
    ArrayList list8 = new ArrayList();
    ArrayList list9 = new ArrayList();
    ArrayList list10plus = new ArrayList();

    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() == 1) {
                list1.add(vrstica);
            }
            if (vrstica.length() == 2) {
                list2.add(vrstica);
            }
            if (vrstica.length() == 3) {
                list3.add(vrstica);
            }
            if (vrstica.length() == 4) {
                list4.add(vrstica);
            }
            if (vrstica.length() == 5) {
                list5.add(vrstica);
            }
            if (vrstica.length() == 6) {
                list6.add(vrstica);
            }
            if (vrstica.length() == 7) {
                list7.add(vrstica);
            }
            if (vrstica.length() == 8) {
                list8.add(vrstica);
            }
            if (vrstica.length() == 9) {
                list9.add(vrstica);
            }
            if (vrstica.length() > 9) {
                list10plus.add(vrstica);
            }
        }
        do{
            do {
                System.out.println("Vnesi dožino besede, ki jo iščeš:");
                if (in.hasNextInt()) {
                    dolzina = in.nextInt();
                } else if (in.hasNextLine()) {
                    System.out.printf("Napačen vnos! Poskusi ponovno:%n ",
                            in.nextLine());
                } 
            } while (dolzina <= 0);



        System.out.println("Vnesi besedo za neznano črko vpiši * :");
        vnos = input.nextLine();
        vnos = vnos.replace("*", ".");

        if (dolzina == 1) {
            for (int i = 0; i < list1.size(); i++) {
                String s = (String) list1.get(i);
                if (s.matches(vnos))
                    System.out.println(s);
            }

        }

        if (dolzina == 2) {
            for (int i = 0; i < list2.size(); i++) {
                String s = (String) list2.get(i);
                if (s.matches(vnos))
                    System.out.println(s);
            }

        }
        if (dolzina == 3) {

            for (int i = 0; i < list3.size(); i++) {
                String s = (String) list3.get(i);
                if (s.matches(vnos))
                    System.out.println(s);
            }
        }
        if (dolzina == 4) {

            for (int i = 0; i < list4.size(); i++) {
                String s = (String) list4.get(i);
                if (s.matches(vnos))
                    System.out.println(s);
            }
        }
        if (dolzina == 5) {
            for (int i = 0; i < list5.size(); i++) {
                String s = (String) list5.get(i);
                if (s.matches(vnos))
                    System.out.println(s);
            }
        }
        if (dolzina == 6) {
            for (int i = 0; i < list6.size(); i++) {
                String s = (String) list6.get(i);
                if (s.matches(vnos))
                    System.out.println(s);
            }
        }
        if (dolzina == 7) {
            for (int i = 0; i < list7.size(); i++) {
                String s = (String) list7.get(i);
                if (s.matches(vnos))
                    System.out.println(s);
            }
        }
        if (dolzina == 8) {
            for (int i = 0; i < list8.size(); i++) {
                String s = (String) list8.get(i);
                if (s.matches(vnos))
                    System.out.println(s);
            }
        }
        if (dolzina == 9) {
            for (int i = 0; i < list9.size(); i++) {
                String s = (String) list9.get(i);
                if (s.matches(vnos))
                    System.out.println(s);
            }
        }
        if (dolzina > 9) {
            for (int i = 0; i < list10plus.size(); i++) {
                String s = (String) list10plus.get(i);
                if (s.matches(vnos))
                    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();

    }
}}

Upvotes: 0

Views: 420

Answers (1)

RP-
RP-

Reputation: 5837

This won't give you a complete answer, but just puts in that direction.

You need to check if the first char is not *, then do a binary search otherwise iterate over all the strings and do String.endsWith().

if(vnos.charAt(0) != '*') { //do binary search with the substring } else { //iterate and check if the string endsWith given suffix. }

Upvotes: 1

Related Questions