Heyho everyone!
My project is a bit bigger so I decided to short it a bit and showing only the problem code, which i have currently. On the first, im programming on a console. Reading six strings from a Scanner, before I save them in a variable i'm doing a validity check (length, special signs, etc...). So I decided to make this in an extra method check_newCustomer(). I used an ArrayList to return more as one value. So now is the point that I need the captured inputs in the main() function or any other method which writes the new Customer in the database. Problem is now i don't know how I can reference to userID, firstname, secondname... in other method. I just can refer with an index. But I would prefer it when i can use the variable names to refer to it. So its much easier on the other methods to handle with strings. Possible?
public static void main(String[] args) {
ArrayList<String> newCustomer = new ArrayList<String>();
check_newCustomer (newCustomer);
}
public static void check_newCustomer(ArrayList<String> newCustomer) throws IOException {
String userID = null;
String firstname = null;
String secondname = null;
String street = null;
String zipcode = null;
String city = null;
// validity check before I fill the input fields
...
// fill arraylist
newCustomer.add(userID);
newCustomer.add(firstname);
newCustomer.add(secondname);
newCustomer.add(street);
newCustomer.add(zipcode);
newCustomer.add(village);
}
Thanks!
No, the value in the ArrayList
is just a reference. The fact that you originally referred to it using a different variable is irrelevant.
You could use a Map<String, String>
instead... but it would be much cleaner to have a Customer
class which had fields for the various pieces of information. If you want multiple customers, you can then have a List<Customer>
.
See more on this question at Stackoverflow