2

I am writing a program for shopping system. In this I am using array to get input from the brand name from the user. I am using method which returns string to get the input. Following is code:

public class Brand{
 private string brandName;

    public string BrandName
    {
        get { return brandName; }
        set { brandName = value; }
    }
    public string getBrandName()
    {
        string[] brands = new string[5];
        brands[0] = "Honda";
        brands[1] = "Suzuki";
        brands[2] = "Ferrari";
        brands[3] = "BMW";
        brands[4] = "Toyota";
        Console.WriteLine("Please enter the brand name from the above given   brands..");
        string temp = Console.ReadLine();
        do
        {
            try
            {
                for (int i = 0; i < 6; i++)
                {
                    if (brands[i].Contains(temp))
                    {
                        this.BrandName = temp;
                        break;

                    }

                }
                return this.BrandName;
            }
            catch
            {
                Console.WriteLine("Your provide brand does not match with the database in our system. Please try another one.");
            }
        } while (BrandName!=temp);



    }

    }

The problem is that I am at beginner level and not getting the trick what should be in this while statement that it loops and asks user to input again and again until he enters the correct brand name. Please help me.

3 Answers 3

2

Maybe this will work based on your code:

public string getBrandName()
{
    string[] brands = new string[5];
    brands[0] = "Honda";
    brands[1] = "Suzuki";
    brands[2] = "Ferrari";
    brands[3] = "BMW";
    brands[4] = "Toyota";
    Console.WriteLine("Please enter the brand name from the above given   brands..");
    string temp = Console.ReadLine();
    while(!brand.Contains(temp))
    {
        Console.WriteLine("Your provide brand does not match with the database in our system. Please try another one.");
        temp = Console.ReadLine();
    }
    return temp;
}

Few things to notice:

  1. We will ask the user for a brand name.

  2. We will check that the input is a brand from brands list (you use contains to check if the input is in the char array of every brand name, watch the difference).

  3. If then name is in the list we will not enter inside the loop and we will return the brand name.

  4. If the name is not in the list we will ask the user again to insert a valid brand name until he will enter any and then we will return it.

0
0

if you have only 4 no of brands then you can try or statement for all of them in while loop

while (input== 'brand1'||'brand2')

or if your list is too big then you can put them in an arraylist

like this

List <String> listClone = new ArrayList<String>(); 
       for (String string : list) {
           if(string.matches("(?i)(bea).*")){
               listClone.add(string);
           }
       }
    System.out.println(listClone);
3
  • you mean i should use variable instead of array for each brand
    – Ahmad
    Oct 8, 2016 at 19:47
  • It depends on your needs..you can also try while(!brand.Contains(temp)) but then you wont be able to recognize the brand chosen Oct 8, 2016 at 19:49
  • this will work because i will assign this to the current object property BrandName which will be returned as a string from the function. Hope fully. thanks for your help bro
    – Ahmad
    Oct 8, 2016 at 19:56
0
  1. You have an outOfRange exception here : for (int i = 0; i < 6; i++) beacuse there is out of range from the array where brands[5].
  2. The function contains return true or false when the input string is substring of the specified string and not equel !!!
  3. You arrive to this row only if has exception: Console.WriteLine("Your provide brand does not match with the database in our system. Please try another one.");

Check the following code - it work well :

class Program { static void Main(string[] args) { Brand brand = new Brand(); string brandName = brand.getBrandName(); Console.WriteLine("You enter correct brand name !!!"); Console.WriteLine(brandName); Console.ReadLine(); }

    public class Brand
    {
        private string brandName;

        public string BrandName
        {
            get { return brandName; }
            set { brandName = value; }
        }
        public string getBrandName()
        {
            bool isValid = false;
            string temp = "";
            string[] brands = new string[5];
            brands[0] = "Honda";
            brands[1] = "Suzuki";
            brands[2] = "Ferrari";
            brands[3] = "BMW";
            brands[4] = "Toyota";
            Console.WriteLine("Please enter the brand name from the above given   brands..");

            while (!isValid)
            {
                for (int i = 0; i < brands.Length; i++)
                {
                    Console.WriteLine(brands[i]);
                }
                temp = Console.ReadLine();
                for (int i = 0; i < brands.Length; i++)
                {
                    if (brands[i] == temp)
                    {
                        this.BrandName = temp;
                        isValid = true;
                        break;
                    }
                    else
                    {
                        isValid = false;
                    }

                    if (i == brands.Length - 1)
                    {
                        Console.WriteLine("Your provide brand does not match with the database in our system. Please try another one.");
                    }
                }
            }
            return temp;
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.