0

I've a code that display information, everything work like as well, but the problem occur when i using while statement. When i repeat the process again, error message show appear (so the error come after through while process).

Error Picture:

Error Picture

Here's my code:

int u = 0;
int i = 0;
public  void cs4()
    {
        Console.Clear();
        while (Repeat == "Y" || Repeat == "y")
        try
        {
            f = new FileStream(@"E:\Data.txt", FileMode.Open, FileAccess.Read);
            r = new StreamReader(f);
            int VNN = 0;
            while (VNN == 0)
            {
                try
                {
                    Console.WriteLine("\t\t    Here's the current information of Product");
                    case1 q = new case1();
                    q.cs1();
                    Console.WriteLine("\t\t   To update product information, type the Product ID");
                    Console.Write("Type the Product ID\t\t\t: ");
                    nuggetid[i] = Console.ReadLine();
                    if (nuggetid[i].Length == 0)
                    {
                        Console.WriteLine("Sorry you cannot enter minus value!");
                        VNN = 0;
                    }
                    else
                    {
                        VNN = 1;
                    }
                }
                catch (FormatException)
                {
                    Console.WriteLine("Invalid Format");
                    VNN = 0;
                }
            }
            Boolean find = false;
            String Str;
            String Chkstr1;
            int Pos;
            info[0] = "Product ID\t";
            info[1] = "Product Name\t";
            info[2] = "Product Price";
            info[3] = "Current stock";
            f1 = new FileStream(@"E:\Info.txt", FileMode.Append, FileAccess.Write);
            w = new StreamWriter(f1);
            String alltext = "";
            while ((Str = r.ReadLine()) != null)
            {
                Chkstr1 = nuggetid[i].ToString();
                Pos = Str.IndexOf("#");
                String Chkstr2 = Str.Substring(0, Pos);
                String newLine = "";
                if ((Chkstr1.CompareTo(Chkstr2) == 0))
                {
                    String[] elemen = Str.Split('#');
                    Console.WriteLine("\t\t    Here's the detail informations of related product");
                    for (int a = 0; a < 4; a++)
                    {
                        string s = info[u];
                        Console.WriteLine((a + 1) + ". " + s + "\t: " + elemen[a]);
                        u++;
                    }
                    Console.Write("Type the number to edit data [1-4]\t: ");
                    find = true;
                    int indexYangdiUbah = Convert.ToInt32(Console.ReadLine());
                    if (indexYangdiUbah == 1)
                    {
                        Console.WriteLine("The current product ID\t\t\t: " + Chkstr2);
                        Console.Write("Type the new product ID\t\t\t: ");
                        elemen[indexYangdiUbah - 1] = Console.ReadLine();
                    }
                    if (indexYangdiUbah == 2)
                    {
                        Console.WriteLine("The product ID\t\t\t\t: " + Chkstr2);
                        Console.Write("Type the new product name\t\t: ");
                        elemen[indexYangdiUbah - 1] = Console.ReadLine();
                    }
                    if (indexYangdiUbah == 3)
                    {
                        Console.WriteLine("The product ID\t\t\t\t: " + Chkstr2);
                        Console.Write("Type new product price\t\t\t: ");
                        elemen[indexYangdiUbah - 1] = Console.ReadLine();
                    }
                    if (indexYangdiUbah == 4)
                    {
                        Console.WriteLine("The product ID\t\t\t\t: " + Chkstr2);
                        Console.WriteLine("The old stock is\t\t\t: " + elemen[indexYangdiUbah - 1]);
                        Console.Write("Type the new quantity stock do want\t: ");
                        elemen[indexYangdiUbah - 1] = Console.ReadLine();
                    }
                    newLine = elemen[0];
                    for (int a = 1; a < elemen.Length; a++)
                    {
                        newLine = newLine + "#" + elemen[a];
                    }
                    alltext += newLine + "\n";
                    Console.WriteLine("\nThe data has been successfully changed!\n");
                }
                else
                {
                    alltext = alltext + Str + "\n";
                }
            }
            if (!find)
            {
                Console.WriteLine("Sorry, the ID not found!");
            }
            w.Close();
            r.Close();
            File.WriteAllText(@"E:\Data.txt", alltext);
            Console.Write("Do you want update another data? [y-n]\t: ");
            Repeat = Console.ReadLine();
        }
        catch (FileNotFoundException)
        {
            Console.WriteLine("Sorry the data is not found!");
            Console.WriteLine("Make sure the file is exist!");
        }
iMar
  • 25
  • 4
  • where is your variable 'u'? You are iterating through 'a' and indexing on 'u' – Jinish Mar 28 '17 at 11:21
  • i wrote var 'u' above of that code, int u = 0; – iMar Mar 28 '17 at 11:22
  • and what this **info** has??? – J.SMTBCJ15 Mar 28 '17 at 11:23
  • what value does u have when the error occurs? How big is the array info? – PaulF Mar 28 '17 at 11:24
  • this var, take look again, i've edited main post – iMar Mar 28 '17 at 11:26
  • 1
    What is element[a] ? – Christophe Mar 28 '17 at 11:30
  • you should post the entire while loop. the posted code does not reproduce the error. Try it yourself in an extra console project of in LinqPad – Mong Zhu Mar 28 '17 at 11:40
  • Where is the while loop that you talk about - it does not appear in your code or your picture. – PaulF Mar 28 '17 at 11:50
  • okay i'm sorry folks if i does not give entire code. Okay i update again the main post – iMar Mar 28 '17 at 12:02
  • 1
    Well - you initialise u outside of your method, inside the method it is just incremented - hence first time through the while it is OK, second time through it is already out of bounds for the array. Move the initialisation to just before the for loop. Is it possible to use your loop counter a, rather than a separate variable u? – PaulF Mar 28 '17 at 12:37
  • Ohh okay, working like a charm thanks. So i just need counter a to perform looping?. BTW, how to up vote comment? – iMar Mar 28 '17 at 13:15
  • @iMar you need more reputation. I did it for you ;) – Mong Zhu Mar 28 '17 at 14:36

0 Answers0