Ahmad
Ahmad

Reputation: 887

using do while statement in method returning string

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.

Upvotes: 2

Views: 87

Answers (3)

idan
idan

Reputation: 91

  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;
        }
    }
}

Upvotes: 0

YuvShap
YuvShap

Reputation: 3835

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.

Upvotes: 2

Sarmad Shah
Sarmad Shah

Reputation: 3805

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);

Upvotes: 0

Related Questions