I'm working on a assignment for my second semester java class and I'm having a little trouble trying to figure out how best to loop this. I need the "choose an option" line to pop up at the beginning of each loop and allow the user to enter a,b,c or q. If the user picks a,b,c I want to it do the action and then to return to the "choose an option" line and start over and if the user enters q it should quit. What kind of loop should I use for this?
public void go() throws Exception {
Scanner kb = new Scanner(System.in);
ArrayList<Task> list = new ArrayList<Task>();
TaskManager taskMgr = new TaskManager();
System.out.println("Welcome to MyTaskManager");
System.out.println("choose an option \n a: Add a new item \n b: Display tasks by date \n c: Display tasks by keyword \n q: quit");
System.out.println("Enter your choice");
String choice = kb.nextLine();
if (choice.equalsIgnoreCase("a")) {
taskMgr.makeTasks();
}
else if (choice.equalsIgnoreCase("b")) {
System.out.println("enter the date you wish to view: ");
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
String date=kb.nextLine();
Date dueDate = sdf.parse(date);
taskMgr.findTasksByDate(dueDate);
taskMgr.displayTaskReportByDate(dueDate);
}
else if (choice.equalsIgnoreCase("c")) {
System.out.println(" Enter a keyword: ");
String keyword=kb.nextLine();
System.out.println("Task that contain the term "+keyword);
taskMgr.findTasksByKeyword(keyword);
taskMgr.displayTaskReportByKeyword(keyword);
}
else if (choice.equalsIgnoreCase("q")) {
System.exit(0);
}
else {
System.exit(0);
}
}
You seem to be missing a loop. So, to work with your current code -
while (true) { // <-- or for(;;) {
System.out.println("choose an option \n a: Add a new item \n b: Display tasks by date \n c: Display tasks by keyword \n q: quit");
System.out.println("Enter your choice");
String choice = kb.nextLine();
if (choice.equalsIgnoreCase("a")) {
taskMgr.makeTasks();
} else if (choice.equalsIgnoreCase("b")) {
System.out.println("enter the date you wish to view: ");
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
String date=kb.nextLine();
Date dueDate = sdf.parse(date);
taskMgr.findTasksByDate(dueDate);
taskMgr.displayTaskReportByDate(dueDate);
} else if (choice.equalsIgnoreCase("c")) {
System.out.println(" Enter a keyword: ");
String keyword=kb.nextLine();
System.out.println("Task that contain the term "+keyword);
taskMgr.findTasksByKeyword(keyword);
taskMgr.displayTaskReportByKeyword(keyword);
} else if (choice.equalsIgnoreCase("q")) {
// System.exit(0);
break; //<-- ends the loop.
} // no else and quit. we want to loop.
}
You can do this with while(true)
loop - it never stops (until exit). That is because while()
usually has a value in its bracket which changes to false if the loop should stop. with while(true){ACTION}
you have the classic infinite loop, used often in computer science for a variety of applications that need user input.
public void go() throws Exception {
Scanner kb = new Scanner(System.in);
ArrayList<Task> list = new ArrayList<Task>();
TaskManager taskMgr = new TaskManager();
System.out.println("Welcome to MyTaskManager");
while(true){
System.out.println("choose an option \n a: Add a new item \n b: Display tasks by date \n c: Display tasks by keyword \n q: quit");
System.out.println("Enter your choice");
String choice = kb.next();
if (choice.equalsIgnoreCase("a")) {
taskMgr.makeTasks();
}
else if (choice.equalsIgnoreCase("b")) {
System.out.println("enter the date you wish to view: ");
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
String date=kb.nextLine();
Date dueDate = sdf.parse(date);
taskMgr.findTasksByDate(dueDate);
taskMgr.displayTaskReportByDate(dueDate);
}
else if (choice.equalsIgnoreCase("c")) {
System.out.println(" Enter a keyword: ");
String keyword=kb.nextLine();
System.out.println("Task that contain the term "+keyword);
taskMgr.findTasksByKeyword(keyword);
taskMgr.displayTaskReportByKeyword(keyword);
}
else if (choice.equalsIgnoreCase("q")) {
System.exit(0);
}
else {
System.exit(0);
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.