0

I can't seem to find much on this online :(

For my course I have to get text from a grocery.txt file and output an invoice.

The text file looks like this:

regular,bread,2.00,2
regular,milk,2.00,3
regular,cereal,4.00,1
fresh,rump steak,11.99,0.8
fresh,apple,4.60,1.00
fresh,cucumber,2.20,0.936
regular,cream,2.20,1
regular,mustard,3.30,1
fresh,carrots,1.79,1.5
regular,tomato sauce,2.95,1
regular,salt,2.80,1
fresh,bananas,4.00,1.2

Currently I use this method to get the text:

    string[] groceryItemsArray = System.IO.File.ReadAllLines(@"C:\C\groceries.txt");

What I get is an array that in stores the entire line (for example "fresh,bananas,4.00,1.2"). I have no idea how to split the array into sub arrays or even whats the best way to go about this. Unfortunately this course task are way too advanced for the little material we have been taught and time we have for this. I have already spend around 6 hours watching videos and getting not very far in learning the advanced stuff.

This is what I am trying to accomplish.

A. Class named grocerrItem. This class has properties: name and price this class must have 2 constructors

A. subclass of this is a purchasedItem. this class has an integer property 'quantity' one of its methods findCost, calculates the cost of the item, including 10% GST. the formula for this is price * quantity * 1.1

C. A subclass of this class (purchasedItem), freshItem has a property weight, a double type. The findCost method here, uses the formula:wieght * price, as it is not subject to GST.

Groceries.txt contains information as follows:

type(regular or fresh), name, price, quantity/weight - depending on being regular or fresh.

** An invoice should be represented as a collection of grocery items (parent class). This can be done using any container structure i.e. array, list or collection. you will then use iteration

5
  • 1
    possible duplicate of CSV File Imports in .Net Commented May 28, 2015 at 5:16
  • Just to confirm, are you trying to split it into individual strings? Or are you trying to have a "sub array" for each line? What do you mean by "sub arrays?" There are a ton of ways of getting either one of those done very easily, but we need more information. Commented May 28, 2015 at 5:16
  • 2
    Look up string.Split() Commented May 28, 2015 at 5:17
  • Could you add what you expect as output? Commented May 28, 2015 at 5:17
  • Sorry, added outcome to question. Commented May 28, 2015 at 5:42

3 Answers 3

2

You can just use String.Split which returns you the sub array that you want.

public static void Main()
{
    string[] groceryItemsArray = System.IO.File.ReadAllLines(@"C:\C\groceries.txt");

    // now split each line content with comma. It'll return you an array (sub-array)
    foreach (var line in groceryItemsArray)
    {
        string[] itemsInLine = line.Split(',');

       // Do whatevery you want to do with this.
       // e.g. for 1st line itemsInLine array will contains 4 items 
       // "regular","bread","2.00", "2"
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Interesting, makes sense. but when I try to write out itemsInLine I get an error "The name itemsInLine does not exist in this context". Can you please explain a little how this works?
@RiCHiE where are you accessing that itemsInLine variable and also let me know, what actually you want to do at the end so that I can explain how to achieve that
Never mind sorry I was using wrong syntax. I'm using Console.Write(groceryItemsArray[0][1]); this seems to be seperated by letter though and not by the ,
2

You can use this code (I'm not sure what exactly you looking for):

/// It's return all text as a single list
var groceryItemsArray = System.IO.File.ReadAllLines(@"C:\C\groceries.txt")
      .SelectMany(x => x.Split(new char[]{','} , StringSplitOptions.RemoveEmptyEntries)).ToList() ;

Or if want to return as Child List can use this code:

 var groceryItemsArray = System.IO.File.ReadAllLines(@"C:\C\groceries.txt").Select(x => 
           new { Child = x.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries) }).ToList();

2 Comments

Looking to split the array up by each item in each line but not loose each line as an item.
Lambdas might be a bit too advanced for him. He needs to understand what they are, what they do and how they should be used... not just given the code. Just a thought.
1

I didn't really understand what are you trying to output, but here's some basic things I did understand on your question:

(code not tested)

I created a parent class:

class GroceryItem 
{
    public string ItemName {get;set;}
    public decimal Price {get;set;}

    public GroceryItem() { }
}

And creates a child class:

class PurchasedItem : GroceryItem
{
    public int Quantity { get;set; }
    public decimal Cost { get;set; }

    public PurchasedItem(string[] item)
    {
        ItemName = item[1];
        Price = decimal.Parse(item[2]);
        Quantity = int.Parse(item[3]);
        FindCost();
    }

    private void FindCost()
    {
        Cost = Price * Quantity * 1.1;
    }
}

Using your input to get all groceryItems, you can iterate using foreach loop and collect a list of purchasedItems.

Main()
{
    string[] groceryItems = System.IO.File.ReadAllLines(@"C:\C\groceries.txt");

    var purchasedItems = new List<PurchasedItem>();
    foreach (var item in groceryItems)
    {
        string[] line = item.Split(',');
        purchasedItems.Add(new PurchasedItem(line));
    }
}

Well if you didn't understand this basic concept of programming, you better start on this link.

3 Comments

Yeah a bit lost I guess, been through a lot of basic material.
If you didn't understand the example that I've posted. I recommend you to start with the link I included to my answer, good luck! :)
Yeah I'm having a look, I don't see why it's so complicated to turn the array into a multidimensional array that has the 4 split values similar to what Sachin posted. Maybe my thinking of other languages is completely different to C#

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.