compareToIgnoreCase()を使用して、配列を使用せずにエントリを文字列内の部分文字列と比較します
Nov 21 2020
私の課題では、コレクションや配列を使用することは禁じられています。String Tokenizerの使用は許可されていますが、StringとSystem以外のクラスは許可されていません。このソリューションは、任意の数のエントリで機能する必要があります。
私はこのような文字列を持っています:
1|Aaron|Peter|3063543030|[email protected] + "\n"
2|Buffet|Anthony|3063543030|[email protected] + "\n"
3|Dunty|Richard|3063543030|[email protected]
たとえば、エントリが4 | Doe | John | 3063543030 | [email protected]の場合、compareToIgnoreCase()を使用して比較が行われ、エントリは3 | Dunty | Richard | 3063543030 | john @gmailの直前に挿入されます。 com
ここにエントリ名を取得するメソッドがあり、StringTokenizerを使用しています。
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;
}
このメソッドでは、文字列にエントリを挿入し、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;
}
今のところ成功せずにやろうとしているのは、比較の結果が1または0の場合にのみ、エントリを文字列に挿入することです。
誰かが私がその問題を解決するのを手伝ってくれるなら、私は感謝します。
ありがとうございます
回答
1 AlexRudenko Nov 21 2020 at 23:33
連絡先の姓と名前を取得するために、次のヘルパーメソッドが実装されていると仮定します。
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|[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);
出力:
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]
---------