Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: user input going to wrong ArrayList

  1. #1
    Member
    Join Date
    Mar 2012
    Posts
    42
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default user input going to wrong ArrayList

    I am having trouble figuring out how to get my inputs to go to the right array list. Here is some of my code:
    public class Week01 {
    	String givenName = null;
    	String surname = null;
    	String customerID = null;
    	String employeeID = null;
    	String phoneNumber = null;
    	String streetAddress = null;
    	String city = null;
    	String state = null;
    	String zip = null;
    	String zipPlus4 = null;
    	ArrayList<Customer> mycustomer;
    	ArrayList<Employee> myemployee;
    	private JFrame frame;
    	private JTextField tfgiven;
    	private JTextField tflast;
    	private JTextField tfpersonID;
    	private JTextField tfphone;
    	private JTextField tfaddress;
    	private JTextField tfcity;
    	private JTextField tfstate;
    	private JTextField tfzip;
    	private JTextField tfzip4;
     
    	/**
    	 * Launch the application.
    	 */
    	public static void main(String[] args) {
    		EventQueue.invokeLater(new Runnable() {
    			public void run() {
    				try {
    					Week01 window = new Week01();
    					window.frame.setVisible(true);
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
    			}
    		});
    	}
     
     
    	/**
    	 * Create the application.
    	 */
    	public Week01() {
    		initialize();
    	}
     
    	/**
    	 * Initialize the contents of the frame.
    	 */
    	private void initialize() {		JButton btnAddCust = new JButton("Add Customer");
    		btnAddCust.setFont(new Font("Tahoma", Font.BOLD, 11));
    		btnAddCust.setBounds(25, 239, 123, 40);
    		frame.getContentPane().add(btnAddCust);
    		mycustomer = new ArrayList<Customer>();
    		btnAddCust.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent arg0) {
    				mycustomer.add(new Customer(givenName, surname, 0));
    				givenName = tfgiven.getText();
    				tfgiven.setText("");
    				surname = tflast.getText();
    				tflast.setText("");
    				customerID = tfpersonID.getText();
    				tfpersonID.setText("");
    				if(customerID.trim().equals("")) {
    					JOptionPane.showMessageDialog(null, "Customer ID required.");
    				} else {
    					Integer.parseInt(customerID);
    				phoneNumber = tfphone.getText();
    				tfphone.setText("");
    				streetAddress = tfaddress.getText();
    				tfaddress.setText("");
    				city = tfcity.getText();
    				tfcity.setText("");
    				state = tfstate.getText();
    				tfstate.setText("");
    				zip = tfzip.getText();
    				tfzip.setText("");
    				if(zip.trim().equals("")) {
    					return;
    				} else {
    					Integer.parseInt(zip);
    				}
    				zipPlus4 = tfzip4.getText();
    				tfzip4.setText("");
    				if(zipPlus4.trim().equals("")) {
    					return;
    				} else {
    					Integer.parseInt(zipPlus4);
    				}
    				}
    			}
    		});
     
     
    		JButton btnAddEmpl = new JButton("Add Employee");
    		btnAddEmpl.setFont(new Font("Tahoma", Font.BOLD, 11));
    		btnAddEmpl.setBounds(158, 239, 117, 40);
    		frame.getContentPane().add(btnAddEmpl);
    		myemployee = new ArrayList<Employee>();
    		btnAddEmpl.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent arg0) {
    				myemployee.add(new Employee(givenName, surname, false, 0));
    				givenName = tfgiven.getText();
    				tfgiven.setText("");
    				surname = tflast.getText();
    				tflast.setText("");
    				employeeID = tfpersonID.getText();
    				tfpersonID.setText("");
    				if(employeeID.trim().equals("")) {
    					JOptionPane.showMessageDialog(null, "Employee ID required.");
    				} else {
    					Integer.parseInt(employeeID);
    				phoneNumber = tfphone.getText();
    				tfphone.setText("");
    				streetAddress = tfaddress.getText();
    				tfaddress.setText("");
    				city = tfcity.getText();
    				tfcity.setText("");
    				state = tfstate.getText();
    				tfstate.setText("");
    				zip = tfzip.getText();
    				tfzip.setText("");
    				if(zip.trim().equals("")) {
    					return;
    				} else {
    					Integer.parseInt(zip);
    				}
    				zipPlus4 = tfzip4.getText();
    				tfzip4.setText("");
    				if(zipPlus4.trim().equals("")) {
    					return;
    				} else {
    					Integer.parseInt(zipPlus4);
    				}
    				}
    			}
    		});
     
    		JButton btnDisplayAll = new JButton("Display All");
    		btnDisplayAll.setForeground(new Color(255, 192, 203));
    		btnDisplayAll.setBackground(new Color(0, 139, 139));
    		btnDisplayAll.setFont(new Font("Tahoma", Font.BOLD, 11));
    		btnDisplayAll.setBounds(320, 239, 105, 40);
    		frame.getContentPane().add(btnDisplayAll);
    		btnDisplayAll.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				for(int i = 0; i < mycustomer.size(); i++) {
    					System.out.println("Customer ID: " + mycustomer.get(i).getCustomerID() +
    							           "\n" + mycustomer.get(i).toString());
    				}
    				for(int j = 0; j < myemployee.size(); j++) {
    					System.out.println("Employee ID: " + myemployee.get(j).getEmployeeID() +
    							           "\n" + myemployee.get(j).toString());
    				}
    			}
    		});
    	}
    }

    The problem I am having right now is that when I enter in information in the text fields and hit the Add Customer button, that data is being sent to the ArrayList<Employee> myemployee instead of mycustomer arrayList and the data that I input and hit the Add Employee button is going off into never never land. Here is an example of what my results look like:

    Customer ID: 0
    null null
    unknown
    unknown
    unknown, unknown, 99999-9999
     
    Employee ID: 0
    some person
    unknown
    unknown
    unknown, unknown, 99999-9999

    So my other issue is getting my inputs to read in the output. The "some person" was an entry I did and hit the Add Customer button, but it showed up in the employee array. This is where I am not sure what direction I should be going.


  2. #2
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: user input going to wrong ArrayList

    Your logic is in the wrong order. You create the new Employee / Customer object before you assign the Strings (such as 'givenName'). Try to limit the scope of your variables by declaring them closer to where they're being used. The reason you're seeing data from one in the other is that you're doing
    create a new employee from the (null) temporary variables
    assign all the temporary variables
    create a new customer from the (assigned for employee) temporary variables
    assign all the temporary variables

Similar Threads

  1. [SOLVED] Returning ArrayList via user input
    By IanSawyer in forum Collections and Generics
    Replies: 4
    Last Post: March 27th, 2012, 05:40 PM
  2. User Input with a Do Loop
    By RadiantChaos in forum Loops & Control Statements
    Replies: 4
    Last Post: March 13th, 2012, 07:14 PM
  3. Need help with user input/output for GUI.
    By Tctwins in forum AWT / Java Swing
    Replies: 6
    Last Post: February 20th, 2012, 04:13 PM
  4. Valid user input
    By ChristopherLowe in forum Java Programming Tutorials
    Replies: 1
    Last Post: June 21st, 2011, 04:53 PM
  5. User Input File Name
    By PineAppleKing in forum Java Theory & Questions
    Replies: 12
    Last Post: June 3rd, 2011, 10:23 AM