I am writing a program with different classes and, because I am new to programming, it is confusing me quite a bit. I am unsure of where the problem lies although I think I can pinpoint it to two pieces of code. But for the sake of transparency I will put the whole code in.
The overall goal is to build an array that adds peoples name and addresses to an array (I do not need to delete anyone), and this is done from the main class via user input. Firstly I use getters and setter methods. Seems to be no problems here.
public class MyAddress
{
String name = "";
String address = "";
//@param Name is name of addressee
//@param Address is address of addressee
public void setName(String Name)
{
name = Name;
}
public void setAddress(String Address)
{
address = Address;
}
public String getName()
{
return name;
}
public String getAddress()
{
return address;
}
}
I then use these methods within this class to obtain user inputs and put it into an array.
public class MyAddressBook
{
public void addAddress()
{
Scanner in = new Scanner(System.in);
System.out.println("Enter name: ");
String name = in.nextLine();
System.out.println("Enter address ");
String address = in.nextLine();
//Create MyAddress object that sets the address and name and then gets those values to put into the list
MyAddress input = new MyAddress();
input.setName(name);
input.setAddress(address);
//MyAddress input = new MyAddress();
String newName = input.getName();
String newAddress = input.getAddress();
//Create a list for AddressBook with max of 10 input values
addressBook = new String[10];
//Iterates through the list to add name and address values
for (int i = 0; i < addressBook.length; i++)
{
if(addressBook[i] == null)
{
addressBook[i] = newName;
addressBook[i+1] = newAddress;
break;
}
}
}
}
Although I am still unsure why I need the getters and setters since it seems I could just directly use the input from the user this is what was asked for so I have included it. My idea was that I would iterate through the array and if the value was NULL then I would enter in the name, then address, and then break out of the if statement. I was thinking that when I then entered a new person and address it would iterate through until it found the next null value at which point it would enter in the person and address. But this doesn't seem to be happening. Is it because when it is null, it just breaks out of the if statement and then stops? But even then when I take out the break it still doesn't work, which I am about to tell you why.
Here is the main class.
public class MyCLI {
public static void main(String[] args) {
MyAddressBook personAddress = new MyAddressBook();
personAddress.addAddress();
Scanner in = new Scanner(System.in);
System.out.println("Add another address? Y/N");
String answer = in.nextLine();
if (answer == "Y")
{
personAddress.addAddress();
}
}
}
So what is happening is that it works for the first time I run it but if I need to add another name and address to the array it doesn't work. It doesn't seem to be making it into the second iteration of the for statement in the previous code. Of course the if statment in this piece of code could be wrong also but I just have no idea what I am doing wrong in order to find out how to fix it? Yes I am new to programming:)