Results 1 to 13 of 13
Like Tree1Likes
  • 1 Post By JosAH

Thread: switch case help! :(

  1. #1
    domtay is offline Member
    Join Date
    Dec 2013
    Posts
    6
    Rep Power
    0

    Question switch case help! :(

    Hi guys. this is the problem i have.

    i prompt this vaule to this variable called (carrots) in case 1.

    but i dont know how to keep this value of this variable (carrots) for other purpose to case 2. and so on so for case 3 and 4.

    is there anyway to solve? or all along is i did wrongly?

  2. #2
    domtay is offline Member
    Join Date
    Dec 2013
    Posts
    6
    Rep Power
    0

    Default Re: switch case help! :(

    this is the question: " if user choice is 2, user will select any order that was created in choice 1 to edit."

  3. #3
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,846
    Rep Power
    5

    Default Re: switch case help! :(

    you have to post your code. please use the forum's code tags so it stays readable; in the advanced post editor there is a button for it.

    Probably you have some misunderstanding about what you need to do, from your small description I get the idea that you are treating a switch statement as if it is a menu of choices.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    domtay is offline Member
    Join Date
    Dec 2013
    Posts
    6
    Rep Power
    0

    Default switch case help! :(

    HI guys my first time here.
    this is the question: " if user choice is 2, user will select any order that was created in choice 1 to edit."
    i prompt all the vaule to customerName, carrots, onions, tomatos and potatos in case 1.
    but i dont know how to keep the prompt assign vaule to case 2. ( in the code case 2 is error)
    or all along i did wrongly? im sorry for any inconvience.


    Java Code:
    import java.text.DecimalFormat;
    import java.util.Scanner;
    
    
    public class testing
    {
    	static DecimalFormat fmt2Decimal=new DecimalFormat("0.00"); // 2 Decimal format
    	static DecimalFormat fmt4WholeNumber=new DecimalFormat("0000");// 4 whole number format
    	static Scanner input=new Scanner(System.in).useDelimiter("\r\n");
    	
    	public static void main(String[] args) throws Exception
    	{
    		// TODO Auto-generated method stub
    		
    		int choice, maxOrders, orderNum;
    		double carrots, onions, potatos, tomatos; //declare variable
    		String customerName;
    	
    		System.out.println("\t\tVegetable Sales"); //start of menu choices
    		System.out.println("\t\t---------------");
    		System.out.println("1) Enter Order info");
    		
    		do //start of do-while loop
    		{
    		
    		System.out.println("2) Edit Order info");
    		System.out.println("3) Calculate and display orders");
    		System.out.println("4) Show vegetable with highest sales, and exit");
    		System.out.print("Please select your choice <1-4> : ");
    		choice = input.nextInt();
    		System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"); // end of menu choices
    		
    		switch (choice)
    		{
    		
    		case 1: 
    		do
    		{
    		System.out.println("");
    		System.out.print("How many orders <max 10> : ");
    		maxOrders = input.nextInt();
    		}while (maxOrders>10);// end of maximum orders <=10
    		for (orderNum=1; orderNum<=maxOrders; orderNum++) // Automatically generates the order number
    		{
    			if  ((orderNum==maxOrders) || (maxOrders<=1) ) // start of orders
    			{
    				System.out.println("----------------------------");
    				System.out.println("Please enter vegetable order");
    				System.out.println("----------------------------");
    				System.out.println("Order <" + orderNum +">");
    				System.out.println("Order number: " + "P" + fmt4WholeNumber.format(orderNum));
    				System.out.print("Enter customer name: ");
    				customerName = input.next();
    				do
    				{
    				System.out.print("Enter number of carrots<@$0.40cts>: ");
    				carrots = input.nextDouble();
    					if(carrots >=1000)
    					{
    					System.out.println("Please enter less than 1000");
    					}
    				}while (carrots >=1000);
    				
    				do
    				{
    				System.out.print("Enter number of onions<@$0.45cts>: ");
    				onions = input.nextDouble();
    					if(onions >=1000)
    					{
    					System.out.println("Please enter less than 1000");
    					}
    				}while (onions >=1000);
    				
    				do
    				{
    				System.out.print("Enter number of potatos<@$0.35cts>: ");
    				potatos = input.nextDouble();
    					if(potatos >=1000)
    					{
    					System.out.println("Please enter less than 1000");
    					}
    				}while (potatos >=1000);
    				
    				do
    				{
    				System.out.print("Enter number of tomatos<@$0.50cts>: ");
    				tomatos = input.nextDouble();
    					if(tomatos >=1000)
    					{
    					System.out.println("Please enter less than 1000");
    					}
    				} while (tomatos >=1000); //end of showVegetables method
    				}
    		}	
    		break;
    		
    		case 2:
    		System.out.println("Select order to edit");
    		System.out.println("Customer Name" + customerName);
    		System.out.println("Number of carrots: " + carrots);
    		System.out.println("number of onions: " + onions);
    
    		break;
    		
    		case 3:
    		break;
    		
    		case 4:
    		break;
    		
    		default: System.out.println("Error. Try again");
    		}
    
    		}while (choice != 4); //end of do-while loop
    	}//end of main
    }//end of class

  5. #5
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default Re: switch case help! :(

    Why did you start a new thread rather continuing with the original?

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default Re: switch case help! :(

    dont know how to keep the prompt assign vaule to case 2
    Can you copy the full contents of the window from when you execute the program that shows the problem and paste it here?
    Add some comments describing what is wrong with what the program prints.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    domtay is offline Member
    Join Date
    Dec 2013
    Posts
    6
    Rep Power
    0

    Default Re: switch case help! :(

    Hi norm. i have paste the whole content here.
    but i dont know how to carry on from case 2 onwards. i need to edit the variable which i prompt into case 1, from case 2. i not sure what to do or perhaps i started wrongly in the first place.
    so yah. i just want to ask is there any possible that i prompt case 1 variable which can be use in case 2?

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default Re: switch case help! :(

    case 1 variable which can be use in case 2?
    What variable are you having problems with?
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    domtay is offline Member
    Join Date
    Dec 2013
    Posts
    6
    Rep Power
    0

    Default Re: switch case help! :(

    customerName, carrots, onions, tomatos and potatos

  10. #10
    domtay is offline Member
    Join Date
    Dec 2013
    Posts
    6
    Rep Power
    0

    Default Re: switch case help! :(

    norm. thanks for your help. i found out already :)

  11. #11
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,846
    Rep Power
    5

    Default Re: switch case help! :(

    Care to share your solution?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  12. #12
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,432
    Blog Entries
    7
    Rep Power
    20

    Default Re: switch case help! :(

    Quote Originally Posted by domtay View Post
    customerName, carrots, onions, tomatos and potatos
    What's for dinner? Fried customers?

    kind regards,

    Jos (<--- hungry)
    gimbal2 likes this.
    cenosillicaphobia: the fear for an empty beer glass

  13. #13
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,846
    Rep Power
    5

    Default Re: switch case help! :(

    Quote Originally Posted by JosAH View Post
    Jos (<--- hungry cannibal)
    ftfy
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. [SOLVED] switch case with '<='
    By Adomini in forum New To Java
    Replies: 9
    Last Post: 02-17-2013, 04:01 PM
  2. java switch case
    By aconti in forum New To Java
    Replies: 16
    Last Post: 08-09-2011, 07:05 AM
  3. Switch Case statement
    By seanfmglobal in forum New To Java
    Replies: 7
    Last Post: 02-15-2011, 01:18 PM
  4. if else changes to switch-case?
    By noobinoo in forum New To Java
    Replies: 1
    Last Post: 04-23-2010, 05:56 PM
  5. Replies: 2
    Last Post: 02-23-2009, 12:34 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
  •