I still have the problem when i try to add person. The code is suppose to detect invalid name and birthday, and would not to add this person into the personList. However, because i'm not able to get the birthday until the code read it, the person object can only be added when the loop finished. The problem occurs when i try to test to add an invlid person name and/or birthday. It shows:
Failed to add person: Invalid name!!
New record:
Name: null"
Birthday: Unkown DOB
has been added!
Exception in thread "main" java.lang.NullPointerException
at Instruction.readInstruction(Instruction.java:272)
at EPB.main(EPB.java:12)
else if(words[0].equalsIgnoreCase("add"))
{
Person p = new Person();
String s1 = line.substring(words[0].length()).trim();
String[] s2 = s1.split(";");
if(s2.length>=2)// s2[0] = " name Testing Three" s2[1] = " birthday 13-05-1981" and s2[2]=" address.."
{
for(int i=0; i<s2.length;i++)
{
String s3 = s2[i].trim(); // "delete the leading space" s2[0] = "name Testing Three" s2[1] = "birthday 13-05-1981"
String[] s4 = s3.split("\\s+"); //s4[0] = name; s4[1] = Testing; s4[2]=Three
if(s4.length>=2) // s2[1]=birthday 13-05-1986 only has length of 2
{
if(s4[0].equalsIgnoreCase("name"))
{
//System.out.println(s4[1]);
String name="";
if(Functions.nameValidation(s4[1]))
{
for(int j=2;j<s4.length;j++)
{
name = s4[1] + " " + s4[j];
}
if(Functions.nameValidation(name))
{
p.setName(name);
}
else
{
System.out.println("Failed to add person: Invalid name!");
break;
}
}
else
{
System.out.println("Failed to add person: Invalid name!!");
break;
}
}//end of word equals to name
//-----------------------------------------------------------------
else if(s4[0].equalsIgnoreCase("birthday") && s4.length ==2)
{
if(Functions.dateValidation(s4[1]))
{
try
{
p.setBirthdayString(s4[1]);
}
catch (ParseException e)
{
//e.printStackTrace();
}
}
else
{
System.out.println("Failed to add person: Invalid Birthday Format");
break;
}
}
//end of word equals to birthday
//-----------------------------------------------------------------
boolean notFound = false;
for(Person p1: personList)
{
if(p1.getName().equals(p.getName()))
{
if(p1.getBirthdayString().equals(p.getBirthdayString()))
{
System.out.println("Information in record" +"\n" + "name: "+ p.getName() + "\n" + "Birthday: " + p.getBirthdayString() + "\n" +"has been updated");
p1.setEmail(p.getEmail());
p1.setPhone(p.getPhone());
p1.setAddress(p.getAddress());
notFound = true;
}
}
}
if (!notFound)
{
if(Functions.nameValidation(p.getName()) && Functions.dateValidation(p.getBirthdayString()))
{
System.out.println("New record: " +"\n"+"Name: " + p.getName() + "\""+ "\n"+ "Birthday: " + p.getBirthdayString() + "\nhas been added!");
personList.add(p);
}
FileIO.outData(personList, outputFileName);
}
System.out.println();
}
I think what to meant to do in that final loop instead of iterating over personList
and checking at each step if it contains the name and the birthday of p
, was to check this condition for each element in the iteration kind of like this:
p1.getName().equals(p.getName())
instead of personList.contains(p.getName())
and
p1.getBirthdayString().equals(p.getBirthdayString())
instead of personList.contains(p.getBirthdayString())
This would fix the logic bug, and also the exception, since you won't be calling contains
while iterating through the collection.
Edit: Then you would still have a bug where you're trying to add p
to personList
in that same loop (in the "else" block: personList.add(p)
). The way you're doing it now is not only getting you an exception, but it's trying to add person p
for each p1
in the list that's not equal to it. To fix this you would need to keep a boolean inside the loop that checks if any matches were found (true if any of the conditions above for p1 is ever checked), and add p
to personList
outsite the loop, if said boolean is not true.
In conclusion, this is the modified code for the iterating loop:
boolean found = false;
for(Person p1: personList)
{
if(p1.getName.equals(p.getName()))
{
if(p1.getBirthdayString.equals(p.getBirthdayString()))
{
System.out.println("Information in record" +"\n" + "name: "+ p.getName() + "\n" + "Birthday: " + p.getBirthdayString() + "\n" +"has been updated");
p1.setEmail(p.getEmail());
p1.setPhone(p.getPhone());
p1.setAddress(p.getAddress());
FileIO.outData(personList, outputFileName);
found = true;
}
}
}
if (!found)
{
System.out.println("New record has been added!");
personList.add(p);
FileIO.outData(personList, outputFileName);
}