-1

Can someone please tell me what I'm missing, the class reads values from a text file and should store them for other use, I can see the values with Console.Write(eachCell[0]) but I can't save the values. I've tried using string[] and List<string> but no luck. It should store the values being read to a list or array but so far it is not. Nothing shows on the console.

class test
{
    public void tryOut()
    {
        #region file.read
        try
        {
            string fileLocation = @"shelfInfo.txt";
            List<string> cells = File.ReadAllLines(fileLocation).ToList();

            aray(cells);

        }
        catch
        {
            new FileNotFoundException();
        }
        #endregion
    }


    public void aray(List<string> cells)
    {
        string[] t = new string[20];
        string[] k = new string[20];
        string a = "", b = "", c = "", d = "";
        int i = 0;
        foreach (string cell in cells)
        {
            string[] eachCell = cell.Split('@', '\t', '\n');
            a = t[0] = eachCell[0].ToString();  //Consol.Write on eachCell directly shows the right values.
            b = t[1] = eachCell[1].ToString();
            c = t[2] = eachCell[2].ToString();
            d = t[3] = eachCell[3].ToString();
            for (; i < eachCell.Length; i++)
            {
                k[i] = a;  //should store the values from eachCell
            }
        }
        Console.Write(" " + a + " " + b + " " + " " + c + " " + d); //means the value are being receivedbut no stored
    }
}
// contents of text file
//A11[1]@ A12[0]@ A13[1]@ A14[1]@ 
//A21[1]@ A21[1]@ A23[0]@ A24[0]@
//A31[1]@ A32[0]@ A33[1]@ A34[1]@
//A41[1]@ A41[1]@ A43[0]@ A44[0]@ 

I'd also, appreciate any tips on exception handling.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • Put `Console.Write` inside `foreach` loop. – apocalypse May 18 '17 at 10:25
  • 1
    where do you want to "save" values? – Nino May 18 '17 at 10:25
  • 1
    What's the meaning of `List ne = new List(); int i = 0;`? You don't seem to be using it. Why do you declare `int i = 0;` outside the `for` loop? What is the use of that `for` loop at all (filling `k` with `a` a bunch of times)? Why do you fill `k` if you never use it? – Corak May 18 '17 at 10:32
  • Pro tip: always format and indent your code neatly before putting it in a question, don't make us wade through a mess... Also, be clear about what goes wrong: "I can't save" does not tell us at all what is happening vs what you wish to happen. – Peter B May 18 '17 at 10:41
  • i was using 'List ne' to save the values but that didn't work. i'll remove it 'int i' and the 'for()' loop is to iterate through 'ne' 'string[] k' is also supposed to store the values – Tobi Adediji May 18 '17 at 11:23
  • the program compiles and runs but when i want to display the stored values, nothing shows up on the console – Tobi Adediji May 18 '17 at 11:24
  • What is `t` used for? Why is `t` set to size `20` if you only ever store `4` items? And again, what is `k` used for? Why does it have a fixed size of `20`? Why not have it depend on `eachCell.Length`? And why `k[i] = a;`? Why not `k[i] = eachCell[i];`? Why use `eachCell[?].ToString()`? The items in `eachCell` can already only be `string` values. And the main question: why not debug you code step by step by setting a breakpoint for example where you set the `fileLocation` and then hit `` until you get to the point where things don't go like you think they should go? – Corak May 18 '17 at 12:43
  • thanks so much. I didn't know how to do that till now. The problem is that it keeps reading from the file even after there is no more text in it. i need a way to stop the ' List cells = File.ReadAllLines(fileLocation).ToList();' after the text has finished. but 'List cells = (File.ReadAllText(fileLocation).ToList()).ToString;'doesn't work – Tobi Adediji May 18 '17 at 13:51

1 Answers1

1

Your program doesn't return anything because you use the following code.

catch { new FileNotFoundException(); }

Console.Write return nothing just because the exception is caught but it doesn't throw a new exception. There is an exception because eachCell doesn't contain 4 elements and you try to access the element. In fact, you don't have to do the try-catch unless you want to handle this exception manually. If the file isn't there, a FileNotFoundException would already be thrown automatically. Change the tryOut method as the following.

