My code is currently busted and I am aware of this. I'm working one one part at a time and I have hit two stumbling blocks which I'm hoping to find help here in regards too.
I am trying to address my formatting issue with this question here.
With the code below, i read in a text file using StreamReader, and the selections come in all on the same line as I want them too seperated by a BAR ("|"). Unfortunately, the bar prints at the end of each word, not actually 'between' each word. I don't want the last bar to print. I know i could cheat and add a bar to the font with a Console.Write("|") but that would be cheating in my mind - i wouldn't really be printing/displaying a bar between the words as I was trying to make a more elegant console selection.
This is just something I'm doing to learn and is not part of our assignment, but i like to push myself when I can. I am very very new to attempting to code, I am a complete newb, please assume I know nothing and please know all help is very much appreciated.
THIS IS MY CODE THUS FAR:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;// Needed for Streaming...
using System.IO;// Needed for Streaming...
namespace a090___StreamReader_DictionarySearch
{
class Program
{
private const String FILE_NAME = "dictionary.txt";//establish text file instance
public void Play()
{
do
{
DisplayTitle();
List<Listing> items = LoadListings();//create a list of WordDef objects
int count = 0;
foreach (Listing myListing in items)//read in items from file
{
//Console.WriteLine(myListing.GetName() + ": " + myListing.GetDefinition());
Console.Write(myListing.GetName());
if (count != items.Count - 1)
{
Console.Write(" | ");
}
count++;
}
DisplayText("\n\nPlease enter a word from the selections about to see it's definition");// Nice use of PROMPT
String userSelection = Console.ReadLine().ToLower();//Capture input
/// What are we trying to do?
/// Collect value entered for comparison - chek!
///
/// Compare value entered against list (Does value match to name?)
/// IF value matches print description
/// if value does not match(Else), state no match
//if (userSelection == Listing.name)
//{Console.WriteLine("You selected: " + userSelection() +
// "\nWhich Means: " + Listing.GetDefinition());}
//else{Console.WriteLine("I'm sorry, I don't have a match for that.");}
} while (PlayAgain());
Salutation();
}
//ToolBox -- my program specific tools
public List<Listing> LoadListings()//load entries display as list
{
StreamReader fileIn = new StreamReader(FILE_NAME);
List<Listing> entry = new List<Listing>();
//loop through every line of the file
while (!fileIn.EndOfStream)
{
String line = fileIn.ReadLine();
String[] pieces = line.Split(':');
if (pieces.Length < 1) continue;//error handling - set to length of text items
Listing myListing = new Listing(pieces[0], pieces[1]);
entry.Add(myListing);
}
fileIn.Close(); return entry;
}
//MaxBox -- my useful tools
public void DisplayText(String StringNameIs)
{ Console.WriteLine(StringNameIs); }//Where are we?
public Boolean PlayAgain()
{
Console.Write("\n\nDo you select again? (y)es or (n)o: ");
String command = Console.ReadLine().ToLower();
if (command == "y" || command == "yes") return true;
return false;
}
public void Salutation()
{ Console.Clear(); Console.WriteLine("Ti Do - oh - oh Ti Do -- So Do!"); } //The last line from the 'do-re-mi' song from the Sound of Music
public void DisplayTitle()
{ Console.Clear(); Console.WriteLine(">>>-- A Dictionary of Sounds --<<< \n"); } //Announce Our Program
static void Main(string[] args)
{
Program DictionaryLookup = new Program();
DictionaryLookup.Play();
Console.Read();
}
}
}
Please note: I have searched StackOverflow, GOOGLE, BING, MS' resources, et al. for an answer i can understand with my limited skills/understanding. I've also been working on this for a few hours. Please help.
You're looking for string.Join
. Replace the whole foreach
loop
foreach (Listing myListing in items)//read in items from file
{
//Console.WriteLine(myListing.GetName() + ": " + myListing.GetDefinition());
Console.Write(myListing.GetName() + " | ");
}
with
Console.Write(string.Join(" | ", items.Select(x => x.GetName())));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With