私は3つのクラスを持つプログラムを書いています。このプログラムは、予定をリストしたファイルを読み込み、それらを schedule[] に読み込みます。それらをスケジュールに読み込んだら、それらをソートする必要があります。また、追加の予定を追加したり、予定を検索したりできる必要があります。入力ファイルの最初の行は次のようになります。
2013 年 11 月 10 日 14:00 学生のプログラミング ロジックについて話し合う
ここで、11/10/2013 は date という文字列、14:00 は time という文字列、残りの文は text という文字列です。これらすべてを文字列、文字列、文字列として配列スケジュールに読み込みました
私の先生は、日付と時刻を結合する Appointment クラスに compareTo ステートメントを作成しました。これは、これら 2 つの組み合わせに対して検索と並べ替えを実行する必要があるためです。
Appointment () の compareTo は次のようになります。
public int compareTo(Appointment other)
{
String one, two;
one = this.date + this.time;
two = other.date + other.time;
return one.compareTo(two);
} // end compareTo
Schedule クラスでは、クラス "find" を呼び出し、ユーザーに日付と時刻の入力を求めてから、スケジュールの二分探索() を呼び出して、一致するかどうか、その日付と予定があるかどうかを確認する必要があります。時間を見つけてからfind()に戻り、aptまたはapptの詳細を出力しません。私は以前にこれを行ったことがありますが、日付などの 1 つだけを検索していたとき...私のコードは日付で検索するように書かれています。 (日付 + 時間) そして、私は何かを提出しなければなりませんでした...今、私は正しい方法が何であったかを知りたいだけです!!. 日付の代わりに「一」や「二」を入れてみたり、色々と試してみたのですが… もどかしいです。何も機能しません。誰かが検索でこれをどのように書くべきか教えてください。2 つの問題があります。1. 日付と時刻ではなく、日付のみを検索しています。2. バイナリ検索の compareTo で、「型文字列のメソッド compareTo 文字列は、引数の予定には適用できません」というエラーが表示されます。
私の二分探索メソッドを呼び出す find メソッドは次のとおりです。
private void find()
{
String inDate, inTime;
int position;
Appointment searchArg = new Appointment();
// get info from user
System.out.println ("Enter Date and Time separated by space: ");
inDate = console.next();
inTime = console.next();
position = binarySearch(schedule, numAppointments, searchArg);
//should that last thing be "searchArg"? or "date"? or what??
if (position == -1)
System.out.println(" appointment does not exist");
else
System.out.println("\nAppointment =");
outputOneAppointment(position+1);
}
private int binarySearch(Appointment[] list, int listLength, Appointment searchItem)
{
int first = 0;
int last = listLength - 1;
int mid = 0;
boolean found = false;
while (first <= last && !found)
{
mid = (first + last) / 2;
if (list[mid].date.equals(searchItem))
found = true; //I should not search date but rather the date and time together
else if (list[mid].date.compareTo(searchItem) > 0)
//I also get that error here saying what I wrote above in my question
last = mid - 1;
else
first = mid + 1;
}
if (!found)
mid = -1; //it is an unsuccessful search
return mid;
}//end binarySearch