Reputation: 1655
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:)
Upvotes: 0
Views: 93
Reputation: 347204
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...
Upvotes: 2
Reputation: 31689
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
.)
Upvotes: 1
Reputation: 1571
if (answer == "Y")
Strings are compared using the .equals()
method.
if (answer.equals("Y"))
Upvotes: 1