この質問の悪い言い訳を申し訳なく思っていますが、ここで私は固執しています。Java:ブールチェックメソッドを追加してSQLテーブルからデータをチェックしてテキストファイルにコピーする
ID、名前、住所、連絡先、年齢(34歳以下の人)、血液型などのドナーに関するデータを含むデータベーステーブルがあります。
ドナーのデータを血液群に基づいて2つのテキストファイル、すなわちAPositive.txt
とBNegative.txt
にソートする必要があります。つまり、A +血液型グループのデータはAPositive.txtにあり、同じことがB-血液型グループになります。ブールメソッドverifyAge(int, int)
を使用して、テキストファイルに含める人の年齢を確認する必要があります。
EDIT:
以下
public class Donor {
public static boolean verifyAge(int age, int maxAge) {
if (age <= maxAge)
return true;
else
return false;
}
private static Formatter writeToFileFormatter;
private static Formatter writeToFileFormatter1;
public static void main(String[] args) {
try {
writeToFileFormatter = new Formatter("APositive.txt");
writeToFileFormatter1 = new Formatter("BNegative.txt");
}
catch(SecurityException se) {
System.out.println("Access Denied");
System.exit(1);
}
catch(FileNotFoundException fnfe) {
System.out.println("Error creating file");
System.exit(1);
}
try {
Connection con = null;
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/examques?autoReconnect=true&useSSL=false", "root", "admin");
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("Select * from donor");
while (rs.next()) {
int id = rs.getInt("donor_id");
String name = rs.getString("name");
String address = rs.getString("address");
int tel = rs.getInt("tel");
int age = rs.getInt("age");
String blood_grp = rs.getString("blood_group");
if (blood_grp.equals("A+")) {
writeToFileFormatter.format("Name: %s Address: %s Contact: %d Age: %d \n", name, address, tel, verifyAge(age, 34));
}
if (blood_grp.equals("B-")) {
writeToFileFormatter1.format("Name: %s Address: %s Contact: %d Age: %d \n", name, address, tel, verifyAge(age, 34));
}
}
if (writeToFileFormatter != null) {
writeToFileFormatter.close();
}
if (writeToFileFormatter1 != null) {
writeToFileFormatter1.close();
}
}
catch(Exception e) {
e.printStackTrace();
}
}
}
は私が取得していますエラーのスクリーンショットです:ここで は、私がこれまでにやっていることですすべての回答ありがとう
私はそれをこのように解決した:
if(blood_grp.equals("A+")&& verifyAge(age, 34)==true){
writeToFileFormatter.format("Name: %s Address: %s Contact: %d Age: %d \n", name, address, tel,age);
}
if(blood_grp.equals("B-")&&verifyAge(age, 34)==true){
writeToFileFormatter1.format("Name: %s Address: %s Contact: %d Age: %d \n", name, address, tel, age);
}
@PeterMmmライン61は、 'writeToFileFormatter.format(「名前で、それを修正するには、以下のこのスニペットを参照することによって%dを交換してください:%d年齢:%d \ n "、名前、住所、電話番号、verifyAge(年齢、34)); ' – Tia