This is my sorting code for a simple program which would read a llist of names (.txt, First Last) and sort those names alphabeta and write into an output file.
I'm new to C# and this program has been on my mind for about 3 weeks on/off (really off??)
As you can see I'm using an ArrayList and StreamReader/Writer.
The routine shown here is just the sorting, the output I have prepared, and will think about once I get passed this part, Can anyone give me some hints to what i'm doing wrong?
using System;
using System.IO;
using System.Collections;
namespace ConsoleApplication1
{
class Class1
{
static void Main(string[]args)
{
ArrayList db = new ArrayList ();
string infile,outfile,compare;
Console.Write("Where is your input file: ");
infile = Console.ReadLine();
if (!File.Exists(infile))
return;
StreamReader sr = File.OpenText(infile);
Console.Write("Where is your output file, if the location specified does not exist, I will create it: ");
outfile = Console.ReadLine();
if (File.Exists(outfile))
{
Console.Write("Would you like me to overwrite this file? (y/n)");
compare = Console.ReadLine();
if (compare.Equals("n"))
return ;
}
StreamWriter sw = File.CreateText(outfile);
while ((compare = sr.ReadLine().Trim) != null)
{
//begin for loop
if (!compare.CompareTo(db[x]).Equals(0))
db.Add(compare);
//End Loop
}
db.Sort();
for (int x = 1; x <= db.Count; x++)
{
sw.WriteLine(db[x - 1]);
//Console.WriteLine(db.Count + "and" + db[x]);
}
}
}
}
}
edited: o0o0o0o0o, I just employed Sort(), works neat, i'm tryin that, i'll update my new code shortly!
edited: I should probably mention, im using CompareTo locate any duplicates, So basically I can remove those CompareTos and replace them with a single Sort(), and simply add a CompareTo that checks if it's '0' which means a duplicate that should be removed from the arraylist. Easier said than done for me :(
edited: added full code, sorry anyone that had trouble understanding what I was tryin to do, I hope it's easier to give inputs now, and I appreciate it.
Question
+Fulcrum Subscriber¹
This is my sorting code for a simple program which would read a llist of names (.txt, First Last) and sort those names alphabeta and write into an output file.
I'm new to C# and this program has been on my mind for about 3 weeks on/off (really off??)
As you can see I'm using an ArrayList and StreamReader/Writer.
The routine shown here is just the sorting, the output I have prepared, and will think about once I get passed this part, Can anyone give me some hints to what i'm doing wrong?
edited: o0o0o0o0o, I just employed Sort(), works neat, i'm tryin that, i'll update my new code shortly!
edited: I should probably mention, im using CompareTo locate any duplicates, So basically I can remove those CompareTos and replace them with a single Sort(), and simply add a CompareTo that checks if it's '0' which means a duplicate that should be removed from the arraylist. Easier said than done for me :(
edited: added full code, sorry anyone that had trouble understanding what I was tryin to do, I hope it's easier to give inputs now, and I appreciate it.
Edited by DarkRyuLink to comment
Share on other sites
10 answers to this question
Recommended Posts