public void tryOut()
{
    #region file.read
    var fileLocation = @"Path";
    aray(File.ReadAllLines(fileLocation).ToList());
    #endregion
}

public static void aray(List<string> cells)
{
    List<string> t = new List<string>();
    foreach (string cell in cells)
    {
        string[] eachCell = cell.Split('@', '\t');
        foreach (var e in eachCell)
        {
            t.Add(e);
        }
    }
    foreach (var e in t)
    {
        Console.WriteLine(e);
    }
}
Lucas Chau
  • 41
  • 5
  • 1
    +1 for taking `aray(cells)` out of the `try`. Question is, if this `try..catch` even makes sense. If the file isn't there, a `FileNotFoundException` would already be thrown. Maybe `if (File.Exists(fileLocation)) { ... }` would be helpful to minimize (but not completely remove!) the risk of trying to read a file that isn't there. – Corak May 18 '17 at 11:03
  • not using the 'try...catch' the program gets stuck in an endless writting loop – Tobi Adediji May 18 '17 at 11:34
  • yes, i have. Reading the values work fine but the program also needs to save the values (for use in other part of the code). – Tobi Adediji May 18 '17 at 12:00
  • When you use the code above, does it throw exception or display the value of a b c d ? – Lucas Chau May 18 '17 at 12:03
  • I have tried it. I should be either throw an IndexOutOfRangeException or display the value probably. It throws an IndexOutOfRangeException because you use cell.Split('@', '\t', '\n'). Whenever your `txt` doesn't have at least 3 '@' or '\t', `eachCell` will not have 4 elements. `\n` in your Split is useless because `cells` is already a List of string of each line. – Lucas Chau May 18 '17 at 12:25
  • without the '@' i get an IndexOutOfRangeException – Tobi Adediji May 18 '17 at 12:26
  • Have you changed the `fileLocation` to your own path in the above code? What text is inside the `txt`? – Lucas Chau May 18 '17 at 12:29
  • `IndexOutOfRangeException` will also be thrown, if `eachCell.Length > 20`. – Corak May 18 '17 at 12:34
  • yes, i did. Also, 'eachCell' only has 4 members each time. ps, I added a sample of the text content to the code above – Tobi Adediji May 18 '17 at 12:40
  • I have replaced your sample of the text content into my `txt` file. It displays properly. – Lucas Chau May 18 '17 at 12:45
  • You should also put the `Console.Write` inside your `foreach` loop to view all of them. – Lucas Chau May 18 '17 at 12:47
  • it should store into 'List t' then display the contents of 't' – Tobi Adediji May 18 '17 at 12:49
  • First, in your code above, t is an array not a List. Second, in your code above, you use `a,b,c,d` for display not `t`. Third, in your `foreach` loop, it always reassign the vale of t[0], t[1], t[2,] and t[3] only, t[4] and after will not be assigned. – Lucas Chau May 18 '17 at 12:57
  • i changed it to 'List t'and used 't.Add((string)eachCell[i].ToString());' to add the values. 'a,b,c,d' are just to check if the values are being read or not – Tobi Adediji May 18 '17 at 13:06
  • According to your description, I have rewritten the `aray` method. Please check if it is work for you. – Lucas Chau May 18 '17 at 14:16
  • i'm still getting a' IndexOutOfRangeException' I believe the problem is that it keeps reading from the file even after there is no more text in it. i need a way to stop the ' List cells = File.ReadAllLines(fileLocation).ToList();' after the text has finished. but 'List cells = (File.ReadAllText(fileLocation).ToList()).ToString;'doesn't work – Tobi Adediji May 18 '17 at 14:26
  • I think it is because your content of text is in wrong format. I've edited the code again. You could try that again. – Lucas Chau May 18 '17 at 14:30
  • Wow! thank you so much!!!! this works. I've been staring at the same screen since the day before. Thanks a bunch. i still don't understand what went wrong though, i think i tried this same thing a few times before. But i need a rest. – Tobi Adediji May 18 '17 at 14:56
  • It shows that at least one of the line in your content doesn't contain 3 `@` – Lucas Chau May 18 '17 at 15:00