简体   繁体   中英

how do i store a string value from user input in a list using loop for multiple entry c#

Write a program to do the following: Create a console application which asks user to enter suburb and postcode for two cities Auckland and Wellington. Iterate this 10 times (which means the user has to input suburb and postcode, one after the other 10 times). You are required to handle the exceptions using try and catch block. Store the values of suburb in a List called suburb_values and the values of the postcodes in a List called postcode_values. You are now required to ask the user if they want to INSERT(), REMOVE() or CONTAINS(). This must be a switch case. If the user selects 1: you are required to INSERT 5 pre-defined suburb and post code values into the respective lists and print the final values inside the list. If the user selects 2: you are required to REMOVE 4 values using Remove() and RemoveAt(). If the user selects 3: you are required to check if the suburb_values list contains atleast 5 suburbs that the user will enter. If the user selects any other value: you need to display invalid and exit.

THIS IS MY CODE TILL NOW

using System;
using System.Collections.Generic;       // TO HAVE LIST YOU NEED TO ADD THIS LINE using System.Collections.Generic; UNDER the line using System;

namespace Assessent1ListUsingGenerics
{       
class Program                               //List<T> in C# | In Class Activity

{                                           //a. WAP to insert 10 values of Country and capital names and
                                            //its pin code (two lists: suburb<string> and pincode<int>).
                                            //Print and display the list.
    static void Main(string[] args)
    {
        int counter = 0;
        var suburb_values = new List<string>();
        while (counter < 10) 
        {
            //creating list suburb_ values ( String Type )
            //Adding values to the list from user input                        //user input will be saved in country_values

            Console.WriteLine("PLease Enter a Suburb for Auckland City");
            suburb_values.Add(Console.ReadLine());     //suburb_values 1        //this will be the index 2 correct ?            
            suburb_values.Add(Console.ReadLine());     //suburb_values 2            
            suburb_values.Add(Console.ReadLine());     //suburb_values 3            country_values.Add(Console.ReadLine());     //country name 1            
            suburb_values.Add(Console.ReadLine());     //suburb_values 4            
            suburb_values.Add(Console.ReadLine());     //suburb_values 5            
            suburb_values.Add(Console.ReadLine());     //suburb_values 6    
            suburb_values.Add(Console.ReadLine());     //suburb_values 7            
            suburb_values.Add(Console.ReadLine());     //suburb_values 8            
            suburb_values.Add(Console.ReadLine());     //suburb_values 9            
            suburb_values.Add(Console.ReadLine());     //suburb_values 10
            counter;
        } 
       
        //creating list postcode_values ( String Type )
        var postcode_values = new List<string>();
        Console.WriteLine("Please enter a Post code for the Auckland Suburb you entered ");
        //Adding values to the list
        postcode_values.Add(Console.ReadLine());    //postcode 1
        postcode_values.Add(Console.ReadLine());    //postcode 2  
        postcode_values.Add(Console.ReadLine());    //postcode 3
        postcode_values.Add(Console.ReadLine());    //postcode 4
        postcode_values.Add(Console.ReadLine());    //postcode 5
        postcode_values.Add(Console.ReadLine());    //postcode 6
        postcode_values.Add(Console.ReadLine());    //postcode 7
        postcode_values.Add(Console.ReadLine());    //postcode 8
        postcode_values.Add(Console.ReadLine());    //postcode 9    
        postcode_values.Add(Console.ReadLine());    //postcode 10

        Console.WriteLine("PLEASE CHOOSE TO INSERT - REMOVE _ CONTAINS \n");
        Console.WriteLine("PLEASE SELECT ON OPTION: ");
        Console.WriteLine("\nPress 1 for Insert - Press 2 for Remove - Press 3 for Contains ");

        int userInput = Convert.ToInt32(Console.ReadLine());  //ASK YOUR INPUT WITH THIS CODE, what user type will be saved inside userInput

        switch (userInput)
        {
            case 1:  //you are required to INSERT 5 pre-defined suburb and post code values into the respective lists and print the final values inside the list. ");
                {

                    Console.WriteLine("\nOption 1 Selected - PLEASE INSERT 2 SUBURBS AND 2 POSTCODES FOR AUCKLAND CITY");
                    //Inserting values to the Suburb list
                    Console.WriteLine("Please Insert a Suburb ");
                    suburb_values.Insert(0, Console.ReadLine());
                    Console.WriteLine("Please Insert the next Suburb ");
                    suburb_values.Insert(1, Console.ReadLine());

                    //inserting values to the PostCode list
                    Console.WriteLine("Please Inser a PostCode");
                    postcode_values.Insert(0, Console.ReadLine());
                    Console.WriteLine("Please Inser a PostCode");
                    postcode_values.Insert(1, Console.ReadLine());

                    break;
                }

            case 2:
                {
                    Console.WriteLine("\nOption 2 selected - you are required to REMOVE 4 values using Remove() and RemoveAt().D");
                    suburb_values.Remove("");
                    suburb_values.RemoveAt(0);
                    break;
                }
            case 3:
                {
                    Console.WriteLine(" you  are  required  to  check  if  the  suburb_values  list  contains  atleast  5 suburbs that the user will enter. ");
                    // code to check missing
                    break;
                }
            default:
                Console.WriteLine("Invalid");
                break;
        }
        Console.ReadKey();
    }
}

At this stage i have the code almost done but how do I check if the suburb_values list contains atleast 5 suburbs that the user will enter. ???

here is my code updated still not sure if is 100% correct but I'm trying

using System; using System.Collections.Generic; // TO HAVE LIST YOU NEED TO ADD THIS LINE using System.Collections.Generic; UNDER the line using System;

namespace Assessent1ListUsingGenerics {
class Program //List in C# |

{                                           //a. insert 10 values of suburb_values  and postcode_values
                                            

