Results 1 to 4 of 4
  1. #1
    bdl1127 is offline Member
    Join Date
    Jan 2012
    Posts
    26
    Rep Power
    0

    Default Issue with ArrayList contents

    All,

    Im working on a project that allows the user to build Tables with fields of certain data types. For example, a user input of "Define table emp having fields (bday integer, zip integer) would create a table with the name "emp" and two fields. (Field 1 Name: bday, Field 1 Type integer). The Fields are stored in an Array List. Currently, my program allows the user to create a table with fields of the same name, however it is not supposed to do that. In other words, a table cant have multiple fields with the same name. 3 of my classes are posted below. The fields are added to the ArrayList in the "Table" specifically in the "addField" and "splitFieldList" method. For some reason I cant find a solution to this problem. I have tried "contains" and for loops and cant seem to get it right. I want it to throw an exception if the user tries to define a table with fields of the same name. I feel like it is a simple solution...I just need some fresh eyes. Any help is appreciated.

    Table Class:

    Java Code:
     
    package two.tables;
    
    import java.util.ArrayList;
    
    import two.fields.*;
    
    import java.util.regex.*;
    
    import one.WrongException;
    
    /**
     * Stores fields and creates Table object.
     */
    public class Table {
    
    	private ArrayList<Field> fieldCollector = new ArrayList<Field>();
    
    	private String tableName;
    
    	private Pattern pattern = Pattern.compile("char\\s*\\(\\s*([0-9]*)\\s*\\)");
    
    	/**
    	 * Instantiates a new table.
    	 * 
    	 * @param tName
    	 *            the name of the table
    	 * @param fList
    	 *            the list of fields within table
    	 * @throws WrongException
    	 *             the wrong exception if fList is entered incorrectly
    	 */
    	public Table(String tName, String fList) throws WrongException {
    		splitFieldList(fList);
    		tableName = tName;
    	}
    
    	public Table(String tName) {
    		tableName = tName;
    	}
    
    	/**
    	 * Splits field list and adds name and data type to table.
    	 * 
    	 * @param fList
    	 *            the single string of fields to be split
    	 * @throws WrongException
    	 *             the wrong exception if fields are incorrect
    	 */
    	public void splitFieldList(String fList) throws WrongException {
    		for (String x : fList.split(",")) {
    			String[] fields = x.trim().split("\\s+");
    			    addField(fields[0], fields[1]);
    			    }
    		}
    
    
    	/**
    	 * Determines if char data type is entered correctly
    	 * 
    	 * @param fName
    	 *            the field name
    	 * @param input
    	 *            the char data type entered by user
    	 * @return the char field
    	 */
    	public CharField matchesCharField(String fName, String input) {
    		Matcher matcher = pattern.matcher(input.trim());
    		CharField a = null;
    		if (matcher.matches()) {
    			int size = Integer.parseInt(matcher.group(1));
    			a = new CharField(fName, "char", size);
    		}
    		return a;
    	}
    
    	/**
    	 * Generates field based on data type and name.
    	 * 
    	 * @param fName
    	 *            the field name
    	 * @param fType
    	 *            the field type
    	 * @throws WrongException
    	 *             the wrong exception if field data type is entered incorrectly
    	 */
    	public void addField(String fName, String fType) throws WrongException {
    		CharField charf = matchesCharField(fName, fType);
    		if (fType.equalsIgnoreCase("boolean")) {
    			fieldCollector.add(new BooleanField(fName));
    		} else if (fType.equalsIgnoreCase("date")) {
    			fieldCollector.add(new DateField(fName));
    		} else if (charf != null) {
    			fieldCollector.add(charf);
    		} else if (fType.equalsIgnoreCase("integer")) {
    			fieldCollector.add(new IntegerField(fName));
    		} else if (fType.equalsIgnoreCase("real")) {
    			fieldCollector.add(new RealField(fName));
    		} else if (fType.equalsIgnoreCase("varchar")) {
    			fieldCollector.add(new VarcharField(fName));
    		} else {
    			throw new WrongException(
    					"One or more of the field types may have been entered incorrectly");
    		}
    		} 
    		
    
    	/**
    	 * Sets the name of a Table.
    	 * 
    	 * @param x
    	 *            the name of the Table
    	 */
    	public void setName(String x) {
    		tableName = x;
    	}
    
    	/*
    	 * (non-Javadoc)
    	 * 
    	 * @see java.lang.Object#toString()
    	 */
    	@Override
    	public String toString() {
    		String str = tableName;
    		for (Field x : fieldCollector) {
    			str += " | " + x.toString();
    		}
    		return str;
    
    	}
    
    	/**
    	 * Returns Fields in XML format
    	 * 
    	 * @return str the fields returned as a String in XML format
    	 */
    	public String toXML() {
    		String str = "\t<TABLE>" + "\n" + "\t\t<TABLENAME>" + tableName
    				+ "</TABLENAME>" + "\n" + "\t\t\t<FIELDS>";
    		for (Field x : fieldCollector) {
    			str += "\n" + x.toXML();
    		}
    		str += "\n\t\t\t</FIELDS>" + "\n" + "\t</TABLE>" + "\n";
    		return str;
    
    	}
    
    }

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: Issue with ArrayList contents

    ArrayList can have duplicate items in it. Why don't you try to use Set instead of a List. If you must use an ArrayList then you have to check the fieldCollector to see if it has a Field with the same field. For this you can use the contains() method. But make sure that you have correctly implements the equals() method in the Field class.

  3. #3
    bdl1127 is offline Member
    Join Date
    Jan 2012
    Posts
    26
    Rep Power
    0

    Default Re: Issue with ArrayList contents

    Quote Originally Posted by wsaryada View Post
    ArrayList can have duplicate items in it. Why don't you try to use Set instead of a List. If you must use an ArrayList then you have to check the fieldCollector to see if it has a Field with the same field. For this you can use the contains() method. But make sure that you have correctly implements the equals() method in the Field class.
    We must use an ArrayList. Ive played around with contains quite a bit and still cant get it to work. Would you use contains in the addFields() method or in the splitFieldList() method? Ive tried both with no success

  4. #4
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: Issue with ArrayList contents

    Did you implements the equals() method in the Field class? You can of course do something like:

    Java Code:
    if (!fieldCollector.contains(field)) {
        fieldCollector.add(field);
    }
    To make the ArrayList.contains() works you must implements the equals() method in the Field class. Here an example how to implements it.

    Java Code:
    class Field {
        String name;
    
        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
    
            Field that = (Field) o;
    
            if (name != null ? !name.equals(that.name) : that.name != null) return false;
    
            return true;
        }
    
        @Override
        public int hashCode() {
            return name != null ? name.hashCode() : 0;
        }
    
    }

Similar Threads

  1. Replies: 1
    Last Post: 07-23-2012, 10:37 AM
  2. Arraylist issue
    By Bellyfish in forum New To Java
    Replies: 3
    Last Post: 05-03-2012, 02:21 PM
  3. copying contents of an ArrayList to another ArrayList
    By ankit1801 in forum New To Java
    Replies: 8
    Last Post: 03-27-2011, 06:07 AM
  4. Replies: 19
    Last Post: 08-10-2010, 08:36 AM
  5. ArrayList issue
    By tomrobpowell in forum Eclipse
    Replies: 2
    Last Post: 05-08-2010, 07:04 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •