• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 356
  • Last Modified:

Enter and store multiple String Value

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
Kevin_Coors
Asked:
Kevin_Coors
  • 8
  • 6
1 Solution
 
käµfm³d 👽Commented:
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
 
Kevin_CoorsAuthor Commented:
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
 
käµfm³d 👽Commented:
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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Kevin_CoorsAuthor Commented:
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
 
käµfm³d 👽Commented:
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
 
Kevin_CoorsAuthor Commented:
Thanks

Sorry but I cannot figure this out a present. I will come back to it. spent too much time on it already
0
 
Kevin_CoorsAuthor Commented:
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
 
käµfm³d 👽Commented:
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
 
Kevin_CoorsAuthor Commented:
Thanks Kaufmed. I really appreciate all your help thus far.
0
 
Kevin_CoorsAuthor Commented:
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
 
käµfm³d 👽Commented:
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
 
Kevin_CoorsAuthor Commented:
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
 
käµfm³d 👽Commented:
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
 
Kevin_CoorsAuthor Commented:
thanks a lot kaufmed
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 8
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now