gpt4 book ai didi

java - 实现类似的java

转载 作者:行者123 更新时间:2023-12-01 21:50:53 27 4
gpt4 key购买 nike

我正在尝试使用类似的界面按日期对用户输入的任务和日期列表进行排序。我的任务位于外部 .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 类的构造函数缺少 enteredDueDate 变量的赋值,因为参数名为 dueDate 而不是 输入截止日期。您应该将其更改为如下所示:

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:排序时将所有带有空DueDateTasks放到底部的最简单方法是更改​​compareTo DueDate 类中的 方法:

@Override
public int compareTo(DueDate o) {
return -this.enteredDueDate.compareTo(o.enteredDueDate);
}

只需对其进行否定,以便所有条目都按降序排序,并且 DueDate 为空的条目将位于列表的末尾(它依赖于默认情况下如何比较字符串)。
但是,如果您想使用没有截止日期的 Tasks,那么您的 sortList 方法将会遇到问题,因为 split("\t") 方法只会返回长度为 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]));
}
//...

希望这有帮助(:

关于java - 实现类似的java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35256894/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com