I want to allow the people to ask for the full list of tasks after entering task
ID: 3815524 • Letter: I
Question
I want to allow the people to ask for the full list of tasks after entering tasks and then choosing to exit instead of part of the initial question and loop. However, when I run it, it says that the question about the task list and if statement is unreachable. - This is JAVA
package task;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class TaskCreator
{
public static ArrayList list = new ArrayList();
public static void main(String[] args)
{
String display ="";
Scanner scan = new Scanner(System.in);
do
{
System.out.println("Please choose one of the options: ");
System.out.println("Press 1 : Add new tasks");
//System.out.println("Press 2 : Display all added tasks");
System.out.println("Press 3 : Exit");
int choice = scan.nextInt();// receiving the input choice from user
switch (choice)
{
case 1:
Scanner scanr = new Scanner(System.in);
System.out.print("Enter ID : ");
int id = scanr.nextInt();
System.out.print("Enter title : ");
String t = scanr.next();
System.out.print("Enter price : ");
double p = scanr.nextDouble();
System.out.print("Enter Date (format : 01/01/1989): ");
String d = scanr.next();
Task task = new Task(id,t,d,p);
list.add(task);
break;
/*case 2:
for(Task taskObj : list)
{
System.out.println(taskObj);
}
break;*/
case 3:
System.out.println("Exiting...");
System.exit(0);
default:
System.out.println("default");
break;
}
}
while (true);
System.out.print("Would you like to view the list of tasks you have created? Enter Y for yes"+ " and n for no");
display = scan.nextLine();
if (display.equals ("Y") || display.equals("y"))
{
for(Task taskObj : list)
{
System.out.println(taskObj);
}
}
else
{
System.out.println("You have finished entering tasks");
}
}
}
package task;
public class Task
{
int ID;
String title;
String time;
double price;
public Task(int iD, String title, String time, double price)
{
super();
ID = iD;
this.title = title;
this.time = time;
this.price = price;
}
@Override
public String toString() {
return "Task [ID=" + ID + ", title=" + title + ", time=" + time + ", price=" + price + "]";
}
}
Explanation / Answer
Task.java
package task;
public class Task {
int ID;
String title;
String time;
double price;
public Task(int iD, String title, String time, double price)
{
super();
ID = iD;
this.title = title;
this.time = time;
this.price = price;
}
@Override
public String toString() {
return "Task [ID=" + ID + ", title=" + title + ", time=" + time + ", price=" + price + "]";
}
}
__________________
TaskCreator.java
package task;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class TaskCreator {
public static ArrayList<Task> list = new ArrayList<Task>();
public static void main(String[] args) {
String display = "";
Scanner scan = new Scanner(System.in);
do {
System.out.println(" Please choose one of the options: ");
System.out.println("Press 1 : Add new tasks");
System.out.println("Press 2 : Display all added tasks");
System.out.println("Press 3 : Exit");
int choice = scan.nextInt();// receiving the input choice from user
switch (choice) {
case 1:
Scanner scanr = new Scanner(System.in);
System.out.print("Enter ID : ");
int id = scanr.nextInt();
System.out.print("Enter title : ");
String t = scanr.next();
System.out.print("Enter price : ");
double p = scanr.nextDouble();
System.out.print("Enter Date (format : 01/01/1989): ");
String d = scanr.next();
Task task = new Task(id, t, d, p);
list.add(task);
continue;
case 2:
for(Task taskObj : list)
{
System.out.println(taskObj);
}
continue;
case 3:
System.out.println("Exiting...");
System.exit(0);
default:
System.out.println("Invalid.Must be either 1 ,2 or 3");
continue;
}
} while (true);
}
}
___________________
Output:
Please choose one of the options:
Press 1 : Add new tasks
Press 2 : Display all added tasks
Press 3 : Exit
1
Enter ID : 1111
Enter title : xyz
Enter price : 234.45
Enter Date (format : 01/01/1989): 28/01/2006
Please choose one of the options:
Press 1 : Add new tasks
Press 2 : Display all added tasks
Press 3 : Exit
1
Enter ID : 222
Enter title : abc
Enter price : 789.9
Enter Date (format : 01/01/1989): 23/02/2012
Please choose one of the options:
Press 1 : Add new tasks
Press 2 : Display all added tasks
Press 3 : Exit
1
Enter ID : 333
Enter title : mno
Enter price : 345.6
Enter Date (format : 01/01/1989): 18/08/2009
Please choose one of the options:
Press 1 : Add new tasks
Press 2 : Display all added tasks
Press 3 : Exit
2
Task [ID=1111, title=xyz, time=28/01/2006, price=234.45]
Task [ID=222, title=abc, time=23/02/2012, price=789.9]
Task [ID=333, title=mno, time=18/08/2009, price=345.6]
Please choose one of the options:
Press 1 : Add new tasks
Press 2 : Display all added tasks
Press 3 : Exit
3
Exiting...
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.