13 Replies - 3205 Views - Last Post: 20 April 2012 - 04:11 PM Rate Topic: -----

#1 woodlandsrider  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 20-April 12

I cannot get my program to loop properly.

Posted 20 April 2012 - 09:02 AM

I keep trying to put something like a while loop or something to get the program to loop back to the menu options. What am I missing? I have gone all through my book and spend days trying to write this but I cant seem to get it to work. Another road block I keep hitting is trying to retrieve my customerFile.txt in order to put it on the billing receipt part of the program.

I am not asking for someone to do my project for me, I am simply asking for some guidance or a nudge in the right direction. I will put in the time and effort into getting this program to run!!



import java.util.Scanner;  // Needed for the Scanner class.
import java.io.*;				// Needed for the File I/O classes


public class Samsstore2
{
    public static void main(String[] args) throws IOException
	   
    {
       
        int menuOption = 0;    // Number on the option menu.
        int input;        // Hold user input.
        boolean menu;
		  
                // Create a Scanner object for keyboard input
        Scanner keyboard = new Scanner( System.in );
   

	     
		  
        // Get the menu option from the user.
          System.out.print("Choose one of the following \n" +
                                  "by typing 1, 2, 3, or 4:");
          System.out.println("\n");
            System.out.println("1. Enter Customer Information");
            System.out.println("2. Price Lookup");
            System.out.println("3. Display total bill ");
				System.out.println("4. quit");
          System.out.println();

        menuOption = keyboard.nextInt();

       
          if (menuOption == 1)
            {
				
				
				
				//Variables
				String name;				//Hold customers name 
				String address;			//Hold customers address
        		String email;				//Hold customers email
				String product;			//Hold type of product customer is wanting to purchase
				int quantity;				//Hold quantity of product that the customer is wanting to purchase
				double price;				//Hold price of product that the customer is wanting to purchase
            char repeat;				//Hold the @ symbol 
				String info;				//Hold input
				String customerFile;
			  	String fileName;
		  
		  //Open the file
		  PrintWriter outputFile = new PrintWriter("C:customerFile.txt");
		  
		  //Get the customers name
        System.out.print( "\n" + "Enter Customer name: " );
        name = keyboard.nextLine();	
		  
		  //Write the name to the file.
		  outputFile.println(name);
 
		  //Consume the remaining newline.
		  keyboard.nextLine();
		  	  		  		   
		  //Get the customers address
        System.out.print( "Enter Customer address: " );
        address = keyboard.nextLine();
		  
		  //Write the name to the file.
		  outputFile.println(address);

		  //Get the customers email
        System.out.print( "Enter Customer email: " );
        email = keyboard.nextLine();
		  
		  //Write the name to the file.
		  outputFile.println(email);
		  

		  
		  
		  //Get data and write it to a file.
		  
		  outputFile.close();
		  System.out.println( "\n" +
                                      "This customer has been added \n" +
                                        "to the customer list under (C:customerFile.txt).");		
		  
		  
		  	  
		  
		  		  	
			
				}
            else if (menuOption == 2)
              {
				
				int productPrice; 	//To hold the users choice
				
				
            System.out.print("\n" + "Choose from one of the following products to look up the price: ");
				
				
				//Consume the remaining newline.
		 		keyboard.nextLine();
				
				System.out.println("\n");
				
				//List the products
				System.out.println("1. Shoes");
            System.out.println("2. T-Shirts");
				System.out.println("3. Shorts");
            System.out.println("4. Caps");
				System.out.println("5. Jackets");
            System.out.println();
				
				productPrice = keyboard.nextInt();
				
				switch (productPrice) 
		  {
		  
            case 1:  
					System.out.print("$30");
				break;
            
				case 2: 
			   	System.out.print("$10");
				break;
            
				case 3:    
					System.out.print("$30");
				break;
				
				case 4:                       
					System.out.print("$20");
				break;      
				
				case 5:                       
					System.out.print("$50");
				break;

        		default: 
					System.out.println("You did not choose a valid product");
			
			}		

				
				
            										
			// End		
			System.exit(0);
				
				}
            else if (menuOption == 3)
              {
				
				System.out.print("\n" + "Enter customer's name:");
				String name = keyboard.nextLine();
				
								
				//Consume the remaining newline.
		 		keyboard.nextLine();
				
								
				System.out.print("Enter product: ");
				String product = keyboard.nextLine();
				
				System.out.print("Enter quantity of product: ");
				int quantity = keyboard.nextInt();
				
								
				System.out.print("Enter price of product: ");
				double price = keyboard.nextDouble();
				
				
				
				double totalBill = quantity * price;					
				double totalTax = totalBill * 0.08;							
				double totalCost = totalBill + totalTax;
				double taxRate = 0.08;	

				
				
				System.out.println( "\n" +				
									" \n" + 	 			
									name + "\n" + 		
									
									"Product Purchased" + "     " + 
									"Quantity" + "      " +  
									"Total Cost" + 
									"\n" +

									product +  "                   " +  
									quantity +  "              " + 		
									" $" + String.format("%.2f", totalBill) +  					
									" \n" +
																          
									"Tax (@" + taxRate + "%):" +  "                            $" + String.format("%.2f", totalTax) +  "\n" +
																   									
									"Total Cost: " +  "                            $" + String.format("%.2f", totalCost) + " \n" +

									"");						
						

				
				
				
            }
            else if (menuOption == 4)
            {

            System.out.println("4. Quit");
				
				
				
           } 

        }

  }  



