EDN Admin
Well-known member
Hey everybody, I am undergoing studies with c#, and I need a little help with this assignment, it was due a couple of days ago but I just am stuck I guess. I am supposed to read data from a file (empdata.txt in my documents) and I have done some of what
was required. I declared my class as an array so I could get the information stored as a string. The data Im supposed to read is "1 John MerryWeather 123 West Main Street 5.00 30" with spaces and all, so I assume Im going to need a string array with a loop
to display all the data. I am using the algorithm that checks till the end of a file but Im pretty sure that all my information is placed incorrectly. Im just confused as to how to get a class to display this information honestly, weve done this before
but in the previous program mentioned rather than using a class, we just used an array to display the information. If anyone can provide any information on how to do this, or corrections, that would be helpful. pieces of this is copy and pasted because he
has some bits of information we used in previous programs like the streamreader section is copied. Im not sure what I need to display in the writeline on line 29 (haha, it looks so wrong) Im going to edit it and be sure to look over it a couple of more times
before I go to bed. Anyway, thanks for the help to whomever provides it.<br/>
The code below what I wrote (besides what I copied), please note that I am not a pro and am still learning. I added numbers next to the lines to help out, I hope it doesnt make things worse hah.<br/>
<br/>
1: using System;
2: //declare IO because were retrieving data<br/>
3: using System.IO;<br/>
<br/>
4: class Program<br/>
5: {<br/>
6: static void Main()<br/>
7: {
8: //declaring a constant because I need to put some information into my array
9: const int SIZE = 10;
10: //heres my array but I really dont think Im doing this right<br/>
11: Employee[] numbers = new Employee[SIZE];<br/>
12: Employee myEmployee = new Employee();<br/>
<br/>
<br/>
13: // This line of code gets the path to the My Documents Folder
14: string environment = System.Environment.GetFolderPath<br/>
15: (System.Environment.SpecialFolder.Personal) + "\";<br/>
<br/>
16: Console.WriteLine("Enter a file name in My Documents: ");<br/>
17: string input = Console.ReadLine();<br/>
<br/>
18: // concatenate the path to the file name<br/>
19: string path = environment + input;<br/>
<br/>
20:// now we can use the full path to get the document<br/>
21: StreamReader myFile = new StreamReader(path);<br/>
22: string n = (myFile.ReadLine());<br/>
23: if (n != null)<br/>
24: {<br/>
25: do<br/>
26: {<br/>
27: for (int i = 0; i < SIZE; i++)<br/>
28 : {<br/>
29: Console.WriteLine("read {0}",numbers.getEmpInfo());<br/>
30 : Console.ReadLine();<br/>
31 : }<br/>
32 : } while (n != null);<br/>
33: }<br/>
34: Console.ReadLine();<br/>
35: }//End Main()<br/>
36:}//End class Program<br/>
37:class Employee<br/>
38:{<br/>
39: private string empInfo;<br/>
<br/>
40: public string getEmpInfo()<br/>
41: {<br/>
42: return empInfo;<br/>
43 : }<br/>
<br/>
<br/>
}<br/>
<br/>
<br/>
View the full article
was required. I declared my class as an array so I could get the information stored as a string. The data Im supposed to read is "1 John MerryWeather 123 West Main Street 5.00 30" with spaces and all, so I assume Im going to need a string array with a loop
to display all the data. I am using the algorithm that checks till the end of a file but Im pretty sure that all my information is placed incorrectly. Im just confused as to how to get a class to display this information honestly, weve done this before
but in the previous program mentioned rather than using a class, we just used an array to display the information. If anyone can provide any information on how to do this, or corrections, that would be helpful. pieces of this is copy and pasted because he
has some bits of information we used in previous programs like the streamreader section is copied. Im not sure what I need to display in the writeline on line 29 (haha, it looks so wrong) Im going to edit it and be sure to look over it a couple of more times
before I go to bed. Anyway, thanks for the help to whomever provides it.<br/>
The code below what I wrote (besides what I copied), please note that I am not a pro and am still learning. I added numbers next to the lines to help out, I hope it doesnt make things worse hah.<br/>
<br/>
1: using System;
2: //declare IO because were retrieving data<br/>
3: using System.IO;<br/>
<br/>
4: class Program<br/>
5: {<br/>
6: static void Main()<br/>
7: {
8: //declaring a constant because I need to put some information into my array
9: const int SIZE = 10;
10: //heres my array but I really dont think Im doing this right<br/>
11: Employee[] numbers = new Employee[SIZE];<br/>
12: Employee myEmployee = new Employee();<br/>
<br/>
<br/>
13: // This line of code gets the path to the My Documents Folder
14: string environment = System.Environment.GetFolderPath<br/>
15: (System.Environment.SpecialFolder.Personal) + "\";<br/>
<br/>
16: Console.WriteLine("Enter a file name in My Documents: ");<br/>
17: string input = Console.ReadLine();<br/>
<br/>
18: // concatenate the path to the file name<br/>
19: string path = environment + input;<br/>
<br/>
20:// now we can use the full path to get the document<br/>
21: StreamReader myFile = new StreamReader(path);<br/>
22: string n = (myFile.ReadLine());<br/>
23: if (n != null)<br/>
24: {<br/>
25: do<br/>
26: {<br/>
27: for (int i = 0; i < SIZE; i++)<br/>
28 : {<br/>
29: Console.WriteLine("read {0}",numbers.getEmpInfo());<br/>
30 : Console.ReadLine();<br/>
31 : }<br/>
32 : } while (n != null);<br/>
33: }<br/>
34: Console.ReadLine();<br/>
35: }//End Main()<br/>
36:}//End class Program<br/>
37:class Employee<br/>
38:{<br/>
39: private string empInfo;<br/>
<br/>
40: public string getEmpInfo()<br/>
41: {<br/>
42: return empInfo;<br/>
43 : }<br/>
<br/>
<br/>
}<br/>
<br/>
<br/>
View the full article