Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to read and write a file after receiving an array with repeated items
    primarykey
    data
    text
    <p>I receive a list of products(listS) and a list of its quantity(listI), what I want is to remove some items from my database, depending on the quantity that I receive on the listI. It is working like a charm, as long as, I don't receive repeated products on the listS, and that's what I'm trying to solve.</p> <pre><code> public void remove(string[] listS, int[] listI) { int check = 0; int quantidadeDB; string ingredientDB; string ingredient; int quantity; string newLine; string[] linesSplit; string[] lines = System.IO.File.ReadAllLines("c:\\z.txt"); System.IO.StreamWriter fileTemp = new System.IO.StreamWriter("c:\\z2.txt"); for(int i = 0; i &lt; lines.Length;i++) { linesSplit = lines[i].Split(' '); ingredientDB = linesSplit[0]; quantityeDB = int.Parse(linesSplit[1]); check = 0; for(int j=0;j&lt;listS.Length;j++) { ingredient = listS[j]; quantity = listI[j]; if (ingredientDB.ToLower().Equals(ingredient.ToLower())) { newLine = ingredientDB + " " + (quantityDB - quantity); fileTemp.WriteLine(newLine); check++; } } if(check.Equals(0)) fileB.WriteLine(ingredientDB + " " + quantityDB); } fileTemp.Close(); string text = System.IO.File.ReadAllText("c:\\z2.txt"); System.IO.StreamWriter fileL = new System.IO.StreamWriter("c:\\z.txt"); fileL.Write(text); fileL.Close(); } </code></pre> <p>Example: If my database was this one: Fanta 100 Pepsi 2 Coca-Cola 3 IcedTea 23 Ketchup 23</p> <p>After receiving this arrays: </p> <pre><code>listS= {"Pepsi", "Pepsi", "Ketchup", "Pepsi", "Ketchup"}; listI = {2, 3, 1, 3, 2}; </code></pre> <p><strong>This is how it should the output:</strong></p> <pre><code>Fanta 100 Pepsi -6 Coca-Cola 3 IcedTea Ketchup 20 </code></pre> <p><strong>Instead of:</strong> </p> <pre><code>Fanta 100 Pepsi 0 Pepsi -1 Pepsi -1 Coca-Cola 3 IcedTea 23 Ketchup 22 Ketchup 21 </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.
 

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