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: If Statements Using Elements in an Array

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default If Statements Using Elements in an Array

    Hi everyone,

    Have a program that requires data validation from user inputs and currently the way I have it setup is with "magic numbers". In the do-while loops there is an if statement that will make sure that the user input is not outside of the required range and is using "magic numbers" that correspond with the elements in each array. I have tried to make the if statement range work using .length() method or using the element index [#] but both havent worked. Is there anything that can be placed within the if so that the range will be based off the elements within the array, and not have to use these random number values that are related to the array elements but not those exact elements?

    Here is the program:
    import java.util.Scanner;
    import java.text.DecimalFormat;
    public class A_G_RestaurantOrderv6
    {
     
    	public static void main(String[] args)
    	{
    		//Display and introduce the program to the user via title.
    		System.out.println("\t\t\t\t\t*****************************");
    		System.out.println("\t\t\t\t\t*   Welcome to Cafč Java!   *");
    		System.out.println("\t\t\t\t\t*****************************");
     
    		//Create Scanner object and DecimalFormat object.
    		Scanner keyinput = new Scanner(System.in);
     
    		/*Creating a DecimalFormat object for each sub menu's prices. While this could be done with
    		 *one generalized DecimalFormat object, I prefer to make one for each that way it can be easier to
    		 *identify and it just fits my coding style.
    		 */ 		
    		DecimalFormat currencyFormat = new DecimalFormat ("$0.00"); 
     
    		//Provide instructions to the user and ask them to enter the number of people in their group.
    		System.out.print("\nHow many people are in your group? ");
    		int peopleInGroup = keyinput.nextInt();
     
    		System.out.println("Each person must order an item from the following menu categories: appetizers, entrčes, desserts and beverages.");
     
    		//Create an accumulator variable that will add the price of each diner's selection.
    		double totalBill = 0.0;
     
    		//Set up a main for loop that will run code based on the number of people in a group.
    		for(int i = 1; i <= peopleInGroup; i++)
    		{
    			System.out.println("\nDiner # " + i + ", what will your order consist of?");
    			System.out.println("*******************************************");
     
    			//Create and instantaniate eight arrays: four to hold String variables for menu items, and four double arrays to hold prices of those menu items.
    			String[] appetizerNamesArray = new String[] {null, "No Selection", "Deep Fried Calamari", "Soup du Jour", "Garden Salad", "Garlic Bread"};
    			String[] entreeNamesArray = new String[] {null, "No Selection", "Rib-Steak", "Fettuccini Alfredo", "Pan-Fried Sole", "Mediterranean Platter", "Vegetarian Lasagna"};
    			String[] dessertNamesArray = new String[] {null, "No Selection", "Ice Cream Sundae", "Cheesecake", "Chocolate Truffle Cake", "Raspberry Mousse"};
    			String[] beverageNamesArray = new String[] {null, "No Selection", "Water", "Juice", "Pop", "Milk", "Coffee", "Tea"};	
     
    			double[] appetizerPriceArray = new double[] {0.00, 0.00, 7.50, 4.99, 3.99, 4.50};
    			double[] entreePriceArray = new double[] {0.00, 0.00, 15.95, 11.25, 17.95, 13.50, 9.00};
    			double[] dessertPriceArray = new double[] {0.00, 0.00, 2.95, 5.00, 6.00, 4.50};
    			double[] beveragePriceArray = new double[] {0.00, 0.00, 0.00, 2.00, 2.00, 2.00, 1.75, 1.75};
     
    			//Display appetizer menu for the diner and take the diner's selection off the menu.
    			System.out.println("\n\t*****************************");
    			System.out.println("\t*     Appetizer Sub-Menu    *");
    			System.out.println("\t*****************************");
     
    			//Print out the element that contains the menu item and its price, formatted to the currencyFormat.
    			System.out.println("\t1. ** " + appetizerNamesArray[1] + " ** " + currencyFormat.format(appetizerPriceArray[1])); 
    			System.out.println("\t2. " + appetizerNamesArray[2] + " " + currencyFormat.format(appetizerPriceArray[2]));
    			System.out.println("\t3. " + appetizerNamesArray[3] + " " + currencyFormat.format(appetizerPriceArray[3]));
    			System.out.println("\t4. " + appetizerNamesArray[4] + " " + currencyFormat.format(appetizerPriceArray[4]));
    			System.out.println("\t5. " + appetizerNamesArray[5] + " " + currencyFormat.format(appetizerPriceArray[5]));
     
    			//Create a do-while loop that will validate the data prior to entering it into the accumulator for the totalBill. It will keep the user
    			//inside of the loop until a valid selection is inputted.
    			int appetizerSelection;
    			boolean badDataFlag = true;
    			System.out.print("\nPlease select ONE item from the Appetizer menu #: ");
    			do
    			{
    				appetizerSelection = keyinput.nextInt();	
    					if(appetizerSelection < 1 || appetizerSelection > 5)
    					{
    						System.out.print("That is not a valid selection, please choose ONE item from the Entrče menu #: ");
    					}
    					else
    					{
    						badDataFlag = false;
    					}
    			}while (badDataFlag == true);
     
    			totalBill += appetizerPriceArray[appetizerSelection]; //Add the selected item's price, from the menu, to the totalBill.
     
    			//Display entrče menu for the diner and take the diner's selection from the menu.
    			System.out.println("\n\t*****************************");
    			System.out.println("\t*      Entrče Sub-Menu      *");
    			System.out.println("\t*****************************");
    			System.out.println("\t1. ** " + entreeNamesArray[1] + " ** " + currencyFormat.format(entreePriceArray[1]));
    			System.out.println("\t2. " + entreeNamesArray[2] + " " + currencyFormat.format(entreePriceArray[2]));
    			System.out.println("\t3. " + entreeNamesArray[3] + " " + currencyFormat.format(entreePriceArray[3]));
    			System.out.println("\t4. " + entreeNamesArray[4] + " " + currencyFormat.format(entreePriceArray[4]));
    			System.out.println("\t5. " + entreeNamesArray[5] + " " + currencyFormat.format(entreePriceArray[5]));
    			System.out.println("\t6. " + entreeNamesArray[6] + " " + currencyFormat.format(entreePriceArray[6]));
     
    			//Create a do-while loop that will validate the data prior to entering it into the accumulator for the totalBill. It will keep the user
    			//inside of the loop until a valid selection is inputted.
    			int entreeSelection;
    			System.out.print("\nPlease select ONE item from the Entrče menu #: ");
    			do
    			{
    				badDataFlag = true;
    				entreeSelection = keyinput.nextInt();
    					if(entreeSelection < 1 || entreeSelection > 6)
    					{
    						System.out.print("That is not a valid selection, please choose ONE item from the Entrče menu #: ");
    					}
    					else
    					{
    						badDataFlag = false;
    					}
    			}while (badDataFlag == true);
     
    			totalBill += entreePriceArray[entreeSelection]; //Add the selected item's price, from the menu, to the totalBill.
     
    			//Display dessert menu for the diner and take the diner's selection from the menu.
    			System.out.println("\n\t*****************************");
    			System.out.println("\t*     Dessert Sub-Menu      *");
    			System.out.println("\t*****************************");
    			System.out.println("\t1. ** " + dessertNamesArray[1] + " ** " + currencyFormat.format(dessertPriceArray[1]));
    			System.out.println("\t2. " + dessertNamesArray[2] + " " + currencyFormat.format(dessertPriceArray[2]));
    			System.out.println("\t3. " + dessertNamesArray[3] + " " + currencyFormat.format(dessertPriceArray[3]));
    			System.out.println("\t4. " + dessertNamesArray[4] + " " + currencyFormat.format(dessertPriceArray[4]));
    			System.out.println("\t5. " + dessertNamesArray[5] + " " + currencyFormat.format(dessertPriceArray[5]));
     
    			//Create a do-while loop that will validate the data prior to entering it into the accumulator for the totalBill. It will keep the user
    			//inside of the loop until a valid selection is inputted.
    			int dessertSelection;
    			System.out.print("\nPlease select ONE item from the Dessert menu #: ");
    			do
    			{
    				badDataFlag = true;
    				dessertSelection = keyinput.nextInt();
    					if(dessertSelection < 1 || dessertSelection > 5)
    					{
    						System.out.print("That is not a valid selection, please choose ONE item from the Entrče menu #: ");
    					}
    					else
    					{
    						badDataFlag = false;
    					}
    			}while (badDataFlag == true);
     
    			totalBill += dessertPriceArray[dessertSelection]; //Add the selected item's price, from the menu, to the totalBill.
     
    			//Display dessert menu for the diner and take the diner's selection from the menu.
    			System.out.println("\n\t*****************************");
    			System.out.println("\t*     Beverage Sub-Menu     *");
    			System.out.println("\t*****************************");
    			System.out.println("\t1. ** " + beverageNamesArray[1] + " ** " + currencyFormat.format(beveragePriceArray[1]));
    			System.out.println("\t2. " + beverageNamesArray[2] + " " + currencyFormat.format(beveragePriceArray[2]));
    			System.out.println("\t3. " + beverageNamesArray[3] + " " + currencyFormat.format(beveragePriceArray[3]));
    			System.out.println("\t4. " + beverageNamesArray[4] + " " + currencyFormat.format(beveragePriceArray[4]));
    			System.out.println("\t5. " + beverageNamesArray[5] + " " + currencyFormat.format(beveragePriceArray[5]));
    			System.out.println("\t6. " + beverageNamesArray[6] + " " + currencyFormat.format(beveragePriceArray[6]));
    			System.out.println("\t7. " + beverageNamesArray[7] + " " + currencyFormat.format(beveragePriceArray[7]));
     
    			//Create a do-while loop that will validate the data prior to entering it into the accumulator for the totalBill. It will keep the user
    			//inside of the loop until a valid selection is inputted.
    			int beverageSelection;
    			System.out.print("\nPlease select ONE item from the Beverage menu #: ");
    			do
    			{
    				badDataFlag = true;
    				beverageSelection = keyinput.nextInt();
    					if(beverageSelection < 1 || beverageSelection > 7)
    					{
    						System.out.print("That is not a valid selection, please choose ONE item from the Entrče menu #: ");
    					}
    					else
    					{
    						badDataFlag = false;
    					}
    			}while (badDataFlag == true);
     
    			totalBill += beveragePriceArray[beverageSelection]; //Add the selected item's price, from the menu, to the totalBill.
     
    			//Display diner's order.
    			if(appetizerSelection == 1 && entreeSelection == 1 && dessertSelection == 1 && beverageSelection == 1)
    			{
    				System.out.println();
    			}
    			else
    			{
    				System.out.println("\nThank you. Your order will consist of:");
    			}//end thank you message if
     
    			if (appetizerSelection != 1)
    			{
    				System.out.println("\t" + appetizerNamesArray[appetizerSelection]);
    			}//end appetizer if
     
    			if (entreeSelection != 1)
    			{
    				System.out.println("\t" + entreeNamesArray[entreeSelection]);
    			}//end entree if
     
    			if (dessertSelection != 1)
    			{
    				System.out.println("\t" + dessertNamesArray[dessertSelection]);
    			}//end dessert if
     
    			if (beverageSelection != 1)
    			{
    				System.out.println("\t" + beverageNamesArray[beverageSelection]);
    			}//end beverage if
    		}//end main for
    		//Output the total bill for all the diners.
    		System.out.println("-------------------------------------------------");
    		System.out.println("Order completed. Your total bill comes to " + currencyFormat.format(totalBill));
    		System.out.println("-------------------------------------------------");
    	}//end main
    }//end class

    If anyone has any suggestions, they'd be much appreciated!

    Thanks,
    Aaron


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: If Statements Using Elements in an Array

    Whoa -- that's a lot of unformatted code up there! Please edit your original post (press the Edit Post button at the bottom of your question) and surround your pasted code with code tags:

    [code=java]

    // .... your code goes here

    [/code]

  3. #3
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: If Statements Using Elements in an Array

    The min of the range pretty much has to be a magic number as far as i can see. Unless you create a method that specifically gets the min of an array based on null objects. For the max, it seems like you were on the right track with the length method but i'm guessing you were just using it wrong. Without seeing what you tried i can really explain to you why it went wrong. Below is the line formatted as it should be. Let me know if this works fro you and if there is any part of it that you dont understand and i'll be happy to explain it to you. Repeat as needed for the other if statements.

    if(appetizerSelection < 1 || appetizerSelection > appetizerNamesArray.length)
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

Similar Threads

  1. How to sum up elements in an array?
    By D-X69 in forum Java Theory & Questions
    Replies: 2
    Last Post: May 28th, 2012, 01:21 AM
  2. [SOLVED] How To Increment Array Elements
    By Nuggets in forum Java Theory & Questions
    Replies: 15
    Last Post: April 1st, 2012, 12:10 PM
  3. Missing elements in array
    By frozen java in forum What's Wrong With My Code?
    Replies: 6
    Last Post: February 10th, 2012, 11:52 PM
  4. Array elements comparison
    By Pradeep_Parihar in forum Java Theory & Questions
    Replies: 1
    Last Post: December 10th, 2011, 09:45 AM
  5. Conditional statements on Array list objects
    By jaguarpaw in forum Collections and Generics
    Replies: 3
    Last Post: May 10th, 2011, 09:52 AM