SunDontShine
New member
- Joined
- Aug 14, 2014
- Messages
- 2
- Programming Experience
- Beginner
hi Guys,
Am i doing the try / Catch statement correctly?
get the folllowing error when i 'read' the file first before splitting it. error relates to my split method
any reasons? I can not pin point it out.
An unhandled exception of type 'System.NullReferenceException' occurred in SortedTest.cs.exe
Additional information: Object reference not set to an instance of an object.
Idk why my counter is going out of bounds at the end of my program. It writes the elements in the 'names' array fine
without it i get an 'out of bounds' error.
this is suppose to be a simple program that takes names from a file and puts them in a 2d array called 'names'. thanks
regards,
Sun
Am i doing the try / Catch statement correctly?
get the folllowing error when i 'read' the file first before splitting it. error relates to my split method
any reasons? I can not pin point it out.
An unhandled exception of type 'System.NullReferenceException' occurred in SortedTest.cs.exe
Additional information: Object reference not set to an instance of an object.
Idk why my counter is going out of bounds at the end of my program. It writes the elements in the 'names' array fine
without it i get an 'out of bounds' error.
this is suppose to be a simple program that takes names from a file and puts them in a 2d array called 'names'. thanks
regards,
Sun
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; namespace SortedTest.cs { class Program { private string data; private string spl; private StreamReader sr; private StreamReader reader; private StreamWriter writer; private string[,] names; static void Main(String[] args) { Program test = new Program("input.txt"); test.ReadFile(); test.SplitFile(); test.writeFile("output.txt"); Console.ReadKey(); } //When instantiating, stream reader takes in the parameter the 'file name' public Program(string filename) { data = ""; spl = ""; sr = new StreamReader("../../" + filename); reader = new StreamReader("../../" + filename); names = new string[10, 2]; } // class methods private void ReadFile() { while (data != null) { data = reader.ReadLine(); Console.WriteLine(data); } } private void SplitFile() { try { int c1 = 0; int c2 = 0; int counter = 0; for (int i = 0; i <10; i++) { spl = reader.ReadLine(); if (spl != String.Empty) { String[] orig = spl.Split(','); // assign names in to 2d array first column names[c1, c2] = orig[counter]; Console.WriteLine(names[c1, c2].Trim()); c2++; counter++; //assign 2d array second column names[c1, c2] = orig[counter]; counter--; //write and set 2d array counters to select next name Console.WriteLine(names[c1, c2].Trim()); c1++; c2--; } else continue; } reader.Close(); } catch (IndexOutOfRangeException ) { } } private void writeFile(String newfile) { writer = new StreamWriter("../../" + newfile); writer.WriteLine("Last Names"); for(int i = 0; i < 10; i++) { writer.WriteLine(names[i, 0]); } writer.WriteLine("\n"); writer.WriteLine("First Names"); for(int i = 0; i < 10; i++) { writer.WriteLine(names[i, 1]); } writer.Close(); } } }
Last edited: