0

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:)

Danrex
  • 1,657
  • 4
  • 31
  • 44
  • 1
    possible duplicate [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Reimeus Feb 26 '14 at 00:01
  • 2
    Your main program is only capable of adding one address. If you're expecting that when you run it the second time you'll get the same data from the first run--sorry, that doesn't work. When you rerun a program it starts from scratch. To make this work, you need to find out how to set up a file or a database. – ajb Feb 26 '14 at 00:01
  • Sorry, I didn't realize that this actually adds two addresses, not one. So the main problems is the comparison to `"Y"`. However, there are other issues. – ajb Feb 26 '14 at 00:20

3 Answers3

2

This is an extended comment and the question should be closed

String comparision in Java is done via the equals method not ==

== is comparing memory references where as equals compares content...

So, instead of

if (answer == "Y")

You should be using something more like...

if ("Y".equalsIgnoreCase(answer))

Assuming you want the check to be case insensitive...

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
1
 if (answer == "Y")

Strings are compared using the .equals() method.

 if (answer.equals("Y"))
Tdorno
  • 1,571
  • 4
  • 22
  • 32
1

Three major problems (besides using == to compare a string to "Y"):

(1) Since you've allocated space for 10 strings, it appears that you want the user to be able to input that many names. Your program is only able to add up to two names, however, since addAddress only adds one name, and main might call addAddress once or twice but it does not call it in a loop. You'll need to add a loop somewhere to do input, probably in main.

(2) addAddress creates a new array every time it's called. So whatever work you did in the first addAddress gets thrown away the second time. You'll need to create the array somewhere else; one way is to create it in main() and then pass it as an argument to addAddress. But it shouldn't be an array of String... keep reading.

(3) You set up a MyAddress class to hold a name and address, which is fine. However, your addressBook array is an array of strings, which defeats the purpose. Note that although you want your addressBook to hold 10 entries, it only holds 5, because you're using two array elements for each address book entry. If you really want to use an array, the best way to handle this would be:

Don't do this next in addAddress! You don't want to set up a whole new empty array and throw out all the previous data every time you add an address, do you?

//Create a list for AddressBook with max of 10 input values
addressBook = new MyAddress[10];

Note that this is an array of MyAddress, not an array of String.

Then, to add an entry:

//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);

//Iterates through the list to add name and address values
for (int i = 0; i < addressBook.length; i++)
{
    if(addressBook[i] == null)
     {
        addressBook[i] = input;
        break;
     }
}   

Note that now you don't need to get the strings out of input that you just put into them, because you have an array of MyAddress instead of an array of strings. (By the way, an even better approach is to define a constructor in MyAddress that takes your strings as arguments, and then you can say this:

MyAddress input = new MyAddress(name, address);

without needing to use setName and setAddress.)

ajb
  • 31,309
  • 3
  • 58
  • 84
  • Thank you for taking the time to answer this and give me a really good answer. I am completely lost and will endeavour to work through your comments soon but at least I have somewhere to start. Thank you! – Danrex Feb 26 '14 at 02:23