Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Enter and store multiple String Value

Posted on 2013-01-12
14
Medium Priority
?
351 Views
Last Modified: 2013-01-13
Hello

I am a Java beginner and I working on a problem as follows. (Note: I haven't begun to learn  Arrays yet)

I need the user to enter n values for their main course order.

i.e if there a 4 in the dinner party I need to be able to enter and save4 different main course values

Would a Do While Loop achieve this? I am been trying this out for a while now. I will post the awful code I have created so far and keep trying and hope someone sees this post

//Each main course costs €22.00,
import java.util.Scanner;

public class DinnerParty
{
	public static void main (String [] args)
	{
	//print out the menu
	System.out.println ("1. Turkey and Ham \n2. Steak (€5 supplement)\n3. Nut roast (vegetarian €3 discount)\n4. Lasagne");
	
	System.out.println();//new line
	
	int partyNum;
	int mainCourse;
	String courseName1 = "Turkey and Ham"; 
	String courseName2 = "Steak"; 
	String courseName3 = "Nut Roast"; 
	String courseName4 = "Lasagne"; 
	
	Scanner kb = new Scanner(System.in);
	//get the user to enter the number of people in their party (Scanner class)
	System.out.println ("Please enter the number of people in your party");
	partyNum = kb.nextInt();
	
	//display number in party
	System.out.println("The number in your party is " + partyNum);
	
	System.out.println();//new line
	
	do{
		// read in each members main course choice 
		System.out.println ("Please enter each of your party's main course choice");
		mainCourse = kb.nextInt();

		System.out.println("You have chosen maincourse " + mainCourse);
	} while (mainCourse < partyNum);
	
	
	
	//Count and display the number of each main course chosen
	
	// display the total cost of the booking
	}
}

Open in new window

0
Comment
Question by:Kevin_Coors
  • 8
  • 6
14 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38770390
The code isn't that bad. Your loop is structured well, but think about this:  What does the condition:

while (mainCourse < partyNum);

actually mean? What does mainCourse represent? Evaluate that, and I think you'll understand why the condition doesn't quite make sense.
0
 

Author Comment

by:Kevin_Coors
ID: 38770408
Hi Kaufmed

Thanks for you help.

mainCourse did not make sense because of the 4 menu options

I changed the variable to int choice;

I can now run the program and enter each users mainCourse choice. How do I store all these values to memory using the variable choice, without using an array? I am a 1st year student and we have not covered arrays yet. New code below

import java.util.Scanner;

public class DinnerParty
{
	public static void main (String [] args)
	{
	//print out the menu
	System.out.println ("1. Turkey and Ham \n2. Steak (€5 supplement)\n3. Nut roast (vegetarian €3 discount)\n4. Lasagne");
	
	System.out.println();//new line
	
	int partyNum;
	int mainCourse;
	int choice;
		
	Scanner kb = new Scanner(System.in);
	//get the user to enter the number of people in their party (Scanner class)
	System.out.println ("Please enter the number of people in your party");
	partyNum = kb.nextInt();
	
	//display number in party
	System.out.println("The number in your party is " + partyNum);
	
	System.out.println();//new line
	
	do{
		// read in each members main course choice 
		System.out.println ("Please enter each of your party's main course choice");
		choice = kb.nextInt();

		System.out.println("You have chosen maincourse " + choice);
	} while (choice < partyNum);
	
	 //Count and display the number of each main course chosen
	
	// display the total cost of the booking
	}
} 

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38770425
Well, again you have something that doesn't quite make sense. Think about the condition in words:

While the user's choice is less than the number of people in the party, do something.

Why would you be comparing the choice of entree to the number of people at the table?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Kevin_Coors
ID: 38770433
I was thinking along the lines of

There are 4 people for dinner  stored in variable partyNum

If the choices are less than the partyNum then the loop will exit after it reached the number in the party?
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38770442
If the choices are less than the partyNum then the loop will exit after it reached the number in the party?
But you need to compare the number of choices, not the actual choice itself. You need another variable to track how many people have given you their choice.
0
 

Author Comment

by:Kevin_Coors
ID: 38770510
Thanks

Sorry but I cannot figure this out a present. I will come back to it. spent too much time on it already
0
 

Author Comment

by:Kevin_Coors
ID: 38770633
Hi Kaufmed

I have 6 of these do while loop questions to answer and If I get one the rest should be fine.

Would it be possible to give me a kick start. Literally

I have been down with a bad flu for 10 the last 8 days and I'm cannot get my brain to work.

I know I can do this . I seem to be struggling with storing the values of multiple people
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 total points
ID: 38770668
I assure you I am trying to help you, but I have to do so by the guidelines set forth by this site. Both you and I are bound by the terms of service for EE, which permits you to ask questions related to academic assignments, but does not permit me to do the work for you. (I'm sure that's not what you're asking me to do, but I have to be careful in how I structure my answers for you.)

Think about the question literally. What if you were a waiter taking this order in a real restaurant, with real people (the party). These people have the menu in front of the (the entree choices). As you are taking their orders, you (the waiter) have to keep track of who you asked. Now in real life you would handle all of this mentally, and subconsciously, but you have to keep track nonetheless.

In your program, there is no subconscious (that has been proven, *wink*). You have to be very literal in how you track counts, compare items, evaluate mathematical expressions, etc. What I am saying to you is that you need to add another variable that tracks the number of people who have given you their order. Yes, you will have a loop that repeats until everyone has ordered, but you need someway of indicating that the number of people that have ordered has changed. If you create this new variable (initialized to zero, since no one has ordered before the loop begins), then you can increment it each time the loop runs. So the first time the loop runs, one person has ordered, and you update your variable to reflect this. The second time the loop runs, two people have ordered, etc., etc. Your condition for the loop needs to be that you continue to loop until the number of people that have ordered equals the number of people in the party (which you already know from the first input received by the program--i.e before the loop). Once these two values are equal (i.e. everyone in the party has ordered), you exit the loop.
0
 

Author Comment

by:Kevin_Coors
ID: 38770673
Thanks Kaufmed. I really appreciate all your help thus far.
0
 

Author Comment

by:Kevin_Coors
ID: 38770754
Still lost but I'll give you the points.

Thanks a milllion for your assistance

EE have bummed me out for the last time.

Gonna try elsewhere.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38770759
I am happy to keep going with the explanation(s) should you decide to go further with this. You just need to ask specific questions about what you don't understand  = )
0
 

Author Comment

by:Kevin_Coors
ID: 38770800
Ah Thanks. I think I have it actually. Sorry about throwing my soother out of the pram but It's just very frustrating. I prob need a break

Is this any closer?


import java.util.Scanner;

public class DinnerParty
{
	public static void main (String [] args)
	{
		//print out the menu
		System.out.println ("1. Turkey and Ham \n2. Steak (€5 supplement)\n3. Nut roast (vegetarian €3 discount)\n4. Lasagne");
		
		System.out.println();//new line
		
		int partyNum;
		int choice;
		int order;
		int count = 0;
		int total = 0;
		double mainCourse;
		int i = 0;
			
		Scanner kb = new Scanner(System.in);
		//get the user to enter the number of people in their party (Scanner class)
		System.out.println ("Please enter the number of people in your party");
		partyNum = kb.nextInt();
		
		//display number in party
		System.out.println("The number in your party is " + partyNum);
		
		// Read each member of the partys main course
				
		System.out.println();//new line
		
		System.out.println ("Please enter the order for each of your party");
		
		for(i = 1; i <= partyNum; i++)
		{
			choice = kb.nextInt();
			
			System.out.println("Person " + i + " will have maincourse no: " +choice);
			count++;
		}
		
		if (i == 1)
		{
			mainCourse = 22.00;
	   }
		else if (i == 2)
		{
			mainCourse = 27.00;
		}
		else if (i == 3)
		{
		   mainCourse = 19.00;
		}
		else
			mainCourse = 22.00;
		 
		 //Count and display the number of each main course chosen
		 System.out.println("The total orders are " + count);
		 
		// display the total cost of the booking
		
	;
		System.out.println("The total cost of all " + count + " orders is " + total);

	}
}

Open in new window


 /*do{
            // read in each members main course choice
            System.out.println ("Please enter each of your party's main course choice");
            choice = kb.nextInt();
      

            System.out.println("You have chosen maincourse " + choice);
      } while (choice < partyNum);*/
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38770823
for(i = 1; i <= partyNum; i++)
{
      choice = kb.nextInt();

      System.out.println("Person " + i + " will have maincourse no: " +choice);
      count++;
}

Excellent! I figured you wanted to stick with your do/while, but a for loop is a nice way to go as well.
0
 

Author Comment

by:Kevin_Coors
ID: 38771748
thanks a lot kaufmed
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

927 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question