Search code examples
javastringsubstringcompareto

Using compareToIgnoreCase() to compare an entry to substring in string without using array


In my assignment it's forbidden to use collections and any arrays. We are allowed to use String Tokenizer but any other classes than String and System are not allowed. This solution has to work with any number of entries.

I have a string which look like this :

1|Aaron|Peter|3063543030|[email protected] + "\n" 
2|Buffet|Anthony|3063543030|[email protected] + "\n" 
3|Dunty|Richard|3063543030|[email protected] 

For example, if the entry is 4|Doe|John|3063543030|[email protected] then the comparison will be make using compareToIgnoreCase() and the entry will be insert just before 3|Dunty|Richard|3063543030|[email protected]

Here I have a method that get the entry name and it's using String Tokenizer :

 public static String obtenirNomContact (String contactLigne) {
        StringTokenizer tokenizer = new StringTokenizer(contactLigne, "|");
        String id = tokenizer.nextToken();
        String nom = tokenizer.nextToken();
        String prenom = tokenizer.nextToken();


        return nom;
    }

In this method I insert the entry in the string and make the comparison using compareToIgnoreCaseMethod()

 public static String insertEntryInString
        (String myString, String entry) {
            int result = 0;
            String entryName = "";
            String myString = "";
            String entry = "";
            String nameInString = "";
    
            if (myString != null) {
                myString += entry + "\n";
    
                do {
                    entryName = getContactName(entry);
                    nameInString = getContactName(myString);
    
                    result = entryName.compareToIgnoreCase(nameInString);
                    if (result < 0) {
    
                        entry += entryName + "\n";
                        entry += nameInString + "\n";
                    } else {
                        entry += nameInString  + "\n";
                        entry += entryName + "\n";
                    }
    
                } while (result > 0);
                myString += entry + "\n";
                System.out.println(myString);
            }
          
            return myString;  
        }

What I'm trying to do without any success for now is to insert the entry in string only if the result of the comparison is equal to 1 or 0.

I would appreciate if someone could help me resolve that problem.

Thank's


Solution

  • Let's assume the following helper methods are implemented to get surname and name of the contact:

    static String getSurname(String contact) {
        StringTokenizer st = new StringTokenizer(contact, "|");
        st.nextToken(); // skip id
        return st.nextToken();
    }
    
    static String getName(String contact) {
        StringTokenizer st = new StringTokenizer(contact, "|");
        st.nextToken(); // skip id
        st.nextToken(); // skip surname
        return st.nextToken();
    }
    

    Then the method to insert a new contact into sorted "list" of contacts separated with '\n' may be rewritten as follows:

    private static final String NL = "\n";
    
    static String insertContact(String contact, String data) {
        String newSurname = getSurname(contact);
        String newName = getName(contact);
        
        StringTokenizer st = new StringTokenizer(data, NL);
        StringBuilder sb = new StringBuilder();
        boolean inserted = false;
        
        while (st.hasMoreTokens()) {
            String curr = st.nextToken();
            String currSurname = getSurname(curr);
            String currName = getName(curr);
            
            if (!inserted && (currSurname.compareToIgnoreCase(newSurname) > 0 || (currSurname.compareToIgnoreCase(newSurname) == 0 && currName.compareToIgnoreCase(newName) > 0))) {
                inserted = true;
                System.out.println("Inserting before " + curr);
    
                sb.append(sb.length() > 0 ? NL : "").append(contact);
            }
            sb.append(sb.length() > 0 ? NL : "").append(curr);
        }
        if (!inserted) {
            sb.append(sb.length() > 0 ? NL : "").append(contact);
        }
        System.out.println("Data:\n" + sb);
        System.out.println("---------");
        return sb.toString();
    }
    

    Test:

    String data = "1|Abercrombie|Peter|3063543030|[email protected]\n" 
                + "2|Buffet|Anthony|3063543030|[email protected]\n" 
                + "3|Dunty|Richard|3063543030|[email protected]";
    
    data = insertContact("4|Doe|John|3063543030|[email protected]", data);
    data = insertContact("5|Aaron|Paul|5551234567|[email protected]", data);
    data = insertContact("6|Gilligan|Vince|5559123456|[email protected]", data);
    

    Output:

    Inserting before 3|Dunty|Richard|3063543030|[email protected]
    Data:
    1|Abercrombie|Peter|3063543030|[email protected]
    2|Buffet|Anthony|3063543030|[email protected]
    4|Doe|John|3063543030|[email protected]
    3|Dunty|Richard|3063543030|[email protected]
    ---------
    Inserting before 1|Abercrombie|Peter|3063543030|[email protected]
    Data:
    5|Aaron|Paul|5551234567|[email protected]
    1|Abercrombie|Peter|3063543030|[email protected]
    2|Buffet|Anthony|3063543030|[email protected]
    4|Doe|John|3063543030|[email protected]
    3|Dunty|Richard|3063543030|[email protected]
    ---------
    Data:
    5|Aaron|Paul|5551234567|[email protected]
    1|Abercrombie|Peter|3063543030|[email protected]
    2|Buffet|Anthony|3063543030|[email protected]
    4|Doe|John|3063543030|[email protected]
    3|Dunty|Richard|3063543030|[email protected]
    6|Gilligan|Vince|5559123456|[email protected]
    ---------