Mod edit - Please :code:

Is This A Good Question/Topic? 1
  • +

Replies To: I cannot get my program to loop properly.

#2 oha055  Icon User is offline

  • D.I.C Regular

Reputation: 48
  • View blog
  • Posts: 250
  • Joined: 02-February 09

Re: I cannot get my program to loop properly.

Posted 20 April 2012 - 09:09 AM

Please use code tags so your code is readable
Was This Post Helpful? 0
  • +
  • -

#3 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • Joined: 10-September 10

Re: I cannot get my program to loop properly.

Posted 20 April 2012 - 10:01 AM

And point to or somehow identify the area you're trying to loop. If you've written a loop and it's not doing what you want, just post that - in code tags.
Was This Post Helpful? 0
  • +
  • -

#4 woodlandsrider  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 20-April 12

Re: I cannot get my program to loop properly.

Posted 20 April 2012 - 10:41 AM

Sorry about not putting it between brackets :( I feel stupid. Well basically the background on the code that I am trying to write is that it is a menu option for a store clerk. The menu should have four options- 1. Enter customer info (into and external file) 2. Product price check 3. Display total bill ( This is where I come to another snag. Once in this option, the user inputs the customers name and it pulls it from the file created in option to put it at the top of the bill, Once doing that the user is presented with another two questions, "What product?" and "What quantity?". The program then needs to pull the price of the product and multiply it with the quantity to produce a total bill. This total bill will look something like this----

John Doe
2206 Anywhere Street
Anywhere, TX. 88888
john.doe@anywhere.com
Product Purchased Quantity Total Cost
Shoes 2 $100.00
Shorts 1 $75.00
Total Cost: $175.00


My problem that I am running into with loops is that I need to loop the "menuOptions" so that at the end of each of them it will return the user to the first layout of menuOptions except for if they choose option 4-to quit.

Again, I don't want anyone to just tell me the code, I want to learn this real bad! Any help is appreciated as well as any references to the java programming text book by Gaddis. I will do anything to get this right.
Was This Post Helpful? 0
  • +
  • -

#5 xclite  Icon User is offline

  • LIKE A BOSS
  • member icon


Reputation: 894
  • View blog
  • Posts: 3,153
  • Joined: 12-May 09

Re: I cannot get my program to loop properly.

Posted 20 April 2012 - 10:43 AM

Holy spaghetti code, Batman! Also, I don't actually see a loop instruction in your entire program... which line is the loop on?
Was This Post Helpful? 1
  • +
  • -

#6 woodlandsrider  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 20-April 12

Re: I cannot get my program to loop properly.

Posted 20 April 2012 - 10:57 AM

View Postxclite, on 20 April 2012 - 10:43 AM, said:

Holy spaghetti code, Batman! Also, I don't actually see a loop instruction in your entire program... which line is the loop on?



I have not added the loops yet, as when I did before it screwed up the code even more :( I believe that they should go somewhere around lines 88, 148, and 206.

I really appreciate all the quick responses. It's hard for me to get tutoring with my schedule.
Was This Post Helpful? 0
  • +
  • -

#7 xclite  Icon User is offline

  • LIKE A BOSS
  • member icon


Reputation: 894
  • View blog
  • Posts: 3,153
  • Joined: 12-May 09

Re: I cannot get my program to loop properly.

Posted 20 April 2012 - 11:03 AM

I'd be happy to take a look, but I'm pulling the "lasered eyes get tired fast" excuse. Without trying to align the weird spacing, I'd say you want to look somewhere after you instantiate the Scanner object. Since your prompt comes after that, I assume you want most of that logic to be repeated for each input. I see you have a boolean called menu - you want to set that to false whenever an input is entered that ends input, and use it as the loop's control variable (the condition that decides whether to loop again or not).
example:
menu = true;
while(menu) {
//menu display/execute logic
//...
//
if (menuOption == 4)
  menu = false;

}



Further, I would not call System.exit anywhere in the loop - just let the program exit after the user selects quit by exiting the loop.
Was This Post Helpful? 0
  • +
  • -

#8 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • Joined: 10-September 10

Re: I cannot get my program to loop properly.

Posted 20 April 2012 - 11:04 AM

And you're doing all of this in the main() method, without other methods or classes? Do you have to? That's so painful, but I accept (barely) that you have to learn to crawl before walking. A preferred alternative approach would be to use other classes to capture the characteristics of like objects and methods to do specific things: get the main menu choice, write to file, get products/prices, etc.

And you're not storing any of the customer or product data in memory for program use? Just in a file? Wow. Can you use other classes or arrays? I can't get into the workings of your program right now, but what you've outlined seems much harder than it should be.
Was This Post Helpful? 1
  • +
  • -

#9 woodlandsrider  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 20-April 12

Re: I cannot get my program to loop properly.

Posted 20 April 2012 - 11:45 AM

View Postxclite, on 20 April 2012 - 11:03 AM, said:

I'd be happy to take a look, but I'm pulling the "lasered eyes get tired fast" excuse. Without trying to align the weird spacing, I'd say you want to look somewhere after you instantiate the Scanner object. Since your prompt comes after that, I assume you want most of that logic to be repeated for each input. I see you have a boolean called menu - you want to set that to false whenever an input is entered that ends input, and use it as the loop's control variable (the condition that decides whether to loop again or not).
example:
menu = true;
while(menu) {
//menu display/execute logic
//...
//
if (menuOption == 4)
  menu = false;

}



Further, I would not call System.exit anywhere in the loop - just let the program exit after the user selects quit by exiting the loop.



Alright, so I went through and added the menu = true to look like this---
int menuOption = 0;    // Number on the option menu.
        int input;        // Hold user input.
        boolean menu;
		  
                // Create a Scanner object for keyboard input
        Scanner keyboard = new Scanner( System.in );
   

	     menu = true;
		  
		  while(menu)
		  
        // Get the menu option from the user.
          System.out.print("Choose one of the following \n" +
                                  "by typing 1, 2, 3, or 4:");
          System.out.println("\n");
            System.out.println("1. Enter Customer Information");
            System.out.println("2. Price Lookup");
            System.out.println("3. Display total bill ");
				System.out.println("4. quit");
          System.out.println();



and then after the option 4--
 }
            else if (menuOption == 4)
            {
				menu = false;

            System.out.println("4. Quit");



But now when I run the program the System.out.print("Choose one of the following \n" +
"by typing 1, 2, 3, or 4:");
repeats a billion times :(
Was This Post Helpful? 0
  • +
  • -

#10 woodlandsrider  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 20-April 12

Re: I cannot get my program to loop properly.

Posted 20 April 2012 - 11:55 AM

View PostGregBrannon, on 20 April 2012 - 11:04 AM, said:

And you're doing all of this in the main() method, without other methods or classes? Do you have to? That's so painful, but I accept (barely) that you have to learn to crawl before walking. A preferred alternative approach would be to use other classes to capture the characteristics of like objects and methods to do specific things: get the main menu choice, write to file, get products/prices, etc.

And you're not storing any of the customer or product data in memory for program use? Just in a file? Wow. Can you use other classes or arrays? I can't get into the workings of your program right now, but what you've outlined seems much harder than it should be.


Sadly enough :( our teacher is quite horrible in my opinion. Its the way she wants it to be written. She said that we have to store the customer info into an external file that will need to be extracted during the portion of the program for finding the total bill. This is the part of coding that I have for the Display total bill option----
				}
            else if (menuOption == 3)
              {
				//This is where I need the program to pull the customers info from the external file
			      [  System.out.print("\n" + "Enter customer's name:");
				String name = keyboard.nextLine();  ]
				
								
				//Consume the remaining newline.
		 		keyboard.nextLine();
				
								
				System.out.print("Enter product: ");
				String product = keyboard.nextLine();
				
				System.out.print("Enter quantity of product: ");
				int quantity = keyboard.nextInt();
				
								
				System.out.print("Enter price of product: ");
				double price = keyboard.nextDouble();
				
				
				
				double totalBill = quantity * price;					
				double totalTax = totalBill * 0.08;							
				double totalCost = totalBill + totalTax;
				double taxRate = 0.08;	

				
				
				System.out.println( "\n" +				
									" \n" + 
		        //This is where	the customer info should go--------- [  name ] + "\n" + 		
									
									"Product Purchased" + "     " + 
									"Quantity" + "      " +  
									"Total Cost" + 
									"\n" +

									product +  "                   " +  
									quantity +  "              " + 		
									" $" + String.format("%.2f", totalBill) +  					
									" \n" +
																          
									"Tax (@" + taxRate + "%):" +  "                            $" + String.format("%.2f", totalTax) +  "\n" +
																   									
									"Total Cost: " +  "                            $" + String.format("%.2f", totalCost) + " \n" +

									"");						
						


Was This Post Helpful? 0
  • +
  • -

#11 brep  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 22
  • View blog
  • Posts: 166
  • Joined: 26-August 11

Re: I cannot get my program to loop properly.

Posted 20 April 2012 - 11:58 AM

I think the loop is causing the statement to display a bazillions times because you forgot to enclose your code with {...}

while(menu) { //see the braces? If you don't have them, then the statement occurring immediately after the while declaration will execute for forever.

//logic here
}



Hope this helps,
preb

This post has been edited by preb: 20 April 2012 - 11:59 AM

Was This Post Helpful? 1
  • +
  • -

#12 woodlandsrider  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 20-April 12

Re: I cannot get my program to loop properly.

Posted 20 April 2012 - 12:24 PM

View Postpreb, on 20 April 2012 - 11:58 AM, said:

I think the loop is causing the statement to display a bazillions times because you forgot to enclose your code with {...}

while(menu) { //see the braces? If you don't have them, then the statement occurring immediately after the while declaration will execute for forever.

//logic here
}



Hope this helps,
preb


So something like this?
while(menu){
		  
		  
		  
        // Get the menu option from the user.
          System.out.print("Choose one of the following \n" +
                                  "by typing 1, 2, 3, or 4:");
			
          System.out.println("\n");
            System.out.println("1. Enter Customer Information");
            System.out.println("2. Price Lookup");
            System.out.println("3. Display total bill ");
				System.out.println("4. quit");
          System.out.println(); 
			
			menuOption = keyboard.nextInt();

       	}	
          


Was This Post Helpful? 0
  • +
  • -

#13 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • Joined: 10-September 10

Re: I cannot get my program to loop properly.

Posted 20 April 2012 - 01:07 PM

Your loop control variable for the while loop you've posted is the boolean menu. If a loop's control variable does not change in the loop, then the loop will not end, and the loop will be what is known as an "infinite loop." I don't see where the loop control variable changes in the loop, so you will have an infinite loop.

Another common tool for implementing text-based menus is the switch statement. Can you use that?

This post has been edited by GregBrannon: 20 April 2012 - 01:10 PM

Was This Post Helpful? 0
  • +
  • -

#14 woodlandsrider  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 20-April 12

Re: I cannot get my program to loop properly.

Posted 20 April 2012 - 04:11 PM

Alright, so I finally got my loops to work the way they should! :) :)

Now I need to figure out the menu option number 3---Display total bill.

I have been playing with the different ways to do this now for the past couple of hours and I still cannot figure out how to have the program pull the customers info from an external file and put it into the receipt along with having the program be able to pull the values of the products and put use them to calculate the bill. Here's what Ive got--
}
			else if(menuOption == 3)
			{
			
			menu = true;
			
			//Variables
			int product;
			int prod = 6;
			String name;
			
								
				System.out.print("\n" + "Enter customer's name:");
				name = keyboard.nextLine();
				
								
				//Consume the remaining newline.
		 		keyboard.nextLine();
				
				
								
				System.out.print("Enter product: \n\n");
				System.out.print("1. Shoes \n" +
									  "2. T-Shirts \n" +
									  "3. Shorts \n" +
									  "4. Caps \n" +
									  "5. Jackets \n");
									  
				prod = keyboard.nextInt();
				
				if (prod < 2)
				{
					product = 30;
				}
				else if (prod < 3)
				{
					product = 10;
				}
				else if (prod < 4)
				{
					product = 30;
				}
				else if (prod < 5)
				{
					product = 20;
				}
				else if (prod < 6)
				{
					product = 50;
				}
 				else
				{
				System.out.println("You did not choose a valid product \n\n");	
				}	
				
				
				System.out.print("Enter quantity of product: ");
				int quantity = keyboard.nextInt();
				
								
								
				
				
				double totalBill = quantity * prod;					
				double totalTax = totalBill * 0.08;							
				double totalCost = totalBill + totalTax;
				double taxRate = 0.08;	

				
				
				System.out.println( "\n" +				
									" \n" + 
									name + "\n" + 		
									
									"Product Purchased" + "     " + 
									"Quantity" + "      " +  
									"Total Cost" + 
									"\n" +

									prod +  "                   " +  
									quantity +  "              " + 		
									" $" + String.format("%.2f", totalBill) +  					
									" \n" +
																          
									"Tax (@" + taxRate + "%):" +  "                            $" + String.format("%.2f", totalTax) +  "\n" +
																   									
									"Total Cost: " +  "                            $" + String.format("%.2f", totalCost) + " \n" +

									"");						


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1