mercredi 29 mars 2017

How to modify the for() loop or if() statements to replace multiple characters in a String in my case?

My input String uid contains 10 characters, first 4 chars are “A~Z” or "0~9", last 6 chars are "0~9". I am doing fuzzy query in HBase. Following is my code. If the input String uid contains only one '?', no mater where the '?' is, it works well. For example, if uid="A01A000?00", the results are "A01A000000","A01A000100,"A01A000200""……"A01A000900",pretty good.

However, if uid contains two or more '?' in different digits, it fails. For example, if uid= "A01A000?0?", the results are "A01A000000", "A01A000101", "A01A000202", "A01A000303"…… "A01A000909", "A01A000000","A01A000101", "A01A000202", "A01A000303"…… "A01A000909"

acutually, I want the two '?' change like 00,01,02……09,10,11,12……19……90,91,92……99, not 11,22,33,44……99 like above

List<Get> gets = new ArrayList<>();  
for (String uid : uidsArr) {

    for(int k= 0; k<4 ; k++){
        if(uid.charAt(k) == '?'){
            for (int i =0; i<26; i++){
                get = new Get((uid.replace(uid.charAt(k),(char)('A' + i))).getBytes());  
                    get.setMaxVersions(versions);  
                    gets.add(get);  
            }
            for (int i =0; i<10; i++){
                get = new Get((uid.replace(uid.charAt(k),(char)(i + '0'))).getBytes());  
                    get.setMaxVersions(versions);  
                    gets.add(get);  
            }
        }   
    }

    for(int k=4; k<10; k++ ){
        if(uid.charAt(k) == '?'){
            for (int i =0; i<10; i++){
                get = new Get((uid.replace(uid.charAt(k), (char) (i + '0'))).getBytes());  
                    get.setMaxVersions(versions);  
                    gets.add(get);  
            }
        }
    }


    get = new Get(uid.trim().getBytes());  
    get.setMaxVersions(versions);  
    gets.add(get);  
}  

Result[] results = table.get(gets);  

Aucun commentaire:

Enregistrer un commentaire