Solved

Enter and store multiple String Value

Posted on 2013-01-12
14
342 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 74

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 74

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
 

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 74

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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 500 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 74

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 74

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Receive file in Servlet 1 36
nestparen challenge 4 56
mapAB Challlenge 35 85
Java Jpanels and Jframe 8 20
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This video teaches viewers about errors in exception handling.

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now