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 3 of 3

Thread: Need help

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Need help

    Here's my code
    public void add(int newID, String newDesc, double newCost)
    	{
    		catalogItems.trimToSize();
    		CatalogItem copy = new CatalogItem(newID, newDesc, newCost);
    		for(int i = 0; i < catalogItems.size();)
    			if(catalogItems.get(i).getItemID() == newID)
    			{
    				i = catalogItems.size();
    				check = true;
    			}
    			else
    			{
    				i++;
    				check = false;
    			}
    			if(check = true)
    				System.err.println("Error, that item ID already exists.");
    			else
    				catalogItems.add(copy);
    	}
    The problem is, on the first if, else statement it seems like my method is completely ignoring it. I tried to test it by printing some guff in both the if and else and I got nothing back, but check is always true no matter what. catalogItems is an <CatalogItem>ArrayList and the CatalogItem object is (int, String, double) if that helps.

  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: Need help

    Here is your code after I 'prettify' it with
    indent --no-tabs tmp.java

    public void
    add (int newID, String newDesc, double newCost)
    {
      catalogItems.trimToSize ();
      CatalogItem copy = new CatalogItem (newID, newDesc, newCost);
      for (int i = 0; i < catalogItems.size ();)
        if (catalogItems.get (i).getItemID () == newID)
          {
            i = catalogItems.size ();
            check = true;
          }
        else
          {
            i++;
            check = false;
          }
      if (check = true)
        System.err.println ("Error, that item ID already exists.");
      else
        catalogItems.add (copy);
    }

    Does that make it more obvious where you've gone wrong? indent does automatic indentation based on code 'depth'. Javac (the Java compiler) ignores whitespace when it compiles your source, so only braces ('{' and '}') affect code depth.

    [edit]I'm barking up the wrong tree. To compare a boolean variable, use '=='. The return value of 'theBoolean = true' is always true
    Last edited by Sean4u; March 2nd, 2012 at 02:25 PM.

  3. The Following User Says Thank You to Sean4u For This Useful Post:

    TheEvilCouncil (March 2nd, 2012)

  4. #3
    Junior Member
    Join Date
    Mar 2012
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Need help

    Oh man I totally missed that, thank you so much!