Results 1 to 8 of 8
  1. #1
    JR2010 is offline Member
    Join Date
    Apr 2011
    Posts
    15
    Rep Power
    0

    Unhappy Having a problem with the loop and how to stop it at the correct place.

    I am having a problem with the loop. The program does what it is supposed to do until the loop. After the loop it is supposed to stop after "please enter the product name or stop to end the program. It does not stop it continues to the next question, please enter product number and then stops and waits for a response. How do I correct this I can not find the error in my code. The code is below. Could you give me a suggestion as to what I should try.
    My teacher said that there was a problem with the break statement and that I should not be using a break statment instead should be using another command there to ask for the product name and to stop the program when someone enters stop, the problem is I don't know of any other commands to stop the program or how to stop this problem. If anyone has a list of java commands and their applications I would really appreciate a copy if you have one. This program is due tonight by 11:59 and I have been working on this problem all week and can not fix it. Please help.



    package product;


    import java.util.Scanner;// program uses class Scanner



    public class product {

    // main method begins execution of Java application


    public static void main(String[] args) {
    // Multiplication program to calculate payroll

    // create Scanner to obtain input from command window

    Scanner input = new Scanner(System.in);

    boolean enterMoreData = true;
    while(enterMoreData){

    //get product name or stop program


    System.out.print("Enter product name or stop to quit: " );
    String proName = input.nextLine();

    if (proName.equalsIgnoreCase("stop"))
    {
    System.out.println(" Good bye! Thank You for using inventory organizer.");
    break;
    }


    // get product number
    double proNumber;

    do
    {

    System.out.println(" Please enter the product identification number for inventory: ");
    proNumber = input.nextDouble();
    if (proNumber < 0) {
    System.out.println("number must be positive, please try again. ");
    }
    }while (proNumber < 0);

    int proQuant;
    // get product quantity in inventory
    do
    {
    System.out.println(" Please enter the quantity of product in inventory: ");
    proQuant = input.nextInt();
    if (proQuant < 0) {
    System.out.println("number must be positive, please try again. ");
    }

    }
    while ( proQuant < 0);

    double proPrice;
    // get product
    do
    {
    System.out.println("Please enter product price: ");
    proPrice = input.nextDouble();
    if (proPrice < 0) {
    System.out.println("number must be positive, please try again.");
    }

    }

    while ( proPrice < 0);

    Item item = new Item (proName, proNumber, proQuant, proPrice);

    System.out.printf("Product name is: %s\n Product number is : %.2f\n "
    + "Product quantity in stock is: %.2f\n Product price is: %.2f\n Total of products"
    + " in inventory is: $ %.2f\n ",
    item.proName, item.proNumber, item.proQuantity, item.proPrice, item.getproTotal());



    }// end while
    } //end method main

    } //end class product

    class Item //creat new class Product
    {
    //input variables

    String proName;
    double proNumber;
    double proQuantity;
    double proPrice;
    double proTotal;

    // Get input

    public Item(String name, double number, double quant, double price)
    {
    this.proName = name;
    this.proNumber = number;
    this.proQuantity = quant;
    this.proPrice = price;
    computeproTotal();
    }
    // method to compute proTotal
    private void computeproTotal()
    {
    proTotal = proQuantity * proPrice;
    }
    // method to get Total
    public double getproTotal()
    {
    return proTotal;

    }
    }// end class Product

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    Here's your problem:
    Java Code:
    if (proName.equalsIgnoreCase("stop"))
    {
    System.out.println(" Good bye! Thank You for using inventory organizer.");
    [COLOR="red"]break;[/COLOR]
    }

    I guess you assumed that 'break;' would end the program, but it doesn't. break; in your case only ends your while loop. You need to use System.exit(0); to exit the program.

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    As ozzy said, break ends the loop and continues at the first line outside the loop.

    Also, when posting code please use code tags.

    A few tips that aren't similar to your question. The name variables in the item class should probably not be proXxx, it's not much more to say product, if you get too comfortable with abbreviations you may end up with confusing code.

    The constructor for item can be changed as well, if the argument variables are different than the classes instance variables you don't need to use this. You can make the arguments to the constructors the same as the classes variables and use this, it makes it so you have to think of less variable names.
    Last edited by sunde887; 04-01-2011 at 05:06 PM.

  4. #4
    JR2010 is offline Member
    Join Date
    Apr 2011
    Posts
    15
    Rep Power
    0

    Default Thanks

    Ozzie,

    Thank you for your suggestion. It solves the problem of exiting the program, but the other problem is still there. When it comes back through the loop it is supposed to stop and wait for a responce after Enter product name or stop to exit. It does not stop. It continues to the product number then stops and waits for the number. How do I get the program to wait for the responce to the first question?

    sunde887

    I am very new to this and this is the first program I have written and the first time I have posted a question to this forum. What are code tags? You asked me to use them when posting code.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,407
    Rep Power
    20

    Default

    What are code tags?
    Use code tags to post codes -- [code]CODE[/code] will display as
    Java Code:
    CODE
    db

  6. #6
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    Try to make your code more readable so that we may understand it. Here's an example:

    Java Code:
    //class variable
    private boolean enterMoreData = true;
    private boolean validNumber = false;
    Java Code:
    //main method
    Scanner input = new Scanner(System.in);
    
    while (enterMoreData) {
        System.out.print("input data: ");
        String userInput1 = input.nextLine();
        evaluate(userInput1);
        do {
            System.out.print("input double: ");
            Double userInput2 = input.nextDouble();
            evaluate(userInput2);
        } while (validNumber == false);
    }
    
    System.out.println("Good bye! Thank You for using inventory organizer.");
    System.exit(0);
    Java Code:
    //external methods
    public void evaluate(String inputString) {
        if (inputString.equalsIgnoreCase("stop"))
        {
            enterMoreData = false;
        }
    }
    public void evaluate(Double inputDouble) {
        if (inputDouble < 0) {
            System.out.println("Number must be positive, please try again.");
        } else {
            validNumber = true;
        }
    }

    Quote Originally Posted by JR2010 View Post
    It solves the problem of exiting the program, but the other problem is still there. When it comes back through the loop it is supposed to stop and wait for a responce after Enter product name or stop to exit. It does not stop. It continues to the product number then stops and waits for the number. How do I get the program to wait for the responce to the first question?

    Does this happen from the 2nd time onwards, or right from the start? I'm asking this because, usually when people have this problem they need to follow an input number e.g. Scanner.nextInt() with a nextLine() just to pass the new-line character, before they can get the next String with another nextLine().
    Last edited by ozzyman; 04-01-2011 at 07:59 PM.

  7. #7
    JR2010 is offline Member
    Join Date
    Apr 2011
    Posts
    15
    Rep Power
    0

    Default Problem 2nd time around

    The problem occurs the second time around. It stops where it should the first time. The code below shows where it should stop and wait for a responce but it is not.

    Java Code:
     
         System.out.print("Enter product name or stop to quit: " );  //Should stop here and wait for responce before going on. 
         String proName = input.nextLine();
    
         if (proName.equalsIgnoreCase("stop")) {
         System.out.println(" Good bye! Thank You for using inventory  organizer.");
         System.exit(0);}

  8. #8
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    Yes we got that, so I think that confirms that you should follow each instance of Scanner.nextObject() with Scanner.nextLine().

    e.g.
    myInt = Scanner.nextInt();
    Scanner.nextLine() //eats line character

    myString = Scanner.nextLine();
    //no need to follow-up as line-break character has been taken in

Similar Threads

  1. how to stop for loop
    By rjagan in forum New To Java
    Replies: 3
    Last Post: 04-01-2011, 04:15 PM
  2. help!, while loop wont stop!
    By crank6015 in forum New To Java
    Replies: 4
    Last Post: 03-24-2011, 02:27 AM
  3. Use stop button to stop moving (stop timers) on JPanel
    By mneskovic in forum New To Java
    Replies: 3
    Last Post: 07-23-2010, 12:50 PM
  4. Replies: 21
    Last Post: 06-11-2009, 02:18 AM
  5. How to place the GUI components in correct order
    By impact in forum New To Java
    Replies: 2
    Last Post: 05-04-2008, 06:41 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
  •