java—使用comparetoignorecase()将条目与字符串中的子字符串进行比较,而不使用数组

pqwbnv8z  于 2021-07-11  发布在  Java
关注(0)|答案(1)|浏览(341)

在我的作业中,禁止使用集合和任何数组。我们可以使用字符串标记器,但是除了string和system之外的任何其他类都是不允许的。此解决方案必须处理任意数量的条目。
我有一个字符串,看起来像这样:

1|Aaron|Peter|3063543030|john@gmail.com + "\n" 
2|Buffet|Anthony|3063543030|john@gmail.com + "\n" 
3|Dunty|Richard|3063543030|john@gmail.com

例如,如果条目是4 | doe | john | 3063543030|john@gmail.com 然后使用comparetoignorecase()进行比较,并在3 | dunty | richard | 3063543030之前插入条目|john@gmail.com
这里我有一个获取条目名称的方法,它使用字符串标记器:

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;
    }

在这个方法中,我在字符串中插入条目,并使用compareTognoreCaseMethod()进行比较

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;  
        }

我现在要做的是在比较结果等于1或0时才在字符串中插入条目,但没有成功。
如果有人能帮我解决这个问题,我将不胜感激。
谢谢

hs1ihplo

hs1ihplo1#

假设实现了以下helper方法来获取联系人的姓氏和姓名:

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();
}

然后将新联系人插入到按 '\n' 可重写如下:

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();
}

测试:

String data = "1|Abercrombie|Peter|3063543030|john@gmail.com\n" 
            + "2|Buffet|Anthony|3063543030|john@gmail.com\n" 
            + "3|Dunty|Richard|3063543030|john@gmail.com";

data = insertContact("4|Doe|John|3063543030|john@gmail.com", data);
data = insertContact("5|Aaron|Paul|5551234567|apaul@breakingbad.com", data);
data = insertContact("6|Gilligan|Vince|5559123456|vinceg@breakingbad.com", data);

输出:

Inserting before 3|Dunty|Richard|3063543030|john@gmail.com
Data:
1|Abercrombie|Peter|3063543030|john@gmail.com
2|Buffet|Anthony|3063543030|john@gmail.com
4|Doe|John|3063543030|john@gmail.com
3|Dunty|Richard|3063543030|john@gmail.com
---------
Inserting before 1|Abercrombie|Peter|3063543030|john@gmail.com
Data:
5|Aaron|Paul|5551234567|apaul@breakingbad.com
1|Abercrombie|Peter|3063543030|john@gmail.com
2|Buffet|Anthony|3063543030|john@gmail.com
4|Doe|John|3063543030|john@gmail.com
3|Dunty|Richard|3063543030|john@gmail.com
---------
Data:
5|Aaron|Paul|5551234567|apaul@breakingbad.com
1|Abercrombie|Peter|3063543030|john@gmail.com
2|Buffet|Anthony|3063543030|john@gmail.com
4|Doe|John|3063543030|john@gmail.com
3|Dunty|Richard|3063543030|john@gmail.com
6|Gilligan|Vince|5559123456|vinceg@breakingbad.com
---------

相关问题