Javaでエラー処理/例外をスローすることを習得しようとしています。 私はクラスのUserDatabaseクラスに生徒のコレクションを保持しており、そのコレクションをファイルに保存する必要があります。Javaで例外、try/catchブロック、およびシングルトンに苦労しています
私が正しく行っていないと確信しているのは、ファイル処理の方法です。メソッドpublic boolean saveDatabaseは例外をスローしてtry-catchで処理してはならず、Studentオブジェクトのstudentクラスのencodeメソッドを使用して、すべてのオブジェクトをファイルの行として書き込むようにしてください。これはコンパイルされていますが、それは私には見えません。本の中には、メソッドを記述することが記載されています。public boolean saveDatabase();あなたが見ることができるように、私はそれのヘッダーを私に意味をなさせるように変更しました。これは主に、ヘッダが()で終わるメソッドを記述する方法がわからないためです。
メソッドpublic boolean loadDatabaseもIOエラーを処理し、発生した場合はfalseを返します。フィールド内のすべての行について、スーセントクラスからのpublic Student(String encodedStudent)コンストラクターによって生徒オブジェクトを作成する必要があります。ファイル内の行を学生としてデコードできない場合、新しい例外DatabaseFormatException(これは別のクラスです)をスローする必要があります。これもpublic boolean loadDatabase()としてリストされています。本の中で。それに直面してみましょう、これは完全にオフです。私は何をすべきか分からず、何時間も何時間も働いて、本を読んで、オンラインで読んで、私は迷っています。
/**
* This method should not throw exceptions.
* By using a try/catch block, we anticipate possible failures.
* We recognize that these actions might fail and throw errors.
*/
public boolean saveDatabase() throws IOException {
//This method is using the encode method on student objects and should
//write each object as a line in the file.
String encode = null;
boolean saved;
try {
encode = null;
userdatabase.saveDatabase();
saved = false;
}
catch (IOException e) {
System.out.println("Error");
saved = false;
}
finally {
if(encode.equals(students)) {
//System.out.println("Students" + students)?;
saved = true;
}
}
return saved;
}
/**
* Method loadDatabase should handle possible IO errors, and return false
* if one occurs. Otherwise, it should return true and create a new
Student object
* by using the constructor public Student(String encodedStudent).
* If a line cannot be decoded as a student, the method should throw a
new
* exception "DatabaseFormatException".
*
*/
public boolean loadDatabase() throws IOException,DatabaseFormatException {
//Attempting to use the String encodedStudent constructor from Student class
String encodedStudent = null;
boolean loaded;
try {
//Attempting to create possible IO errors returning false if they occur
enco dedStudent = null;
//UserDatabase.loadDatabase(encodedStudent);
loaded = false;
}
catch(IOException e) {
if (encodedStudent == null) {
System.out.println("Error");
loaded = false;
}
}
//Trying a for each loop to throw a DatabaseFormatException
for(Student student : students) {
//This is supposed to throw a DatabaseFormatException if a
//line in the file cannot be decoded as a student!
if(student.getName.isEmpty() && this.course.isEmpty()) {
throw new DatabaseFormatException(
"No student found");
}
}
ありがとうございます!コードはエラーなしでコンパイルされます。しかし、このタスクでは、生徒オブジェクトに対してencodeメソッドを使用し、すべてのオブジェクトをファイルの1行として記述するように具体的に言います。 (UserDatabaseクラスは生徒のコレクションを保持しており、コレクションをファイルに保存することもできます)。どのように私はこれを正常に行うことができますか? – Angelica
@Angelica私はそれのようなものを考え出した。それは "あなたの仕事"です。コメントを追加した 'try'ブロック内のすべてのことを行う必要があります。私は" userdatabase "やあなたがアクセスできる" encode "メソッドを知らない。そして、私は本当にそれらについて知りたいとは思っていません。私はちょうどtry-catchをユーザーにする方法と何を返すべきかに関する一般的な考えを伝えたいと思っていました。 – luk2302
さて、わかりました。しかし、ありがとう - それは今私にとってもっと理にかなっている! – Angelica