Page 1 of 2 12 LastLast
Results 1 to 20 of 28
  1. #1
    baumboards is offline Member
    Join Date
    Jan 2011
    Posts
    24
    Rep Power
    0

    Default using for-loop to print out a string

    I am to have four methods, each method will print out the same paragraph of information:
    "This program supports 4 functions:
    1. Setup Shop
    2. Buy
    3. List Items
    4. Checkout
    Please choose the function you want:"

    Except once I create the second method, the new paragraph doesn't print out, my code will only output my first method and then stop. I have an inkling that using a for-loop is the way to solve this, but I don't completely know how.(btw this code is to allow a user to go through the 4 steps to choose an item, decide how many they want, list the items and then see the total)
    Java Code:
    import java.util.Scanner;
    
    
    public class shop {
    	 public static void main(String[] args) {
    		 Scanner input = new Scanner(System.in);
    		 
    			int choice;
    		    for(int i=0;i>4;i++){
    		    System.out.println("This program supports 4 functions: ");
    		    System.out.println("1. Setup Shop");
    			System.out.println("2. Buy");
    			System.out.println("3. List Items");
    			System.out.println("4. Checkout");
    			System.out.println("Please choose the function you want:");
    			 
    			choice = input.nextInt();     
    	 }}

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

    Default

    This method doesn't really do anything but get a number and then re-ask the question four times. What is the rest of your code?

    I am assuming you mean to ask something, get some input and then do the correct thing based on the choice.

  3. #3
    baumboards is offline Member
    Join Date
    Jan 2011
    Posts
    24
    Rep Power
    0

    Default

    rest of the code is:
    Java Code:
    public static void Setup(String[] args,double discount,int choice,int items,int amount, String [] name, double[] price) {
    	 Scanner input = new Scanner(System.in);
    	
    	
    	if(choice==2)
    		System.out.println("Shop is not setup yet!");
    	if(choice==3)
    		System.out.println("Shop is not setup yet!");
    	if(choice==4)
    		System.out.println("Shop is not setup yet!");
    	if(choice>=5)
    		System.out.println("Error: Do not know "+choice);
    	 
    	if(choice==1)
    		System.out.println("Please enter the number of items: ");
    	items=input.nextInt();
    	for(int i=0;i<items;i++){
    		System.out.println("Enter name of product " + i+" :");
    	    name[i] = input.next();
    	    System.out.println("Enter price of product "+i+" :");
    	    price[i] = input.nextDouble();
    	}
    	System.out.println("Please enter the amount to qualify for discount: ");
    	amount = input.nextInt();
    	System.out.println("Please enter the discount rate(0.1 for 10%): ");
    	discount = input.nextDouble();
    	
    	 } 
    	
    	
    
    
    public static void BuyItems (String[] args){
    	Scanner input = new Scanner(System.in);
        int choice;
    
        System.out.println("This program supports 4 functions: ");
    	System.out.println("1. Setup Shop");
    	System.out.println("2. Buy");
    	System.out.println("3. List Items");
    	System.out.println("4. Checkout");
    	System.out.println("Please choose the function you want:");
    	 
    	choice = input.nextInt();
    	
    	if(choice==3)
    		System.out.println("Try again: You have not bought anything");
    	if(choice==4)
    		System.out.println("Try again: You have not bought anything");
    	if(choice<=5)
    		System.out.println("Error: Do not know "+choice);
    
    	}
    
    
    
    
    
    
    
    
    
    
    
    
    
    }

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

    Default

    Just to get a better understanding. Your goal is to ask them what they want to do in main and then depending on what they respond with call some other method?

  5. #5
    baumboards is offline Member
    Join Date
    Jan 2011
    Posts
    24
    Rep Power
    0

    Default

    kind of,this is what i have to do:
    Setup Shop (I)
    1. Ask for the number of items to sell
    2. For each item
    a. Ask the name of the item (one word)
    b. Ask the price of the item
    3. Discount
    a. Ask for the threshold (over which amount to give discount)
    b. Ask for the rate (how much % discount)
    4. User can run setup multiple times so keep the latest version

    Buy Items (II)
    1. If setup is not done yet then tell customer to setup shop first 2. For each item
    a. Ask the amount they wish to purchase
    3. User can purchase multiple times so only process the latest
    order (think of init() from labs)

    List of Items (III)
    1. If setup is not done yet then tell customer to setup shop first
    2. If setup is done but the customer hasnít bought anything then
    ask to buy first
    3. For each item purchased (non-zero amount)
    a. Display amount purchase and price per item

    Check Out (IV)
    1. If setup is not done yet then tell customer to setup shop first
    2. If setup is done but the customer hasnít bought anything then
    ask to buy first
    3. Display the summary
    a. Sub Total
    b. Discount
    c. Total
    4. End the program

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Java Code:
    for(int i=0;i>4;i++){
    Your loop does nothing as the condition is false. Even if you fixed it, it will only allow user to do 4 things and then exit. Perhaps you need a while loop instead.

    Also, why do you print the menu again in the BuyItems method?

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

    Default

    Just a thought, this problem would be a pretty ideal situation for the use of objects. Why not create a class for the shop and a class for items, the shop can maintain the items for you, making some tasks very easy.

    I know this isn't really your question, however it's a thought. Also definitely consider a while loop. It also seems like you should be getting some input and then using it to call methods..or create objects and other fancy things.

  8. #8
    baumboards is offline Member
    Join Date
    Jan 2011
    Posts
    24
    Rep Power
    0

    Default

    so something like:
    Java Code:
    while (true) {
    System.out.print("Please choose the function you want:");
    int func = input.nextInt();
    i have the menu repeated twice because at the beginning of each new method I am supposed to print out the menu again, yet it doesn't work.

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Quote Originally Posted by baumboards View Post
    i have the menu repeated twice because at the beginning of each new method I am supposed to print out the menu again, yet it doesn't work.
    I think you are misinterpreting the instructions as this makes no sense. You print the menu and ask user what they want to do. User selects "2. Buy". Program enters the BuyItems method and immediately prints the menu again. HUH?????

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

    Default

    a while true loop should work in printing it continually. Is this assignment intended to use objects or no? How exactly is the shops info stored when you choose setup shop?

    the code should be setup like this
    Java Code:
    show menu and prompt for user input
    store input
    do something with input(use if/else or switch statements)

  11. #11
    baumboards is offline Member
    Join Date
    Jan 2011
    Posts
    24
    Rep Power
    0

    Default

    yupp that's actually exactly what the program is to do, the whole reason I am supposed to reprint the menu 4 times is because this project tests whether students (like me) know how to use 4 methods in total, after each method i reprint the menu, the user is supposed to choose the next step, if the user enters an invalid choice, then the program is to print " Shop is not setup yet!", so you see the user has to choose the next step after each method, it seems stupid but its so that the student (me) is forced to fool-proof the code so that if the customer does not follow the ascribed order then there should be guidance on what to do first.

    My question is how do i get the menu to keep reprinting after each method?

  12. #12
    baumboards is offline Member
    Join Date
    Jan 2011
    Posts
    24
    Rep Power
    0

    Default

    Is this assignment intended to use objects or no?


    No i am only supposed to use methods, sorry for not clarifying this earlier.

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

    Default

    a for loop should work if you want it to only print 4 times
    Java Code:
    for(int i = 0; i < 4; i++){
      print menu
      get input
      store input
      do something with input
    }
    Say you call another method in the "do something with input" line, after that method is finished the loop will reset and re print the menu.

  14. #14
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    So you are misinterpreting the instructions. If you fix the for loop in the main method then it shouold work.

    In main method
    loop 1
    print menu
    user selects 1
    go to Setup shop method (DO NOT PRINT MENU)
    back to main method
    loop 2
    print menu
    user selects 2
    go to the Buy method (DO NOT PRINT MENU)
    back to main method
    loop 3
    etc

  15. #15
    baumboards is offline Member
    Join Date
    Jan 2011
    Posts
    24
    Rep Power
    0

    Default

    alright your completely right, thanks for pointing me in the right direction, now to figure out how it will go back to the main method...

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

    Default

    If you call a method in main the main method get set aside until the method being called returns. At this point it returns to the correct spot in main.

    To be more detailed, any calls to new methods in main get put on the stack and begins running to completion. If this method calls any methods they are also put on the stack, when the method completes it is popped off the stack. The bottom of the stack is the main method, and eventually it automatically returns to the correct spot(after the method call)
    Last edited by sunde887; 03-10-2011 at 06:34 AM.

  17. #17
    baumboards is offline Member
    Join Date
    Jan 2011
    Posts
    24
    Rep Power
    0

    Default

    I think im getting on the right track now, here is my new code:
    Java Code:
    import java.util.Scanner;
    
    
    public class shop {
    	 public static void Setup(String[] names,Scanner input, double[] prices){
    		 
    		 int choice=0,items,amount;
    	     double discount;
    	
    	    
    	
    	
    	
    	if(choice==2)
    		System.out.println("Shop is not setup yet!");
    	if(choice==3)
    		System.out.println("Shop is not setup yet!");
    	if(choice==4)
    		System.out.println("Shop is not setup yet!");
    	if(choice>=5)
    		System.out.println("Error: Do not know "+choice);
    	 
    	
    	
    	
    	 
    	if(choice==1)
    		System.out.println("Please enter the number of items: ");
    	items=input.nextInt();
    	for(int j=0;j<items;j++){
    		System.out.println("Enter name of product " + j+" :");
    	    names[j] = input.next();
    	    System.out.println("Enter price of product "+j+" :");
    	    prices[j] = input.nextDouble();
    	}
    	System.out.println("Please enter the amount to qualify for discount: ");
    	amount = input.nextInt();
    	System.out.println("Please enter the discount rate(0.1 for 10%): ");
    	discount = input.nextDouble();
    	
    	 } 
    	
    	
    
    
    
    	
        
    
    	
    	
    
    	
    
    
    
    
    
    
    	 
    
    public static void main(String[] args) {
    	Scanner input = new Scanner(System.in);
    	int max = 10;
    	String[] names = new String[max];
    	double[] prices = new double[max];
    	 int choice;
    	 for(int i=0;i<4;i++){
        	 System.out.println("This program supports 4 functions: ");
        	 System.out.println("1. Setup Shop");
        	 System.out.println("2. Buy");
        	 System.out.println("3. List Items");
        	 System.out.println("4. Checkout");
        	 System.out.println("Please choose the function you want:");
    	     
        	 choice=input.nextInt();
    
       
          
          Setup(names,input ,prices);
         
    }}}

  18. #18
    baumboards is offline Member
    Join Date
    Jan 2011
    Posts
    24
    Rep Power
    0

    Red face

    Alright so I have finally been able to figure out how to get the menu to repeat after each method, now my only issue is figuring out how to use if-statements. The program needs to self-check, so if the user inputs 2 when the user should input 1, the program should prompt the user to reenter the correct input. This is what I have so far in simplified form:
    Java Code:
    while(true)
    menu
    if(choice==1)
    goes to method 1, then user enters value 2
       else if(choice==2)
         goes to method 2, then user enters value 3
       else if(choice==3)
         goes to method 3, then user enters value 4
       else if(choice==4)
         goes to method 4
    //this next part is what doesn't work
    if(choices==2)
       prints out: reenter value 
    if(choices==3)
       prints out: reenter value
    if (choices==4)
       prints out: reenter value
    There must be a more efficient way to coding for every possible input between 1 and 4, here is the actual code:
    Java Code:
    import java.util.Scanner;
    
    
    public class shop2 {
    	public static void main(String[] args) {
    		Scanner input = new Scanner(System.in);
    		 int max=2;
    		
    		double discounttotal=0;
             double subtotal=0;
    	     
    		 String[] names = new String[max];
    		  double[] prices = new double[max];
    		  int[] amount= new int[max];
    		int[] discount=new int[1];
    		 double[] discountrate=new double[1];
    		while(true){
    		System.out.println("This program supports 4 functions:");
    				System.out.println("1. Setup Shop ");
    				System.out.println("2. Buy ");
    				System.out.println("3. List Items"); 
    				System.out.println("4. Checkout");
    	            System.out.println("Please choose the function you want: ");
    	           int choice=input.nextInt();
    	           
    	          if(choice==1) 
    	        	  Setup( choice,max,discountrate,discount,names,prices);
    	          else if(choice==2)
    	              Buy( choice,names,max,amount);
    	          else if(choice==3)
    	        	   ListItems(names,amount,max,prices);
    	          else if(choice==4)
    	        	      checkout(discount,discountrate,prices,amount,max,discounttotal,subtotal);
    	         
    	         
    	        
    		}}
    
      public static void Setup(int choice,int max,double[] discountrate,int[]discount,String[] names, double[] prices){
    	  Scanner input = new Scanner(System.in);
    	  
      if(choice>=5)
    	  System.out.println("Error: Do not know "+choice );
    	  if(choice==4)
    	  System.out.println("Shop is not setup yet! ");
      if(choice==3)
    	 System.out.println("Shop is not setup yet! ");
      if(choice==2)
    	  System.out.println("Shop is not setup yet! ");
      if(choice==1)
    	   System.out.println("Please enter the number of items: ");
           max =input.nextInt();
      for(int i=0;i<max;i++){
    	  System.out.println("Enter name of product "+ i+":");
    	  names[i]=input.next();
    	  System.out.println("Enter price of product "+i+":");  
    	  prices[i]=input.nextDouble();
     }for(int i=0;i<1;i++){
      System.out.println("Please enter the amount to qualify for discount: ");
       discount[i]=input.nextInt();
       System.out.println("Please enter the discount rate(0.1 for 10%): ");
       discountrate[i]=input.nextDouble();
      }}
    
             public static void Buy(int choice,String[] names,int max,int []amount){
    	Scanner input = new Scanner(System.in);
        if(choice>=5)
    	System.out.println("Error: Do not know "+choice );
    	if(choice==4)
    	System.out.println("Try again: You have not bought anything ");
    	if(choice==3)
    	System.out.println("Try again: You have not bought anything ");
    	
        if(choice==2){
    		
        for(int i=0;i<max;i++){
    	System.out.println("Enter the amount of " +names[i]+ ":");
        amount[i]=input.nextInt();
        
        }}}
      
             public static void ListItems(String[] names,int [] amount,int max,double[] prices){
          for(int i =0;i<max;i++){
            	 System.out.println(amount[i]+" count of " + names[i]+" @ "+prices[i]+" = "+ " $"+(amount[i]*prices[i]));
             }}
      
    public static void  checkout(int[] discount,double[] discountrate,double[]prices,int[]amount,int max,double discounttotal,double subtotal){
            	
                 
                 for(int i=0;i<max;i++){
               	  subtotal=subtotal+ amount[i]*prices[i];
                 }
      
                 for(int i=0;i<1;i++){
             if(subtotal>discount[i]){
            	 discounttotal=subtotal-(discount[i]*discountrate[i]);
      System.out.println("Thanks for coming! ");
      System.out.println("Sub Total: "+"$"+subtotal);
      System.out.println("-Discount: "+ "$"+subtotal*discountrate[i]);
      System.out.println("Total    : "+"$"+(subtotal-(subtotal*discountrate[i])));
             }
     
                 if (subtotal<discount[i]){
    	  System.out.println("Thanks for coming! ");
             System.out.println("Sub Total: "+"$"+subtotal);
             System.out.println("-Discount: "+ "$0");
             System.out.println("Total    : "+"$"+subtotal);
      
             }}}}
    :confused:

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

    Default

    I haven't really looked to much into the most recent code but I have a few immediate tips. If you made the braces the way they are you really need to fix that. You should have them help you visualize where code block begin and end. If the format was messed up when pasting you can disregard this.

    A bit of an efficiency tip(as far as typing is concerned) check out switch cases. As far as I know if else clauses and switch cases are about the same for the computer(I could be wrong)

    If you are looking for more efficient way to write the entire program it would require designing some classes. To validate you has two choices. You can store the prev choice and use that to determine whether they have done the previous step. (can only choose 2 if 1 was chosen previously, etc. ) The other method relies on checking on previous state. Something like, if choice is 2 check if shop is set up, if it is execute 2, else issue error.
    Last edited by sunde887; 03-11-2011 at 10:57 AM.

  20. #20
    baumboards is offline Member
    Join Date
    Jan 2011
    Posts
    24
    Rep Power
    0

    Default

    what would I have to fix in order to still use if-else statements, I'm not allowed to use more than one class.

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 3
    Last Post: 02-03-2011, 11:19 PM
  2. I can only print string literals
    By leeavital in forum New To Java
    Replies: 1
    Last Post: 12-11-2009, 02:20 AM
  3. println doesn't print from inside for loop, et.al.
    By rdtindsm in forum New To Java
    Replies: 5
    Last Post: 03-27-2009, 02:19 PM
  4. Loop System print or println statements
    By Singing Boyo in forum New To Java
    Replies: 6
    Last Post: 03-22-2009, 04:52 AM
  5. how to print output on same line in 'while loop'?
    By acidblue in forum New To Java
    Replies: 5
    Last Post: 12-13-2007, 03:30 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
  •