Question

My problems is that I have a method that enters into an unexpected infinite loop and I can't identify why. The method is supposed to take input from the user and assign it to three different objects. However, after the loop has run twice and has asked the user to input the name for the third object, the program loops infinitely. I should point out that I am still relatively new to java and learning to program in general.

Here is the code(I have started comments with !!!! that I believe are related to the problem):

private void readInput()    
//This method accepts input from the user about product data and sets the values of 
//product1, product2 and product3
//Also validates all input so as not to crash the program if the user enters incorrect data
{       
    String name;        
    int demandRate, productChoice=1; 
    final int MAXPRODUCTS=3;
    double setupCost, unitCost, inventoryCost, sellingPrice;

    Scanner console = new Scanner(System.in);

    while (productChoice <= MAXPRODUCTS)    
    //A loop that makes the user enter data for all three products and then stops after the third entry
    {                                      
        System.out.println("Please enter the product's name: ");
        name = console.next();       
        matesStore.isAProduct(name);     // checks whether the product name the user has entered is unique.
        while (matesStore.isAProduct(name))  
        //If a name that has already been entered is found, the user will be asked to reenter the name
        {
            System.out.println("That name is already in use. Please choose another: "); 
            name = console.next();
        }
        while (!matesStore.isAProduct(name))
        //If a name has not been used, the name is added to the product
        {
            matesStore.addProduct(name); //!!!!I suspect the problem is this line.
        }            
        System.out.println("Please enter the product's demand rate: ");
        demandRate = console.nextInt();                         
        System.out.println("Please enter the product's setup cost: ");
        setupCost = console.nextDouble();
        System.out.println("Please enter the product's unit cost: ");
        unitCost = console.nextDouble();
        System.out.println("Please enter the product's inventory cost: ");
        inventoryCost = console.nextDouble();
        System.out.println("Please enter the product's selling price: ");
        sellingPrice = console.nextDouble();
        matesStore.addData(productChoice, demandRate, setupCost, unitCost, inventoryCost, sellingPrice);    //Uses the method from the Store class to set the data values for the products.
        productChoice++;        
    }
    while (productChoice > MAXPRODUCTS)
    {
        System.out.println("The list is now full.");
        continueOption();
    }         
}//End of Method and Interface class

And this is the methods from the Store class:

public boolean isAProduct(String product)
//Returns true if a name has been found otherwise returns false
{
    boolean found = false;
    int counter = 0;

    while (!found && (counter < MAXNUMBEROFPRODUCTS))
    {
        if (product.equalsIgnoreCase(product1.getName()))
        {
            found = true;
        }
        else if (product.equalsIgnoreCase(product2.getName()))
        {
            found = true;                 
        }
        else 
        {
            counter++;
        }            
    }        

    return found;
}//End of method

public void addProduct(String product)
//If isAProduct() returns false then the product name 
//entered by the user is stored in product1, product2 and product3 respectively

//!!!!I think this is where the problem originates but can't figure out why
{   
    if (numberOfProducts == 0) 
    //!!!!numberOfProducts has been declared as private int numberOfProducts=0;
    //I tried declaring numberOfProducts as a variable within addProduct()
    //but that just set the value to 0 each time and so only the name for product1 was set
    {
        product1.setName(product);
        numberOfProducts++;
    }
    else if (numberOfProducts == 1)
    {
        product2.setName(product);
        numberOfProducts++;
    }
    else if (numberOfProducts == 2)
    {                
        product3.setName(product);
    }            
    else
    {
        System.exit(0);
    }
} 

Any help or advice would be greatly appreciated :)

Cheers

Was it helpful?

Solution

while (!matesStore.isAProduct(name))
        //If a name has not been used, the name is added to the product
        {
            matesStore.addProduct(name); //!!!!I suspect the problem is this line.
        }       

The reason the 3rd product is infinite looping is because you should use If for this statement, while also work but you see there's an error is due to your method addProduct. If you change to If it won't be an infinite loop however your product 3 will be bugged.

public boolean isAProduct(String product)
//Returns true if a name has been found otherwise returns false
{
    boolean found = false;
    int counter = 0;

    while (!found && (counter < MAXNUMBEROFPRODUCTS))
    {
        if (product.equalsIgnoreCase(product1.getName()))
        {
            found = true;
        }
        else if (product.equalsIgnoreCase(product2.getName()))
        {
            found = true;                 
        }
        else 
        {
            counter++;
        }            
    }        

    return found;
}//End of method

Noticed that you don't check for product3, so it will always return false when searching for your 3rd product name.

!matesStore.isAProduct(name) = !false = true

Viola!! There you goes it's while(true), an infinite loop!

Fix by implementing the product3.getName() in your isAProduct() method.

Your code is messy, you should use array for the products and you can do a better search with an array.

public boolean isAProduct(String product)
//Returns true if a name has been found otherwise returns false
{
    boolean found = false;
    int counter = 0;

    while (!found && (counter < MAXNUMBEROFPRODUCTS))
    {
        if (product.equalsIgnoreCase(product1.getName()))
        {
            found = true;
        }
        else if (product.equalsIgnoreCase(product2.getName()))
        {
            found = true;                 
        }
        else if (product.equalsIgnoreCase(product3.getName()))
        {
            found = true;                 
        }
        else 
        {
            counter++;
        }            
    }        

    return found;
}//End of method      
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top