Link to home
Start Free TrialLog in
Avatar of Kevin_Coors
Kevin_Coors

asked on

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

Avatar of kaufmed
kaufmed
Flag of United States of America image

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.
Avatar of Kevin_Coors
Kevin_Coors

ASKER

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

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?
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?
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.
Thanks

Sorry but I cannot figure this out a present. I will come back to it. spent too much time on it already
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
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks Kaufmed. I really appreciate all your help thus far.
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.
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  = )
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);*/
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.
thanks a lot kaufmed