More help with looping please.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sandyw
    New Member
    • Mar 2007
    • 122

    More help with looping please.

    I need help in two areas:
    Both are at the bottom of my program where a client has two choice to make,
    1. When the client enter "Y" it takes him to the begin of the program. I would like to know what this is done ie maybe at the part where it ask you do you want to enter a new product.
    2. If the client enters the wrong value or then "Y" or "N" then they are prompted with "error - Please try again" for some reason my no matter what I enter I get the prompted.
    Any help or tips would be helpful and great.

    here is my code so far

    Code:
    package dw;
    
    //import util for Scanner
    import java.util.*;
    
    public class ProjectClass3 {
    
    	/**
    	 * @param args
    	 */
    
    	public static void main(String[] args) {
    		// define some variables to hold the data from the scanner
    		String answer_flag; // scanner yes or no
    		String product_flag; // scanner for product
    		double price_flag; // scanner for price
    		double Tax; // for taxes takes price_flag and convert it to Price
    		String color_flag; // scannerfor color
    		int weight_flag; // scanner for weight
    		String product2_flag; // scanner yes or no
    
    
    		// Prompt the user Do you want to create a new product yes or no
    		// make a scanner for the response
    		Scanner kbd_answer = new Scanner(System.in);
    
    		// add loop info here for result Y or N
    		System.out.print("\nPlease press Enter afer each response");
    		System.out.print("\nDo you want to create a Product? (Y/N): ");
    		answer_flag = kbd_answer.next();
    		while (answer_flag.equalsIgnoreCase("N")){
    			System.out.println("Thank You");
    		System.exit(1);
    		}// close the response loop
    
    
    		/**
    		 * @param args
    		 */
    
    		// Make a scanner for product
    		Scanner kbd_product = new Scanner(System.in);
    
    		/**
    		 * @param args
    		 */
    
    		System.out.print("\nEnter the Product Name: ");
    		product_flag = kbd_product.next();
    
    		// Make a scanner for price
    		Scanner kbd2 = new Scanner(System.in);
    		System.out.print("\nEnter the Price: ");
    		price_flag = kbd2.nextDouble();
    		Tax = price_flag; //new variable
    		Tax = ((int) Tax * 0.08); // taxes on product
    		while (price_flag >= 10000) {
    			System.out.println("Please enter a price below 10000:");
    			price_flag = kbd2.nextDouble(); // if wrong goes back until right
    		}//close the Price loop
    
    		/**
    		 * @param args
    		 */
    
    //		 make a Scanner object for weight
    		Scanner kbd = new Scanner(System.in);
    
    		// prompt the user for data and start adding it to the object
    		System.out.print("\nEnter the Weight: ");
    		weight_flag = kbd.nextInt();
    
    		while (weight_flag >= 150) {
    			System.out.print("Incorrect Weight - try again"); // if weight is greater then return you to input
    			weight_flag = kbd.nextInt();
    		}
    
    
    		/**
    		 * @param args
    		 */
    
    
    		// Prompt the user Do you want to create a new product yes or no
    		// make a scanner for the response
    		Scanner kbd_answer2 = new Scanner(System.in);
    		System.out.print("\nPlease press Enter afer each response");
    		System.out.print("\nDo you want to create a Product? (Y/N): ");//Y on N response
    		product2_flag = kbd_answer2.next();
    		if (product2_flag.equalsIgnoreCase("Y"));
    
    		if (product2_flag.equalsIgnoreCase("N"))
    				System.out.println("Thank You");
    		while (product2_flag != "y" || product2_flag != "Y" || product2_flag != "n" || product2_flag != "N")
    				{System.out.println("Error -  Please try again");
    					product2_flag = kbd_answer2.next();
    				}
    
    
    		System.out.print("Product: ");
    		System.out.println(product_flag);
    		System.out.print("Price: $ ");
    		System.out.println(price_flag);
    
    
    		System.out.print("Tax: ");
    		System.out.println(Tax);
    
    
    
    	}// close the public static void
    
    }// close the public class
    Thanks
    sandy
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    you are using == to compare strings
    Code:
    		while (product2_flag != "y" || product2_flag != "Y" || product2_flag != "n" || product2_flag != "N")
    				{System.out.println("Error -  Please try again");
    					product2_flag = kbd_answer2.next();
    				}
    while compares the references to see of they refer to the same object.
    You need to compare the strings themselves using method equals(), e.g.
    Code:
     while (!(product2_flag.equals("y") || product2_flag.equals("Y") || product2_flag.equals("n") || product2_flag.equals("N")))
                                    {System.out.println("Error -  Please try again");
                                            product2_flag = kbd_answer2.next();
                                    }

    Comment

    • sandyw
      New Member
      • Mar 2007
      • 122

      #3
      Thanks for the info
      Infact I saw this done, and I forgot about it and I was going to do a test on it.
      Now if I can figure out how to make the program go back to the begin it will make my day.

      sandy

      Comment

      • Ganon11
        Recognized Expert Specialist
        • Oct 2006
        • 3651

        #4
        Your loop has to encompass the entire portion you will be repeating. Think about it: the statements you want to continually execute are not the System.exit() commands, they are the 'meat' of the program! So the 'meat' should be enclosed in the loop, not the exit. You should exit after the loop - after the user has decided to stop entering data and computing.

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by sandyw
          I need help in two areas:
          Both are at the bottom of my program where a client has two choice to make,
          1. When the client enter "Y" it takes him to the begin of the program. I would like to know what this is done ie maybe at the part where it ask you do you want to enter a new product.
          2. If the client enters the wrong value or then "Y" or "N" then they are prompted with "error - Please try again" for some reason my no matter what I enter I get the prompted.
          Any help or tips would be helpful and great.

          here is my code so far

          Code:
          package dw;
           
          //import util for Scanner
          import java.util.*;
           
          public class ProjectClass3 {
           
          	/**
          	 * @param args
          	 */
           
          	public static void main(String[] args) {
          		// define some variables to hold the data from the scanner
          		String answer_flag; // scanner yes or no
          		String product_flag; // scanner for product
          		double price_flag; // scanner for price
          		double Tax; // for taxes takes price_flag and convert it to Price
          		String color_flag; // scannerfor color
          		int weight_flag; // scanner for weight
          		String product2_flag; // scanner yes or no
           
           
          		// Prompt the user Do you want to create a new product yes or no
          		// make a scanner for the response
          		Scanner kbd_answer = new Scanner(System.in);
           
          		// add loop info here for result Y or N
          		System.out.print("\nPlease press Enter afer each response");
          		System.out.print("\nDo you want to create a Product? (Y/N): ");
          		answer_flag = kbd_answer.next();
          		while (answer_flag.equalsIgnoreCase("N")){
          			System.out.println("Thank You");
          		System.exit(1);
          		}// close the response loop
           
           
          		/**
          		 * @param args
          		 */
           
          		// Make a scanner for product
          		Scanner kbd_product = new Scanner(System.in);
           
          		/**
          		 * @param args
          		 */
           
          		System.out.print("\nEnter the Product Name: ");
          		product_flag = kbd_product.next();
           
          		// Make a scanner for price
          		Scanner kbd2 = new Scanner(System.in);
          		System.out.print("\nEnter the Price: ");
          		price_flag = kbd2.nextDouble();
          		Tax = price_flag; //new variable
          		Tax = ((int) Tax * 0.08); // taxes on product
          		while (price_flag >= 10000) {
          			System.out.println("Please enter a price below 10000:");
          			price_flag = kbd2.nextDouble(); // if wrong goes back until right
          		}//close the Price loop
           
          		/**
          		 * @param args
          		 */
           
          //		 make a Scanner object for weight
          		Scanner kbd = new Scanner(System.in);
           
          		// prompt the user for data and start adding it to the object
          		System.out.print("\nEnter the Weight: ");
          		weight_flag = kbd.nextInt();
           
          		while (weight_flag >= 150) {
          			System.out.print("Incorrect Weight - try again"); // if weight is greater then return you to input
          			weight_flag = kbd.nextInt();
          		}
           
           
          		/**
          		 * @param args
          		 */
           
           
          		// Prompt the user Do you want to create a new product yes or no
          		// make a scanner for the response
          		Scanner kbd_answer2 = new Scanner(System.in);
          		System.out.print("\nPlease press Enter afer each response");
          		System.out.print("\nDo you want to create a Product? (Y/N): ");//Y on N response
          		product2_flag = kbd_answer2.next();
          		if (product2_flag.equalsIgnoreCase("Y"));
           
          		if (product2_flag.equalsIgnoreCase("N"))
          				System.out.println("Thank You");
          		while (product2_flag != "y" || product2_flag != "Y" || product2_flag != "n" || product2_flag != "N")
          				{System.out.println("Error - Please try again");
          					product2_flag = kbd_answer2.next();
          				}
           
           
          		System.out.print("Product: ");
          		System.out.println(product_flag);
          		System.out.print("Price: $ ");
          		System.out.println(price_flag);
           
           
          		System.out.print("Tax: ");
          		System.out.println(Tax);
           
           
           
          	}// close the public static void
           
          }// close the public class
          Thanks
          sandy
          When exiting from the program System.exit(0) not System.exit(1).

          May I also say that it is better to declare variables closest to where they are used in the Java program. It makes the code easier to read then.

          Comment

          • sandyw
            New Member
            • Mar 2007
            • 122

            #6
            Thanks everyone for the great help.

            Comment

            Working...