Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It would be much better implemented if you had a product struct that contained the Name and the Quantity.</p> <pre><code>struct Product { public string Name; public int Quantity; } </code></pre> <p>There's a way to fix your problem with a nested for loop and converting your arrays into a List.</p> <pre><code>List&lt;Product&gt; products = new List&lt;Product&gt;(); for(int i = 0; i &lt; listS.Length; i++) { bool duplicate = false; foreach(Product p in products) { if(listS[i].ToLower().Equals(p.Name.ToLower())) { p.Quantity += listI[i]; duplicate = true; break; } } if(!duplicate) { Product p; p.Name = listS[i]; p.Quantity = listI[i]; products.Add(a) } } </code></pre> <p>Then products will have no duplicates</p> <p>Edit: If you really want to stay with your code for thoughts that converting everything to a struct will result in a loss in performance, the fix implemented with your code will actually be very inefficient.</p> <p>The entire method can be implemented as such tho.</p> <pre><code>public void remove(string[] listS, int[] listI) { List&lt;Product&gt; products = new List&lt;Product&gt;(); for(int i = 0; i &lt; listS.Length; i++) { bool duplicate = false; foreach(Product p in products) { if(listS[i].ToLower().Equals(p.Name.ToLower())) { p.Quantity += listI[i]; duplicate = true; break; } } if(!duplicate) { Product p; p.Name = listS[i]; p.Quantity = listI[i]; products.Add(a) } } string[] lines = System.IO.File.ReadAllLines("c:\\z.txt"); for(int i = 0; i &lt; lines.Length; i++) { string[] linesSplit = lines[i].Split(' '); foreach(Product p in products) { if (linesSplit[0].ToLower().Equals(p.Name.ToLower())) { lines[i] = linesSplit[0] + " " + (int.Parse(linesSplit[1]) - p.Quantity); products.Remove(p) break; } } } System.IO.StreamWriter file = new System.IO.StreamWriter("c:\\z.txt"); file.Write(lines); file.Close(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload