Reputation: 3
I have made a code to store each appointment calendar given by user to an ArrayList. Each ArrayList contains title, description, date and time.
Now I want to make an option for the user to edit the data of appointment calendar of their choice.
I use the appointment calendar's title for the comparison. Here's the code:
// Asks user to enter the Appointment title that they wish to edit
System.out.println("Please enter the appointment that you wish to edit: ");
String choicetitle = scan.next();
for (Appointment value : aplist)
{
// if the title of the Appointment matches with one of the data, then..
if (choicetitle.equalsIgnoreCase(value.title))
{
// ..the output occurs on the console and asks the user to enter the new data
input = new Scanner(System.in);
System.out.println("Please give the new data: \n");
System.out.println("Please give the date of the appointment! \n" +
"Year (format YYYY): ");
year = input.nextInt();
System.out.println("Month (format MM): ");
month = input.nextInt();
System.out.println("Day (format DD): ");
day = input.nextInt();
value.date = LocalDate.of(year, month, day);
System.out.println("Please give the time of the appointment! \n" +
"Hour (format hh): ");
hour = input.nextInt();
System.out.println("Minute (format mm): ");
min = input.nextInt();
value.time = LocalTime.of(hour, min);
System.out.println("Please give the title of the appointment: ");
value.title = input.next();
System.out.println("Please give the description of the appointment: ");
value.desc = input.next();
}
// if the user enters a title that doesn't exist
else
{
System.out.println("There's no appointment with this title!");
}
}
But if there are already 3 elements (3 appointment calendars) in the ArrayList and let's say I want to edit the third one, it gives the following output:
There's no appointment with this title!
There's no appointment with this title!
Please give the new data:
Please give the date of the appointment!
Year (format YYYY):
Is there any way so that the output:
Thanks in advance!
Upvotes: 0
Views: 75
Reputation: 20914
It appears you are unfamiliar with the process of debugging your code. All computer programmers need to know how to do this. If you are using an IDE then it probably has a debugger. You should learn how to use it.
You are doing all your processing in the for
loop. You should first locate the requested appointment via the for
loop. After the for
loop terminates, and only if you have found the requested appointment, should you process it. Here is some code to get you started.
System.out.println("Please enter the appointment that you wish to edit: ");
String choicetitle = scan.next();
Appointment found = null;
for (Appointment value : aplist)
{
// if the title of the Appointment matches with one of the data, then..
if (choicetitle.equalsIgnoreCase(value.title))
{
found = value;
break;
}
}
if (value == null) {
System.out.println("There's no appointment with this title!");
}
else {
System.out.println("Please give the new data: \n");
System.out.println("Please give the date of the appointment! \n" +
"Year (format YYYY): ");
year = scan.nextInt(); // no need to create a separate `Scanner`
// rest of your processing code
}
Alternatively, you could use the stream API, although I presume you have not learned about that yet. Below code shows how to search for a particular appointment using the stream API.
aplist.stream()
.filter(value -> choicetitle.equalsIgnoreCase(value.title))
.findFirst();
Upvotes: 1