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

Thread: Skipping else if statement

  1. #1
    Junior Member
    Join Date
    May 2014
    Posts
    16
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Skipping else if statement

    Hey guys, I have two different Class object arrays storing 3 elements (2 Store arrays, both initialised to hold 3 Product objects). The problem is, I can set data to one array with no problem but my program doesn't want to set the data for my second array.

    For instance, The main problem is this method:

    public void addData(String store, String newName, int newDemand, double newSetup, double newUnit, double newInventory, double newPrice)
    	    //Method that sets the data value for a product
    	    {
    	    	if (store.equalsIgnoreCase("callaghan"))//!!!!Goes from this line in the debugger
    	    		if (noOfProductsCallaghan==0) 
    	    		{
    	    			setData(product[0], newName, newDemand, newSetup, newUnit, newInventory, newPrice);
    	    			noOfProductsCallaghan++;
    	    		}
    	    		else if (noOfProductsCallaghan==1) 
    	    		{
    	    			setData(product[1], newName, newDemand, newSetup, newUnit, newInventory, newPrice);
    	    			noOfProductsCallaghan++;
    	    		}
    	    		else if (noOfProductsCallaghan==2) 
    	    		{
    	    			setData(product[2], newName, newDemand, newSetup, newUnit, newInventory, newPrice);  
    	    			noOfProductsCallaghan++;
    	    		}
    	    	else if (store.equalsIgnoreCase("lambton"))
    	    		if (noOfProductsLambton==0) 
    	    		{
    	    			setData(product[0], newName, newDemand, newSetup, newUnit, newInventory, newPrice);
    	    			noOfProductsLambton++;
    	    		}
    	    		else if (noOfProductsLambton==1) 
    	    		{
    	    			setData(product[1], newName, newDemand, newSetup, newUnit, newInventory, newPrice);
    	    			noOfProductsLambton++;
    	    		}
    	    		else if (noOfProductsLambton==2) 
    	    		{
    	    			setData(product[2], newName, newDemand, newSetup, newUnit, newInventory, newPrice);  
    	    			noOfProductsLambton++;
    	    		}
    	    }//!!!!Straight to this line in the debugger

    Store will always either be "callaghan" or "lambton". When store is equals to "callaghan" it sets the data perfectly. But when store is equals to "lambton", no data is set and I can't figure out why.

    In case it helps, here is the default constructor for a Product object as well as my declaration of the array:

    public Product()    
        {
            name = "No name yet";
            demandRate = 0;        
            setupCost = 0;
            unitCost = 0;        
            inventoryCost = 0;
            sellingPrice = 0;        
        }

    Product[] product = new Product[3];

    Any help would be greatly appreciated.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Skipping else if statement

    Your indentation is misleading. Your outer if and else/if statements are missing curly brackets. Your code is actually doing this:

    if (store.equalsIgnoreCase("callaghan"))
    	    		if (noOfProductsCallaghan==0) 
    	    		{
    	    			setData(product[0], newName, newDemand, newSetup, newUnit, newInventory, newPrice);
    	    			noOfProductsCallaghan++;
    	    		}
    	    		else if (noOfProductsCallaghan==1) 
    	    		{
    	    			setData(product[1], newName, newDemand, newSetup, newUnit, newInventory, newPrice);
    	    			noOfProductsCallaghan++;
    	    		}
    	    		else if (noOfProductsCallaghan==2) 
    	    		{
    	    			setData(product[2], newName, newDemand, newSetup, newUnit, newInventory, newPrice);  
    	    			noOfProductsCallaghan++;
    	    		}
    	    		else if (store.equalsIgnoreCase("lambton"))
    	    			if (noOfProductsLambton==0) 
    	    			{
    	    				setData(product[0], newName, newDemand, newSetup, newUnit, newInventory, newPrice);
    	    				noOfProductsLambton++;
    	    			}
    	    			else if (noOfProductsLambton==1) 
    	    			{
    	    				setData(product[1], newName, newDemand, newSetup, newUnit, newInventory, newPrice);
    	    				noOfProductsLambton++;
    	    			}
    	    			else if (noOfProductsLambton==2) 
    	    			{
    	    				setData(product[2], newName, newDemand, newSetup, newUnit, newInventory, newPrice);  
    	    				noOfProductsLambton++;
    	    			}
    	    }

    This is one of the reasons you should ALWAYS use curly brackets after if statements and for loops.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

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

    MrPigeon (June 5th, 2014)

  4. #3
    Junior Member
    Join Date
    May 2014
    Posts
    16
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Skipping else if statement

    Thank you so much for providing a fresh pair of eyes. I spent so much time looking at it I didn't even realise I was missing the curly braces.

  5. #4
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    414
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Re: Skipping else if statement

    Two things:

    if (store.equalsIgnoreCase("callaghan"))
    That looks like the parent of the if/else structure. If all the others are related to it - you probably need
    to check the braces to ensure all statements within this body exist in the same scope of that.

    Also - check if that statement is returning false - remember if statements will only execute if the
    condition is true.

    Wishes Ada xx

    EDIT --

    Darn! Kevin beat me to it Glad it's resolved.
    If to Err is human - then programmers are most human of us all.
    "The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    ."
    Augusta Ada Byron, Lady Lovelace (1851)

  6. The Following User Says Thank You to Ada Lovelace For This Useful Post:

    MrPigeon (June 5th, 2014)

Similar Threads

  1. [SOLVED] My program skipping a for loop
    By Dr.Code in forum What's Wrong With My Code?
    Replies: 5
    Last Post: September 16th, 2012, 09:05 AM
  2. Scanner skipping input
    By hblakek in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: February 21st, 2012, 01:44 PM
  3. Skipping Input Problem
    By Matty Alan in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 15th, 2011, 07:45 AM
  4. [SOLVED] if else skipping
    By Scotty in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 25th, 2010, 08:04 PM
  5. need help prog skipping method
    By Pulse_Irl in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 11th, 2010, 08:57 AM

Tags for this Thread