    static void Main(string[] args)
    {   //------------------------------------ AUCKLAND---------------------------------------------------------------

        int counter = 0; // this is for the loop to execute asking user input 10x times for each suburb and postcode values to store in the variables 
        
        var suburb_values = new List<string>(); //creating list suburb_ values ( String Type )
        
        var postcode_values = new List<string>(); //creating list postcode_values ( String Type )
        try
        {
            while (counter < 10)  //counter is zero and won't pass the limit of 10 times if the user input some value
            {
                //Adding values to the list from user input                        //user input will be saved in suburb_values

                Console.WriteLine("Please enter a Suburb for Auckland City: ");
                suburb_values.Add(Console.ReadLine());     //suburb_values 1        //this will be the index 2 correct ?            
                                                           //Console.Clear();

                Console.WriteLine("Please enter a Post code number: ");
                //Adding values to the list
                postcode_values.Add(Console.ReadLine());    //postcode 1
                                                            // Console.Clear();
                counter++;
            }
        }
       
        catch (FormatException e)
        {
            Console.WriteLine(e.Message);
        }

        finally
        {
            Console.WriteLine(""); //This is Finally block and get executed regardless
        }

        //--------------FROM HERE SWITCH LOOP FOR AUCKLAND-------------------------------------------------------

        Console.WriteLine("\nPLEASE SELECT 1 TO INSERT - 2 TO REMOVE - OR 3 TO CHECK ELEMENTS IN THE LIST CONTAINS \n");            


        int userInput = Convert.ToInt32(Console.ReadLine());  //ASK YOUR INPUT WITH THIS CODE, what user type will be saved inside userInput


        switch (userInput)
        {   
            case 1:  //you are required to INSERT 5 pre-defined suburb and post code values into the respective lists and print the final values inside the list. ");
                {
                    try
                    {
                        Console.WriteLine("\nOption 1 Selected - PLEASE INSERT 5 SUBURBS AND POSTCODES FOR AUCKLAND AND WELLINGTON CITY");

                        //Inserting new values to the suburb_values list and postcode list 
                        Console.WriteLine("PLease insert a new Suburb for Auckland City: ");
                        suburb_values.Insert(0, Console.ReadLine());
                        //Console.Clear();
                        //---
                        Console.WriteLine("Please insert a new PostCode for the Aucland city: ");
                        postcode_values.Insert(0, Console.ReadLine());
                        //Console.Clear();
                        //---
                        Console.WriteLine("Please insert the next Suburb for Auckland City: ");
                        suburb_values.Insert(1, Console.ReadLine());
                        //Console.Clear();
                        //---
                        Console.WriteLine("Please Insert the next PostCode for Aucklanf city:");
                        postcode_values.Insert(1, Console.ReadLine());
                        //Console.Clear();
                        //---
                        Console.WriteLine("Please insert the next Suburb for Auckland City: ");
                        suburb_values.Insert(2, Console.ReadLine());
                        //Console.Clear();
                        //---
                        Console.WriteLine("Please Insert the next PostCode for Aucklanf city:");
                        postcode_values.Insert(2, Console.ReadLine());
                        //Console.Clear();
                        //---
                        Console.WriteLine("Please insert the next Suburb for Auckland City: ");
                        suburb_values.Insert(3, Console.ReadLine());
                        //Console.Clear();
                        //---
                        Console.WriteLine("Please Insert the next PostCode for Aucklanf city:");
                        postcode_values.Insert(3, Console.ReadLine());
                        //Console.Clear();
                        //---
                        Console.WriteLine("Please insert the next Suburb for Auckland City: ");
                        suburb_values.Insert(4, Console.ReadLine());
                        //Console.Clear();
                        //---
                        Console.WriteLine("Please Insert the next PostCode for Aucklanf city:");
                        postcode_values.Insert(4, Console.ReadLine());
                        //Console.Clear();
                    }
                    catch (FormatException e)
                    {
                        Console.WriteLine(e.Message);
                    }

                    finally
                    {
                        Console.WriteLine(""); //This is Finally block and get executed regardless
                    }


                    Console.WriteLine("Final Values Inside the Lists: ");
                    //printing the list
                    foreach (var add in suburb_values)
                        Console.Write(add + ": ");
                    //printing the list
                    foreach (var add in postcode_values)
                        Console.WriteLine(add);


                    break;
                }

            case 2:
                {   //REMOVING THE ELEMENT FROM THE LIST USING AT
                    Console.WriteLine("\nOption 2 selected - ");
                    try
                    {
                        int removing = 0;

                        while (removing < 4)             //There is 10 positions on the list but here user will remove only 4   //userinput position 0
                                                                                                                                //userinput position 1
                        {                                                                                                       //userinput position 2
                            Console.WriteLine("\nPLEASE ENTER A NUMBER TO DELETE THE SUBURB ON THE LIST\n");                    //userinput position 3
                            suburb_values.RemoveAt(Convert.ToInt32(Console.ReadLine()));                                        //userinput position 4
                                                                                                                                //userinput position 5
                            foreach (var sub in suburb_values)                                                                  //userinput position 6
                            {                                                                                                   //userinput position 7
                                Console.WriteLine(sub);                                                                         //userinput position 8
                                //removing++;                                                                                   //userinput position 9
                            }                                                                                       
                            removing++;
                        }

                    }
                    catch (FormatException e)
                    {
                        Console.WriteLine(e.Message);
                    }

                    finally
                    {
                        Console.WriteLine(""); //This is Finally block and get executed regardless
                    }
                    //suburb_values.RemoveAt(0);
                    break;
                }
            case 3:
                {
                    Console.WriteLine(" you  are  required  to  check  if  the  suburb_values  list  contains  atleast  5 suburbs that the user will enter. ");
                    // code to check missing
                    //check list here 
                    Console.WriteLine("Checking List values not index number 8 :{0}", suburb_values.Contains("")); // Cheking is 8 in the pincode list or not.
                    Console.WriteLine("Checking values Grey Lynn exist in the subrub list :{0}", suburb_values.Contains("Grey Lynn"));
                    break;
                }
            default:
                Console.WriteLine("Invalid");
                break;
        }
        Console.ReadKey();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM