Java 仅填充最后一个 if 语句
Java only populating last if statement
我正在为 class 创建患者跟踪系统。我需要读取文本文件中的每一行数组,然后找到约会日期。从那里我将与今天的日期进行比较,如果它 returns 1(等于今天或将来)那么它将填充未来 jtable。如果它 returns -1 它将需要填充以前的约会 table。它目前正在读取每个日期,但我只能让它用一个前一个日期填充一个 table。我已经尝试了一个 switch case 和 if 语句,但我似乎无法弄清楚。欢迎提出任何建议。
BufferedReader br = new BufferedReader(new FileReader("appointments.txt"));
Scanner s = new Scanner(new File("appointments.txt"));
String line = "";
while ((line = br.readLine()) != null) {
String in = s.nextLine();
String[] sArray = in.split(",");
String day = sArray[7].toString();
SimpleDateFormat parser = new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy");
Date date = parser.parse(day);
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
String formattedDate = formatter.format(date);
Date todayDate = new Date();
Object[][] data = {};
String[] columnNames = {"Date/Time", "Doctor", "Location", "PreAppointment", "Cancel/Reschedule"};
dm2 = new DefaultTableModel(data, columnNames);
table2 = new JTable(dm2);
Object[][] data2 = {};
String[] columnNames1 = {"Date/Time", "Doctor", "Location", "View Details"};
dm = new DefaultTableModel(data2, columnNames1);
table = new JTable(dm);
for (String word : sArray) {
if (word.equals(userInput)) {
System.out.println(formattedDate);
int result = date.compareTo(todayDate);
System.out.println("result: " + result);
if (result == 0) {
dm2.addRow(new Object[]{formattedDate + " " + sArray[8].toString(),
sArray[4].toString(),
sArray[6].toString() + " " + sArray[12].toString(),
"PreAppointment",
"Cancel/Reschedule"});
} else if (result > 0) {
dm2.addRow(new Object[]{formattedDate + " " + sArray[8].toString(),
sArray[4].toString(),
sArray[6].toString() + " " + sArray[12].toString(),
"PreAppointment",
"Cancel/Reschedule"});
} else if (result < 0) {
dm.addRow(new Object[]{formattedDate + " " + sArray[8].toString(),
sArray[4].toString(),
sArray[6].toString() + " / " + sArray[12].toString(),
"Details"});
}
}
}
}
为什么要为您阅读的每一行创建新的 JTable
和新的 DefaultTableModel
?
代码摘录:
while ((line = br.readLine()) != null) {
// [...]
Object[][] data = {};
String[] columnNames = {"Date/Time", "Doctor", "Location", "PreAppointment", "Cancel/Reschedule"};
dm2 = new DefaultTableModel(data, columnNames);
table2 = new JTable(dm2);
Object[][] data2 = {};
String[] columnNames1 = {"Date/Time", "Doctor", "Location", "View Details"};
dm = new DefaultTableModel(data2, columnNames1);
table = new JTable(dm);
// [...]
}
应该是:
Object[][] data = {};
String[] columnNames = {"Date/Time", "Doctor", "Location", "PreAppointment", "Cancel/Reschedule"};
dm2 = new DefaultTableModel(data, columnNames);
table2 = new JTable(dm2);
Object[][] data2 = {};
String[] columnNames1 = {"Date/Time", "Doctor", "Location", "View Details"};
dm = new DefaultTableModel(data2, columnNames1);
table = new JTable(dm);
while ((line = br.readLine()) != null) {
// [...]
}
我正在为 class 创建患者跟踪系统。我需要读取文本文件中的每一行数组,然后找到约会日期。从那里我将与今天的日期进行比较,如果它 returns 1(等于今天或将来)那么它将填充未来 jtable。如果它 returns -1 它将需要填充以前的约会 table。它目前正在读取每个日期,但我只能让它用一个前一个日期填充一个 table。我已经尝试了一个 switch case 和 if 语句,但我似乎无法弄清楚。欢迎提出任何建议。
BufferedReader br = new BufferedReader(new FileReader("appointments.txt"));
Scanner s = new Scanner(new File("appointments.txt"));
String line = "";
while ((line = br.readLine()) != null) {
String in = s.nextLine();
String[] sArray = in.split(",");
String day = sArray[7].toString();
SimpleDateFormat parser = new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy");
Date date = parser.parse(day);
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
String formattedDate = formatter.format(date);
Date todayDate = new Date();
Object[][] data = {};
String[] columnNames = {"Date/Time", "Doctor", "Location", "PreAppointment", "Cancel/Reschedule"};
dm2 = new DefaultTableModel(data, columnNames);
table2 = new JTable(dm2);
Object[][] data2 = {};
String[] columnNames1 = {"Date/Time", "Doctor", "Location", "View Details"};
dm = new DefaultTableModel(data2, columnNames1);
table = new JTable(dm);
for (String word : sArray) {
if (word.equals(userInput)) {
System.out.println(formattedDate);
int result = date.compareTo(todayDate);
System.out.println("result: " + result);
if (result == 0) {
dm2.addRow(new Object[]{formattedDate + " " + sArray[8].toString(),
sArray[4].toString(),
sArray[6].toString() + " " + sArray[12].toString(),
"PreAppointment",
"Cancel/Reschedule"});
} else if (result > 0) {
dm2.addRow(new Object[]{formattedDate + " " + sArray[8].toString(),
sArray[4].toString(),
sArray[6].toString() + " " + sArray[12].toString(),
"PreAppointment",
"Cancel/Reschedule"});
} else if (result < 0) {
dm.addRow(new Object[]{formattedDate + " " + sArray[8].toString(),
sArray[4].toString(),
sArray[6].toString() + " / " + sArray[12].toString(),
"Details"});
}
}
}
}
为什么要为您阅读的每一行创建新的 JTable
和新的 DefaultTableModel
?
代码摘录:
while ((line = br.readLine()) != null) {
// [...]
Object[][] data = {};
String[] columnNames = {"Date/Time", "Doctor", "Location", "PreAppointment", "Cancel/Reschedule"};
dm2 = new DefaultTableModel(data, columnNames);
table2 = new JTable(dm2);
Object[][] data2 = {};
String[] columnNames1 = {"Date/Time", "Doctor", "Location", "View Details"};
dm = new DefaultTableModel(data2, columnNames1);
table = new JTable(dm);
// [...]
}
应该是:
Object[][] data = {};
String[] columnNames = {"Date/Time", "Doctor", "Location", "PreAppointment", "Cancel/Reschedule"};
dm2 = new DefaultTableModel(data, columnNames);
table2 = new JTable(dm2);
Object[][] data2 = {};
String[] columnNames1 = {"Date/Time", "Doctor", "Location", "View Details"};
dm = new DefaultTableModel(data2, columnNames1);
table = new JTable(dm);
while ((line = br.readLine()) != null) {
// [...]
}