Check Text file for words that contain same letters as user input

ChockyWockyDoodah :

I have a list of locations that are in a text file. Each of these locations has a check character. Example AAAA = ALPHA, AAA1 = BRAVO, AAAC = DELTA, AAA2 = INDIA. AAAA, AAA1, AAAC and AAA2 are the location strings. I have a list of 1.6 million locations with check characters. The locations are made up of 26 letters A-Z and 0-9.

The first part of the program returns all the check characters that are the same with their locations. Example: user inputs the location ABCD, which has the check character -ALPHA. All the location with the check character -ALPHA are displayed.

The second part of the program, I would like to find all the location which match letters or numbers of the user input. Example user inputs 10AZ which has the check character ALPHA. The program iterates through the text file and finds all the locations which end in ALPHA and contain 0,1,A or Z in their name.

I have tried to split the string using regex and then using contains, but it doesn't achieve what i want it to do. The answer is probably simple but I just cant seem to figure it out, I have also looked at other questions but not found what I'm looking for exactly.

The code I have written is messy but I'm just trying to get a working solution for now. Any advise about is appreciated in regards to run time.

I have tried using an anagram method and tried to make it work for me but I'm just making a mess of the code. I have also tried to store the string in an array and iterate through each index of the array but my result are not correct.

    // The name of the file to open.
    String fileName = "Sn.txt";
    // User input for the location
    String UserInput = "10AZ";
    String CheckCharacter;

    //RC method finds the check character for the location 
    CheckCharacter = Check2.RC(UserInput);

    //getPhonetic gives back the phonetic associated with check character
    String NewCC = Check2.getPhonetic(CheckCharacter); 

    String line = null;
    try {
        FileReader fileReader = 
        new FileReader(fileName);
        // Always wrap FileReader in BufferedReader.
        BufferedReader bufferedReader = 
        new BufferedReader(fileReader);
        int count =0;
        while((line = bufferedReader.readLine()) != null) {
            if(line.endsWith(NewCC)) {
                count++;
                System.out.println(line);
             //System.out.println(isAnagram(UserInput, line) + "USER INPUT");
            }
            //System.out.println(line);
        }   
        System.out.println(count);
        bufferedReader.close();         
    }
    catch(FileNotFoundException ex) {
        System.out.println("Unable to open file '" + fileName + "'");                
    }   

I would like the result like this: User inputs a location 10AZ which ends in Delta. 1. All the locations which end in Delta are displayed - this i have working. 2. All the location which end in Delta and start with 1,0,A and Z. 3. All the location which end in Delta and contain 1,0,A and Z.

Any advise is appreciated, no code is required just a nudge in the right direction.

GJCode :

here some hints that I can't give in comments:

All the location which end in Delta and start with 1,0,A and Z

for this you can use regex, like this:

String input;
// fill input string
input.matches("^[1OAZ]$");

you can replace 1OAZ with any string this basically says or 1 or O or A or Z.

All the location which end in Delta and contain 1,0,A and Z.

you can use contains as you said

String input;
 // fill input string
 if(input.contains("1") || input.contains("O") ...){
 // do something

in this case to retrieve all characters from the string "1OAZ" you can use toCharArray function and iterates over characters or you can use split:

char[] chars = string.toCharArray();
String[] strChars = string.split("");
// iterate over chars
for(...) {
if(input.contains(singleCharHere)
 // do something

of course you can achieve the same result with regex but it is more complex in this case. Hope this is helpful.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=163316&siteId=1