I would like the user to provide data by scanner and add it to list. Later, I want to get the size of this list and for example user name, last name. I'm still trying to work with constructors and exceptions and now I want to try to put data from user.
It's Human Service which checks constructors, and if the name has less than 3 letters and lastname less than 5 letters will throw an exception.
import java.util.ArrayList;
import java.util.List;
public class HumanService {
List<Human> humans;
public HumanService() {
humans = new ArrayList<>();
}
public void addHuman(String name, String lastName) throws HumanNameWrongFormat, HumanLastNameWrongFormat {
if(HumanValidator.humanValidatorName(name) && HumanValidator.humanValidatorLastName(lastName)) {
Human human = new Human(sizeOfList(), name, lastName);
humans.add(human);
}
}
public int sizeOfList() {
return humans.size();
}
public Human getHumanByLastName(String lastName) throws HumanNotFoundException {
for (Human human : humans) {
if (human.getLastName().equals(lastName)) {
return human;
}
}
throw new HumanNotFoundException(lastName + " not found");
}
public Human getHumanById (Integer id) throws HumanNotFoundException {
for (Human human : humans) {
if (human.getId().equals(id)) {
return human;
}
}
throw new HumanNotFoundException(id + " not found");
}
}
I want to get data by user to list.
For example.
Please give me your name and last name.
Here is your name and last name by scanner and that will be added to list and checked.
This is also my main class.
public class main {
public static void main(String[] args) throws HumanNotFoundException {
HumanService humanService = new HumanService();
try {
humanService.addHuman("John", "Walker");
humanService.addHuman("Steve", "Williams");
humanService.addHuman("Gregor", "Wroten");
}
catch (HumanNameWrongFormat | HumanLastNameWrongFormat e) {
System.out.println(e.getMessage());
}
System.out.println(humanService.sizeOfList());
try {
humanService.getHumanByLastName("Wroten");
}
catch (HumanNotFoundException e) {
System.out.println(e.getMessage());
}
}
}
Here is the way you can use Scanner to get all the required user names and add them to the list for further processing
public class Test {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
List<Human> humans = new ArrayList();
while (true) {
System.out.println("Please give your first name");
String firstName = scanner.nextLine();
System.out.println("Please give your last name");
String lastName = scanner.nextLine();
humans.add(new Human(firstName, lastName)); // use your humanService here
boolean breakOut = false;
String input;
do {
System.out.println("Do you want to enter more names? (Y/N)");
input = scanner.nextLine();
if (input.equalsIgnoreCase("Y") || input.equalsIgnoreCase("N")) {
breakOut = true;
} else {
System.out.println("Invalid input. try again");
}
} while (!breakOut);
if (input.equalsIgnoreCase("Y")) {
break;
}
}
System.out.println(humans);
}
}
I am assuming you have two fields firstName, lastName in Human
class Human {
private String firstName;
private String lastName;
public Human(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
#Override
public String toString() {
return "first name " + firstName + " and last name " + lastName;
}
}
Input:
Please give your first name
John
Please give your last name
Smith
Do you want to enter more names? (Y/N)
y
Please give your first name
Will
Please give your last name
Smith
Do you want to enter more names? (Y/N)
n
Output:
[first name John and last name Smith, first name Will and last name Smith]
In the above code, replace the list with your humanService to add the humans
Related
This assignment requires me to take in input and print it out. The first half of the output is printing currently, but the other half is giving me null. What's wrong here?
Heres my code:
This is the main class.
import java.util.*;
public class Assignment4 {
public static void main(String[] args) {
// local variables, can be accessed anywhere from the main method
char input1 = 'Z';
// String inputInfo= "";
String courseName, firstName, lastName, office, university;
String line = new String();
// instantiate a Course object
Course cse110 = null;
printMenu();
// Create a Scanner object to read user input
Scanner scan = new Scanner(System.in);
do // will ask for user input
{
System.out.println("What action would you like to perform?");
line = scan.nextLine();
if (line.length() == 1) {
input1 = line.charAt(0);
input1 = Character.toUpperCase(input1);
// matches one of the case statement
switch (input1) {
case 'A': // Add a course
System.out.print("Please enter the Instructor information:\n");
System.out.print("Enter instructor's first name:\t");
firstName = scan.nextLine();
System.out.print("Enter instructor's last name:\t");
lastName = scan.nextLine();
System.out.print("Enter instructor's office number:\t");
office = scan.nextLine();
Instructor myInstructor = new Instructor(firstName, lastName, office);
System.out.print("\nPlease enter the Course information:");
System.out.print("\nEnter course name:\t");
courseName = scan.nextLine();
System.out.print("Enter university name:\t");
university = scan.nextLine();
cse110 = new Course(courseName, myInstructor, university);
break;
case 'D': // Display course
System.out.print(cse110.toString());
break;
case 'Q': // Quit
break;
case '?': // Display Menu
printMenu();
break;
default:
System.out.print("Unknown action\n");
break;
}
} else {
System.out.print("Unknown action\n");
}
} while (input1 != 'Q' || line.length() != 1);
scan.close();
}
/** The method printMenu displays the menu to a user **/
public static void printMenu() {
System.out.print("Choice\t\tAction\n" + "------\t\t------\n" + "A\t\tAdd Course\n" + "D\t\tDisplay Course\n"
+ "Q\t\tQuit\n" + "?\t\tDisplay Help\n\n");
}
}
Heres the Course class:
import java.util.*;
public class Course
{
//----------------------------------------------------------------------
// ATTRIBUTES
private String courseName;
private Instructor instructor;
private String university;
//----------------------------------------------------------------------
// CONSTRUCTOR
public Course()
{
courseName = "?";
university = "?";
instructor = null;
}
public Course(String name, Instructor inst, String univer)
{
this.setName(name);
this.setInstructor(name, Instructor.lastName, Instructor.officeNum);
this.setUniversity(univer);
}
//----------------------------------------------------------------------
// ACCESSORS
public String getName()
{
return courseName;
}
public String getUniversity()
{
return university;
}
public Instructor getInstructor()
{
return instructor;
}
//----------------------------------------------------------------------
//METHODS
public void setName(String someName)
{
this.courseName = someName;
}
public void setUniversity(String someUniversity)
{
this.university = someUniversity;
}
public void setInstructor(String firstName, String lastName, String office)
{
Instructor.firstName = firstName;
Instructor.lastName = lastName;
Instructor.officeNum = office;
}
public String toString()
{
return "Course name:\t" + courseName + " at " + university + "\nInstructor Information:" + instructor + "\n";
}
}
Heres the Instructor class:
import java.util.*;
public class Instructor
{
//----------------------------------------------------------------------
// ATTRIBUTES
public static String firstName;
public static String lastName;
public static String officeNum;
//----------------------------------------------------------------------
// CONSTRUCTOR
public Instructor()
{
firstName = "?";
lastName = "?";
officeNum = "?";
}
public Instructor(String first, String last, String office)
{
this.setFirstName(first);
this.setLastName(last);
this.setOfficeNum(office);
}
public Instructor(Instructor inst)
{
firstName = inst.firstName;
lastName = inst.lastName;
officeNum = inst.officeNum;
}
//----------------------------------------------------------------------
// ACCESSORS
public String getFirstName()
{
return firstName;
}
public String getLastName()
{
return lastName;
}
public String getOfficeNum()
{
return officeNum;
}
//----------------------------------------------------------------------
//METHODS
public void setFirstName(String someFirstName)
{
this.firstName = someFirstName;
}
public void setLastName(String someLastName)
{
this.lastName = someLastName;
}
public void setOfficeNum(String someOffice)
{
this.officeNum = someOffice;
}
public String toString()
{
return ("\nLast Name:\t" + lastName +
"\nFirst Name:\t " + firstName +
"\nOffice Number:\t" + officeNum);
}
}
And finally heres the output:
> Choice Action
------ ------
A Add Course
D Display Course
Q Quit
? Display Help
What action would you like to perform?
A
Please enter the Instructor information:
Enter instructor's first name: John
Enter instructor's last name: Appleseed
Enter instructor's office number: 501
Please enter the Course information:
Enter course name: Intro to Java
Enter university name: ASU
What action would you like to perform?
D
Course name: Intro to Java at ASU
Instructor Information:null
What action would you like to perform?
Instead of Information:null, it should be printing out:
Last name: Appleseed
First name: John
Office Number: 501
How can I fix this?
Thanks!
You've never set the instructor property inside Course, after it is initialized as null. You should not use static fields in Instructor. You should create a new Instructor() and assign the instructor instance to the instructor property of Course
First fix your Instructor class:
public class Instructor
{
/******These should not be public static********/
private String firstName;
private String lastName;
private String officeNum;
...
}
Next fix your Course class
public class Course
{
private String courseName;
private Instructor instructor;
private String university;
public Course(String name, Instructor inst, String univer)
{
this.setName(name);
this.setInstructor(inst);
this.setUniversity(univer);
}
public void setInstructor(Instructor inst)
{
this.instructor = inst;
}
...
}
import java.util.Scanner;
Here I'm trying to throw custom exception if the lastName is empty/null, so I have created a constructor which accepts two string arguments and the value is passed while the object is created, but the problem is it's always null in the constructor. Seems like the value is not being passed. What am I doing wrong here? Any alternate solution or explain please. Thanks (Sorry for bad English).
public class Employee {
private String firstName;
private String lastName;
public Employee(String firstName,String lastName) {//here the value is always null and so throwing exception.
try {
if(this.firstName==null||this.lastName==null) {
throw new FirstnameOrLastnameNotFound("Both first and last name must not be empty");
}
}
catch(FirstnameOrLastnameNotFound e) {
System.out.println(e);
System.exit(1);
}
this.firstName=firstName;
this.lastName=lastName;
}
public void display() {
System.out.println("Name of the employee: " + this.firstName+" "+ this.lastName);
}
public static void main(String[] args) {
//Scanner sc= new Scanner(System.in);
//System.out.println("Enter the first name:");
//String firstNameAndLastName=sc.nextLine();
//String[]name=firstNameAndLastName.split(" ");
Employee obj= new Employee("dummy", "dummy");//this dummy sting is not passing in constructor.
obj.display();
}
}
Your problem is that you are testing the value of the class members firstName and lastName before you are assigning them values.
You want to test whether the constructor parameters are null, so just remove this..
Here is your corrected code.
public class Employee {
private String firstName;
private String lastName;
public Employee(String firstName, String lastName) {
try {
if (firstName == null || lastName == null) { // CHANGE HERE
throw new FirstnameOrLastnameNotFound("Both first and last name must not be empty");
}
}
catch (FirstnameOrLastnameNotFound e) {
System.out.println(e);
System.exit(1);
}
this.firstName = firstName;
this.lastName = lastName;
}
public void display() {
System.out.println("Name of the employee: " + this.firstName + " " + this.lastName);
}
public static void main(String[] args) {
Employee obj = new Employee("dummy", "dummy");
obj.display();
}
}
You also wrote in your comment that it works when you remove this.
Note though, that your fix will throw NullPointerException if you call the constructor with null argument, for example:
Employee obj = new Employee(null, "dummy");
If null or empty names are not allowed, then your check should be as follows:
if (firstName == null || firstName.isEmpty() || lastName == null || lastName.isEmpty()) {
throw new FirstnameOrLastnameNotFound("Both first and last name must not be empty");
}
The idea behind throwing an exception is to indicate to the invoking code that the invoked method failed. You usually don't throw an exception and handle it in the same method. You should declare that the constructor throws FirstnameOrLastnameNotFound and put the try-catch in method main as below:
public class Employee {
private String firstName;
private String lastName;
public Employee(String firstName, String lastName) throws FirstnameOrLastnameNotFound {
if (firstName == null || firstName.isEmpty() || lastName == null || lastName.isEmpty()) {
throw new FirstnameOrLastnameNotFound("Both first and last name must not be empty");
}
this.firstName = firstName;
this.lastName = lastName;
}
public void display() {
System.out.println("Name of the employee: " + this.firstName + " " + this.lastName);
}
public static void main(String[] args) {
try {
Employee obj = new Employee("dummy", "dummy");
obj.display();
}
catch (FirstnameOrLastnameNotFound e) {
System.out.println(e);
System.exit(1);
}
}
}
I am trying to update myArray list by allowing the user to change their first and last name while keeping other information in the arraylist the same.
the code follows
public void editStudentID(int findStudentId) {
for (int i = 0; i < students.size(); i++) {
if (students.get(i).getId() != findStudentId) {
continue;
}
System.out.println("Found a profile containing information for " + findStudentId + ":");
System.out.println("What would you like to change in your profile?");
System.out.println("1.First Name");
System.out.println("2.Last Name");
int decision = scanner.nextInt();
switch (decision) {
case 1:
System.out.println("Enter a new first name to continue");
String newFirstName = scanner.next();//need to find a way to update this in my arraylist
break;
case 2:
System.out.println("Enter a new last name to continue");
String newLastName = scanner.next();//this as well
break;
}
return;
}
System.out.println(" Id not found ");
}
this is my Student class where I only wrote final for only my id and dob to not be changed by the user
public class Student {
private final int id;
private String firstName;
private String lastName;
private final String dob;
public Student(int id, String firstName, String lastName, String dob) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.dob = dob;
}
public int getId() {
return id;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public String getDob() {
return dob;
}
public static Student createStudentID(int id, String firstName, String lastName, String dob) {
return new Student(id, firstName, lastName, dob);
}
}
First, you need to make you Student class mutable, but supplying a couple of "setters" which will allow you to change the first and last name properties, for example
public class Student {
//...
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
Then in you editStudentID method, you simply update the required record, for example
public void editStudentID(int findStudentId) {
for (int i = 0; i < students.size(); i++) {
if (students.get(i).getId() != findStudentId) {
continue;
}
System.out.println("Found a profile containing information for " + findStudentId + ":");
System.out.println("What would you like to change in your profile?");
System.out.println("1.First Name");
System.out.println("2.Last Name");
int decision = scanner.nextInt();
switch (decision) {
case 1:
System.out.println("Enter a new first name to continue");
String newFirstName = scanner.next();//need to find a way to update this in my arraylist
students.get(i).setFirstName(newFirstName);
break;
case 2:
System.out.println("Enter a new last name to continue");
String newLastName = scanner.next();//this as well
students.get(i).setLastName(newFirstName);
break;
}
return;
}
System.out.println(" Id not found ");
}
I am new to OOP and programming in general. I am having trouble with how to put things in the parent class and call them from the other classes and main.
I have the following arraylist creators in main, but feel to be really OOP these should be in the parent and subclasses and just called from main. Is this is correct can someone help me with how this would work.
How do I get the arraylist in the parent class and then call it correctly from main?
This is what I have for main:
package ooo1;
import java.util.ArrayList;
import java.util.Scanner;
public class ContactList {
public static void main(String[] args) {
Scanner input1 = new Scanner(System.in);
int type = 0;
while(type != 5){
System.out.println("Please select an option:");
System.out.println("Personal Contact: Enter 1");
System.out.println("Business Contact: Enter 2");
System.out.println("Display Personal Contacts: Enter 3");
System.out.println("Display Business Contacts: Enter 4");
System.out.println("5 to quit");
type = input1.nextInt();
if(type == 5){
System.out.println("Goodbye");
break;
}
ArrayList<Contact> contacts = new ArrayList<Contact>();
Scanner input = new Scanner(System.in);
System.out.println("Please enter ContactId : ");
String contactId = input.nextLine();
System.out.println("Please enter First Name : ");
String firstName = input.nextLine();
System.out.println("Please enter Last Name : ");
String lastName = input.nextLine();
System.out.println("Please enter Address : ");
String address = input.nextLine();
System.out.println("Please enter Phone Number : ");
String phoneNumber = input.nextLine();
System.out.println("Please enter Email Address : ");
String emailAddress = input.nextLine();
if(type == 1){
System.out.println("Please enter Birthday: ");
String dateofBirth = input.nextLine();
Contact pcontact = new PersonalContact(contactId, firstName, lastName, address, phoneNumber, emailAddress, dateofBirth);
contacts.add(pcontact);
}
else if(type == 2){
System.out.println("Please enter Job Title: ");
String jobTitle = input.nextLine();
System.out.println("Please enter Organization: ");
String organization = input.nextLine();
Contact bcontact = new BusinessContact(contactId, firstName, lastName, address, phoneNumber, emailAddress, jobTitle, organization);
contacts.add(bcontact);
}
}
}
}
This is what I have for the parent class:
package ooo1;
import java.util.ArrayList;
import java.util.Scanner;
public abstract class Contact {
String contactId;
String firstName;
String lastName;
String address;
String phoneNumber;
String emailAddress;
public Contact(String contactId,String firstName,String lastName, String address, String phoneNumber, String emailAddress)
{
this.contactId = contactId;
this.firstName = firstName;
this.lastName = lastName;
this.address = address;
this.phoneNumber = phoneNumber;
this.emailAddress = emailAddress;
}
public void setContactId(String input){
this.contactId = input;
}
public String getContactId(){
return contactId;
}
public void setFirstName(String input){
this.firstName = input;
}
public String getFirstName(){
return firstName;
}
public void setLastName(String input){
this.lastName = input;
}
public String getLastName(){
return lastName;
}
public void setAddress(String input){
this.address = input;
}
public String getAddress(){
return address;
}
public void setPhoneNumber(String input){
this.phoneNumber = input;
}
public String getPhoneNumber(){
return phoneNumber;
}
public void setEmailAddress(String input){
this.emailAddress = input;
}
public String getEmailAddress(){
return emailAddress;
}
void displayContacts(){
System.out.println("Contact ID:" + contactId + " First Name:" + firstName + " Last Name:" + lastName);
System.out.println("Address:" + address);
System.out.println("Phone Number:" + phoneNumber);
System.out.println("Email Address:" + emailAddress);
}
}
One of my subclasses: other same just adds a few more variables:
Display Contact(): doesn't work not sure what to do with it either.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package ooo1;
public class PersonalContact extends Contact {
private String dateofBirth;
public PersonalContact(String contactId, String firstName, String lastName, String address, String phoneNumber, String emailAddress, String dateofBirth){
super(contactId, firstName, lastName, address, phoneNumber, emailAddress);
this.dateofBirth = dateofBirth;
}
public void setDateofBirth(String input){
this.dateofBirth=input;
}
public String getDateofBirth(){
return this.dateofBirth;
}
#Override
public void displayContacts(){
System.out.print("Personal Contacts: ");
System.out.println("Contact ID:" + contactId + " First Name:" + firstName + " Last Name:" + lastName);
System.out.println("Address:" + address);
System.out.println("Phone Number:" + phoneNumber);
System.out.println("Email Address:" + emailAddress);
System.out.println("Birthday:" + dateofBirth);
}
}
You probably want something like this.
public class AddressBook<T extends Contact>
{
private List<T> contacts = new ArrayList<T>();
public void addContact(T contact)
{
contacts.add(contact);
}
}
You could instantiate and use this class like this.
AddressBook<Contact> book = new AddressBook<Contact>();
book.add(new PersonalContact(...));
book.add(new BusinessContact(...));
Then over time you have the flexibility to add methods to AddressBook that work with the underlying collection. For instance you might want to search for contacts with a particular name. Or return an iterator of Contacts ordered by a particular attribute.
You can add a method in class Contact :
public void getData(){
// take in all the inputs here, so that you can directly store them in class member variables instead of passing them from main.
}
Assuming that PersonalContact & BusinessContact are inherited classes from Contact.
You can add a method in them:
class PersonalContact extends Contact{
String dateofBirth;
public void getData(){
super.getData(); //calls getData() method from base class
// take DOB as input & store it
}
similarly for BusinessContact class.
I suggest you take a look at abstract classes & interfaces for future use.
The Contact class seems okay. But ContactList not that much. It's supposed to be a data structure for contacts, so there's not reason for main method there.
public class ContactList {
private ArrayList<Contact> contacts;
public ContactList(){
this.contacts = new ArrayList<Contact>();
}
public void addContact(Contact contact){
this.contacts.add(contact);
}
public Contact getContact(int index){
return contacts.get(index);
}
// other methods that work with the data structure
// f.e. searching, deleting, ...
}
and then you could have some ContactUtil class that would take care of reading contact info from user (what you had in you main method).
public final class ContactUtil {
private ContactUtil(){} // we don't want to create instances of this class
public static Contact readNewContact(){
Scanner input1 = new Scanner(System.in);
int type = 0;
...
return contact;
}
}
And finally you will have some class just for main():
public class Main {
public static void main(String[] args){
ContactList myContacs = new ContactList();
myContacts.add(ContactUtil.readNewContact());
Contact contact = ContactUtil.readNewContact();
myContacts.add(contact);
}
}
I am required to make an address book application without the use of databases (on memory). I have decided to use ArrayLists to do so. But the problem is that once I input a new name/contact, it overrides any other contacts that I "stored" (or thought I stored) before. I have been trying to figure it out and am outright confused.
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
firstActions();
}
static String firstName;
static String lastName;
static String phoneNumber;
static String search = null;
static public int choice = 0;
static Scanner input = new Scanner (System.in);
static ContactInformation contact;
static ArrayList<String> information = new ArrayList<String>();
public static void firstActions()
{
System.out.println("Address Book Menu: What would you like to do? 1) Input data. 2) Search data. 3) Close.");
choice = input.nextInt();
switch (choice) {
case 1:
inputData();
case 2:
System.out.println("Search by: 1) First Name 2) Last Name 3) Phone Number 4) Zip Code.");
choice = input.nextInt();
switch (choice) {
case 1:
searchName();
break;
case 2:
searchLastName();
case 3:
searchPhoneNumber();
case 4:
//execute search by Zip Code
default:
System.out.println("Please compile again.");
break;
}
break;
case 3:
System.out.println("Application terminated.");
System.exit(0);
default:
System.out.println("Please compile again.");
break;
}
}
public static void inputData ()
{
information = new ArrayList<String>();
contact = new ContactInformation(firstName, lastName, phoneNumber, information);
System.out.println("What is your first name?");
contact.setFirstName(input.next());
information.add(contact.getFirstName());
System.out.println("What is your last name?");
contact.setLastName(input.next());
information.add(contact.getLastName());
System.out.println("What is your phone number?");
contact.setPhoneNumber(input.next());
information.add(contact.getPhoneNumber());
System.out.println("Saved.");
System.out.println("What would you like to do next?");
firstActions();
}
public static void searchName()
{
System.out.println("What is the first name you are looking for?");
search = input.next();
if (search.equals(information.get(0)))
{
System.out.println(information);
System.out.println("What would you like to do next?");
firstActions();
}
else
{
System.out.println("This person is not saved in the address book. Please try again.");
firstActions();
}
}
public static void searchLastName()
{
System.out.println("What is the last name you are looking for?");
search = input.next();
if (search.equals(information.get(1)))
{
System.out.println(information);
firstActions();
}
else
{
System.out.println("This person is not saved in the address book. Please try again.");
firstActions();
}
}
public static void searchPhoneNumber()
{
System.out.println("What is the last name you are looking for?");
search = input.next();
if (search.equals(information.get(2)))
{
System.out.println(information);
firstActions();
}
else
{
System.out.println("This person is not saved in the address book. Please try again.");
firstActions();
}
}
}
Here is my contact information class:
import java.util.ArrayList;
public class ContactInformation {
public String firstName;
public String lastName;
public String phoneNumber;
ArrayList <String> information = new ArrayList<String> ();
public ContactInformation(String firstName, String lastName,
String phoneNumber, ArrayList<String> information) {
super();
this.firstName = firstName;
this.lastName = lastName;
this.phoneNumber = phoneNumber;
this.information = information;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
}
You first create the ArrayList here:
static ArrayList<String> information = new ArrayList<String>();
but every time you go to the inputData() method, you create a NEW ArrayList:
information = new ArrayList<String>();
From how you wrote the code, I would assume you have a ContactInformation object that you should be placing into the ArrayList.
Change the ArrayList to: static ArrayList<ContactInformation> information = new ArrayList<ContactInformation>();
Then you can create each object and ADD the object to the ArrayList INSTEAD of all the information separately.
EDIT:
Your "ContactInformation" object contains String variables. After you add this object to the ArrayList, you can use a loop to find if the data in the object matches what you are looking for. It should look something like this:
for (int i = 0; i != information.size(); i++) {
if (information.get(i).getFirstName().matches(search)) {
System.out.println("found");
}
}
The if statement says that "if the element 'i's variable 'firstName' in ArrayList 'information' matches the variable 'search', print the word 'found'."
You can obviously change what happens if the name is found, I just simplified it.
everytime you want to insert a name you are creating a new Object from ArrayList
information = new ArrayList<String>();
initizalize this arraylist in your main method and then access it via its variable(information)
The immediate problem is with the first line in your inputData() method:
information = new ArrayList<String>();
You're creating a new ArrayList object every time the method is called, which means the old object, and the data it contained, is lost.