实施可比 java
Implementing comparable java
我正在尝试使用类似界面按日期对用户输入的任务和日期列表进行排序。我在外部 .txt 文件中有任务,如果我正在访问正确的变量,我会有点困惑。该程序编译,但当我尝试对任务进行排序时,它似乎删除了文件的内容。这是我拥有的:
public class DueDate implements Comparable<DueDate>{
public String addedTask = "";
public String enteredDueDate;
public DueDate(String addedTask, String dueDate){
this.addedTask = addedTask;
this.enteredDueDate = enteredDueDate;
}
public String toString(){
return addedTask+"\t"+enteredDueDate+"\t";
}
@Override
public int compareTo(DueDate o) {
return this.enteredDueDate.compareTo(o.enteredDueDate);
}
}
public class Main {
public static String fileName = "/Users/eringray/Desktop/tasklistjava/src/javatask.txt";
public static void main(String[] args) throws IOException {
int menuItem = -1;
while(menuItem != 0){
menuItem = menu();
switch (menuItem){
case 1:
showTaskList();
break;
case 2:
addTask();
break;
case 3:
sortList();
case 4:
deleteTasks();
break;
case 0:
break;
default:
System.out.println("Invalid Input");
}
}
}
static int menu(){
int choice;
Scanner sc = new Scanner(System.in);
System.out.println("\n Task List Menu \n");
System.out.println("0: Exit Menu");
System.out.println("1: Show Tasks in List");
System.out.println("2: Add Task to List");
System.out.println("3: Sort Tasks by Due Date");
System.out.println("4: Delete Tasks");
System.out.println();
System.out.println("Enter a choice: ");
choice = sc.nextInt();
return choice;
}
static void showTaskList(){
System.out.println("\nTask List\n");
try {
Scanner inFile = new Scanner(new FileReader(fileName));
String line;
int number = 1;
while(inFile.hasNextLine()){
line = inFile.nextLine();
System.out.println(number + ". " + line);
++number;
}
System.out.println();
inFile.close();
} catch (FileNotFoundException ioe) {
System.out.println(ioe);
}
}
static void addTask(){
System.out.println("\nAdd Task\n");
try {
Scanner input = new Scanner(System.in);
PrintWriter outFile = new PrintWriter(new FileWriter(fileName, true));
System.out.println("Enter a Task: ");
String addedTask = input.nextLine();
System.out.println("Set Due Date for this Task(yyyy-mm-dd): ");
String dueDate = input.nextLine();
outFile.println(addedTask + "\t" + dueDate);
outFile.close();
} catch (IOException ioe) {
System.out.println(ioe);
}
}
static void sortList() throws IOException {
System.out.println("\nSorted List\n");
try {
BufferedReader br = new BufferedReader(new FileReader(fileName));
BufferedWriter bw = new BufferedWriter(new FileWriter(fileName, true));
ArrayList<DueDate> tasks = new ArrayList<DueDate>();
String line = "";
while((line = br.readLine()) != null) {
String[] values = line.split("\t");
if(values.length == 2) {
String addedTask = values[0];
String enteredDueDate = values[1];
DueDate d = new DueDate(addedTask, enteredDueDate);
tasks.add(d);
}
}
Collections.sort(tasks);
for(int i = 0; i < tasks.size(); i++){
DueDate date = tasks.get(i);
String lineText = date.toString();
bw.write(lineText);
bw.newLine();
}
br.close();
bw.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
private static void deleteTasks(){
PrintWriter writer = null;
try {
writer = new PrintWriter(fileName);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
assert writer != null;
writer.print("");
writer.close();
}
}
对于额外的代码,我们深表歉意,但我认为它可能会帮助您了解我正在尝试做的事情。任何建议都会很棒。我是Java的新手,所以请耐心等待!谢谢!
问题是您的 switch
中缺少 break
语句,该语句在调用 sortList()
之后调用 deleteTasks()
方法。将您的代码更改为此,它应该可以正常工作:
int menuItem = -1;
while(menuItem != 0){
menuItem = menu();
switch (menuItem){
case 1:
showTaskList();
break;
case 2:
addTask();
break;
case 3:
sortList();
break; //The missing break
case 4:
deleteTasks();
break;
case 0:
break;
default:
System.out.println("Invalid Input");
}
}
并且 DueDate
class 的构造函数缺少 enteredDueDate
变量的赋值,因为参数被命名为 dueDate
而不是 enteredDueDate
。你应该把它改成这样:
public DueDate(String addedTask, String enteredDueDate){
this.addedTask = addedTask;
this.enteredDueDate = enteredDueDate;
}
因为您当前正在将成员变量分配给它自己的值。
您还应该考虑更改 sortList
方法,尤其是 BufferedWriter
,否则每次调用它时都会复制列表。像这样的东西可以做到:
BufferedReader br = new BufferedReader(new FileReader(fileName));
ArrayList<DueDate> tasks = new ArrayList<DueDate>();
String line = "";
while((line = br.readLine()) != null) {
String[] values = line.split("\t");
if(values.length == 2) {
String addedTask = values[0];
String enteredDueDate = values[1];
DueDate d = new DueDate(addedTask, enteredDueDate);
tasks.add(d);
}
}
Collections.sort(tasks);
br.close();
//Changed it to not append but overwrite the old file so it only contains the sorted list
BufferedWriter bw = new BufferedWriter(new FileWriter(fileName, false));
for (DueDate date : tasks) {
String lineText = date.toString();
bw.write(lineText);
bw.newLine();
}
bw.flush();
bw.close();
编辑:要打印排序列表,您可以做一些事情。
最简单的方法可能是将 showTaskList
方法的调用附加到 sortList
方法的末尾,例如
//...
bw.flush();
bw.close();
showTaskList();
} catch (FileNotFoundE
e.printStackTrace(
}
或者您可以遍历 ArrayList
并像这样打印出来:
//...
bw.flush();
bw.close();
for (int i = 0; i < tasks.size(); i++) {
DueDate dueDate = tasks.get(i);
System.out.println(i+". "+dueDate.toString());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
编辑 2:排序时将所有 Tasks
和空 DueDate
放到底部的最简单方法是更改 compareTo
DueDate
class:
中的方法
@Override
public int compareTo(DueDate o) {
return -this.enteredDueDate.compareTo(o.enteredDueDate);
}
只需取反,所有条目都会降序排列,空 DueDate
的条目将排在列表的末尾(它依赖于默认情况下字符串的比较方式)。
但是如果你想使用没有截止日期的 Tasks
你会 运行 在你的 sortList
方法中遇到问题,因为 split("\t")
方法只会 return长度为 1 的数组和您的 if
条件将失败。一种解决方法如下所示:
//...
while ((line = br.readLine()) != null) {
String[] values = line.split("\t");
//To ensure it's still valid data
if (values.length >= 1 && values.length <= 2) {
String addedTask = values[0];
String enteredDueDate;
//Check whether dueDate has a value or is empty
if (values.length == 1)
enteredDueDate = "";
else
enteredDueDate = values[1];
DueDate d = new DueDate(addedTask, enteredDueDate);
tasks.add(d);
}
}
//...
或更紧凑的版本(做完全相同的事情)
//...
while ((line = br.readLine()) != null) {
String[] values = line.split("\t");
if (values.length >= 1 && values.length <= 2)
tasks.add(new DueDate(values[0], values.length == 1 ? "" : values[1]));
}
//...
希望这对您有所帮助 (:
我正在尝试使用类似界面按日期对用户输入的任务和日期列表进行排序。我在外部 .txt 文件中有任务,如果我正在访问正确的变量,我会有点困惑。该程序编译,但当我尝试对任务进行排序时,它似乎删除了文件的内容。这是我拥有的:
public class DueDate implements Comparable<DueDate>{
public String addedTask = "";
public String enteredDueDate;
public DueDate(String addedTask, String dueDate){
this.addedTask = addedTask;
this.enteredDueDate = enteredDueDate;
}
public String toString(){
return addedTask+"\t"+enteredDueDate+"\t";
}
@Override
public int compareTo(DueDate o) {
return this.enteredDueDate.compareTo(o.enteredDueDate);
}
}
public class Main {
public static String fileName = "/Users/eringray/Desktop/tasklistjava/src/javatask.txt";
public static void main(String[] args) throws IOException {
int menuItem = -1;
while(menuItem != 0){
menuItem = menu();
switch (menuItem){
case 1:
showTaskList();
break;
case 2:
addTask();
break;
case 3:
sortList();
case 4:
deleteTasks();
break;
case 0:
break;
default:
System.out.println("Invalid Input");
}
}
}
static int menu(){
int choice;
Scanner sc = new Scanner(System.in);
System.out.println("\n Task List Menu \n");
System.out.println("0: Exit Menu");
System.out.println("1: Show Tasks in List");
System.out.println("2: Add Task to List");
System.out.println("3: Sort Tasks by Due Date");
System.out.println("4: Delete Tasks");
System.out.println();
System.out.println("Enter a choice: ");
choice = sc.nextInt();
return choice;
}
static void showTaskList(){
System.out.println("\nTask List\n");
try {
Scanner inFile = new Scanner(new FileReader(fileName));
String line;
int number = 1;
while(inFile.hasNextLine()){
line = inFile.nextLine();
System.out.println(number + ". " + line);
++number;
}
System.out.println();
inFile.close();
} catch (FileNotFoundException ioe) {
System.out.println(ioe);
}
}
static void addTask(){
System.out.println("\nAdd Task\n");
try {
Scanner input = new Scanner(System.in);
PrintWriter outFile = new PrintWriter(new FileWriter(fileName, true));
System.out.println("Enter a Task: ");
String addedTask = input.nextLine();
System.out.println("Set Due Date for this Task(yyyy-mm-dd): ");
String dueDate = input.nextLine();
outFile.println(addedTask + "\t" + dueDate);
outFile.close();
} catch (IOException ioe) {
System.out.println(ioe);
}
}
static void sortList() throws IOException {
System.out.println("\nSorted List\n");
try {
BufferedReader br = new BufferedReader(new FileReader(fileName));
BufferedWriter bw = new BufferedWriter(new FileWriter(fileName, true));
ArrayList<DueDate> tasks = new ArrayList<DueDate>();
String line = "";
while((line = br.readLine()) != null) {
String[] values = line.split("\t");
if(values.length == 2) {
String addedTask = values[0];
String enteredDueDate = values[1];
DueDate d = new DueDate(addedTask, enteredDueDate);
tasks.add(d);
}
}
Collections.sort(tasks);
for(int i = 0; i < tasks.size(); i++){
DueDate date = tasks.get(i);
String lineText = date.toString();
bw.write(lineText);
bw.newLine();
}
br.close();
bw.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
private static void deleteTasks(){
PrintWriter writer = null;
try {
writer = new PrintWriter(fileName);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
assert writer != null;
writer.print("");
writer.close();
}
}
对于额外的代码,我们深表歉意,但我认为它可能会帮助您了解我正在尝试做的事情。任何建议都会很棒。我是Java的新手,所以请耐心等待!谢谢!
问题是您的 switch
中缺少 break
语句,该语句在调用 sortList()
之后调用 deleteTasks()
方法。将您的代码更改为此,它应该可以正常工作:
int menuItem = -1;
while(menuItem != 0){
menuItem = menu();
switch (menuItem){
case 1:
showTaskList();
break;
case 2:
addTask();
break;
case 3:
sortList();
break; //The missing break
case 4:
deleteTasks();
break;
case 0:
break;
default:
System.out.println("Invalid Input");
}
}
并且 DueDate
class 的构造函数缺少 enteredDueDate
变量的赋值,因为参数被命名为 dueDate
而不是 enteredDueDate
。你应该把它改成这样:
public DueDate(String addedTask, String enteredDueDate){
this.addedTask = addedTask;
this.enteredDueDate = enteredDueDate;
}
因为您当前正在将成员变量分配给它自己的值。
您还应该考虑更改 sortList
方法,尤其是 BufferedWriter
,否则每次调用它时都会复制列表。像这样的东西可以做到:
BufferedReader br = new BufferedReader(new FileReader(fileName));
ArrayList<DueDate> tasks = new ArrayList<DueDate>();
String line = "";
while((line = br.readLine()) != null) {
String[] values = line.split("\t");
if(values.length == 2) {
String addedTask = values[0];
String enteredDueDate = values[1];
DueDate d = new DueDate(addedTask, enteredDueDate);
tasks.add(d);
}
}
Collections.sort(tasks);
br.close();
//Changed it to not append but overwrite the old file so it only contains the sorted list
BufferedWriter bw = new BufferedWriter(new FileWriter(fileName, false));
for (DueDate date : tasks) {
String lineText = date.toString();
bw.write(lineText);
bw.newLine();
}
bw.flush();
bw.close();
编辑:要打印排序列表,您可以做一些事情。
最简单的方法可能是将 showTaskList
方法的调用附加到 sortList
方法的末尾,例如
//...
bw.flush();
bw.close();
showTaskList();
} catch (FileNotFoundE
e.printStackTrace(
}
或者您可以遍历 ArrayList
并像这样打印出来:
//...
bw.flush();
bw.close();
for (int i = 0; i < tasks.size(); i++) {
DueDate dueDate = tasks.get(i);
System.out.println(i+". "+dueDate.toString());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
编辑 2:排序时将所有 Tasks
和空 DueDate
放到底部的最简单方法是更改 compareTo
DueDate
class:
@Override
public int compareTo(DueDate o) {
return -this.enteredDueDate.compareTo(o.enteredDueDate);
}
只需取反,所有条目都会降序排列,空 DueDate
的条目将排在列表的末尾(它依赖于默认情况下字符串的比较方式)。
但是如果你想使用没有截止日期的 Tasks
你会 运行 在你的 sortList
方法中遇到问题,因为 split("\t")
方法只会 return长度为 1 的数组和您的 if
条件将失败。一种解决方法如下所示:
//...
while ((line = br.readLine()) != null) {
String[] values = line.split("\t");
//To ensure it's still valid data
if (values.length >= 1 && values.length <= 2) {
String addedTask = values[0];
String enteredDueDate;
//Check whether dueDate has a value or is empty
if (values.length == 1)
enteredDueDate = "";
else
enteredDueDate = values[1];
DueDate d = new DueDate(addedTask, enteredDueDate);
tasks.add(d);
}
}
//...
或更紧凑的版本(做完全相同的事情)
//...
while ((line = br.readLine()) != null) {
String[] values = line.split("\t");
if (values.length >= 1 && values.length <= 2)
tasks.add(new DueDate(values[0], values.length == 1 ? "" : values[1]));
}
//...
希望这对您有所帮助 (: