Java:添加一个布尔检查方法来检查sql表中要复制到文本文件的数据
我很抱歉这个问题措词不好,但这就是我被绊倒的地方 我有一个数据库表,其中包含有关捐赠者的数据,如身份证、姓名、地址、联系人、34岁以下的人的年龄和血型 我必须将捐献者的血型数据分为两个文本文件,即APositive.txt和BNegative.txt,即A+血型的捐献者的数据将在APositive.txt中,B-血型的捐献者的数据也是如此。应使用布尔方法verifyAgeint,int来验证文本文件中要包含的人员的年龄 以下是我迄今为止所做的工作:Java:添加一个布尔检查方法来检查sql表中要复制到文本文件的数据,java,mysql,boolean,text-files,Java,Mysql,Boolean,Text Files,我很抱歉这个问题措词不好,但这就是我被绊倒的地方 我有一个数据库表,其中包含有关捐赠者的数据,如身份证、姓名、地址、联系人、34岁以下的人的年龄和血型 我必须将捐献者的血型数据分为两个文本文件,即APositive.txt和BNegative.txt,即A+血型的捐献者的数据将在APositive.txt中,B-血型的捐献者的数据也是如此。应使用布尔方法verifyAgeint,int来验证文本文件中要包含的人员的年龄 以下是我迄今为止所做的工作: public class Donor {
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();
}
}
}
您正在使用%d作为您姓名中的年龄:%s地址:%s联系人:%d年龄:%d\n字符串。所以最后一个参数应该是十进制的,但您的verifyAge函数返回布尔值。可能是这样的:
writeToFileFormatter.format("Name: %s Address: %s Contact: %d Age: %d \n", name, address, tel, verifyAge(age, 34)?1:0);
%d需要一个不是布尔值的数字。%d格式正在查找整数值,但您正在传递布尔值是导致该错误的原因 如果要打印布尔值,则为true或false。请将%d替换为%b 请参阅下面的代码片段以修复此问题
class Test{
public static void main(String args[]){
Test a1 = new Test();
boolean a = false;
boolean b = true;
System.out.println(String.format("%b", a));
System.out.println(String.format("%b", b));
}
}@petermm第61行为writeToFileFormatter。格式名称:%s地址:%s联系人:%d年龄:%d\n姓名、地址、电话、verifyAgeage、34;不,这没用。它只输出0或1表示年龄。符合条件的为1,不符合条件的为0。如下:姓名:玛丽地址:阿什顿路联系人:7654222年龄:1姓名:詹姆斯地址:里斯路联系人:7044222年龄:0感谢您的努力。我已经解决了这个问题,谢谢。但是我想打印年龄,不是真的还是假的你想打印年龄吗?如果为false,您希望打印什么?如果为false,即年龄>34岁,则不会接受捐赠者,因此捐赠者的详细信息不会出现在任何文本文件中。不管怎样,我已经解决了这个问题。我的答案对你有帮助吗?很抱歉,我不能这样做,因为我必须打印一个整数而不是布尔值。
class Test{
public static void main(String args[]){
Test a1 = new Test();
boolean a = false;
boolean b = true;
System.out.println(String.format("%b", a));
System.out.println(String.format("%b", b));
}