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>
.
One needs to make a class Customer, just a group of fields. Instead of a list of Strings.
public class Customer {
String userID;
String firstname;
String secondname;
String street;
String zipcode;
String city;
}
And in the code:
Customer newCustomer = new Customer();
newCustomer.userID = ....
System.out.println(newCustomer.userID);
When you pass check_newCustomer (newCustomer);
you are only passing a copy of the array list. In this case, the original array list newcustomer is left intact while a new copy whose scope is within the method check_newCustomer is where all the strings are stored. You can either create a new class for Customers or you can create a class and have your array list as a class variable and check_newCustomer as a class method. In the latter case, it is much simple.
class customer
ArrayList<String> newCustomer;
public static void main(String[] args) {
newCustomer = new ArrayList<String>();
check_newCustomer ();
}
public static void check_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);
}
}
This must work.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.