-3

I am trying to do this book exercise for quite a while now in C# using visual studio and windows forms, however, I am having trouble with the last few steps and the book has no solution manual that I can look to for help.

Here is what the exercise says:

  1. Read in the file
  2. Split the file line-by-line
  3. Push each line on to a stack
  4. Pop each line out to a results Window
  5. Save the reversed file (the sentences should be reversed).

Here is a picture of what the windows form should look like:

What the windows form should look like

Here is the sample .txt file named SocialJustice-SampleText.txt:

The idea of social justice is innately a subjective concept. A socially created reality critical to the enactment of social institutions, ‘justice’ exists within the minds of all individuals as we each have varying ideas of what is just or unjust, fair or unfair, right or wrong (Tyler, 1997). In turn, what emerges from this socially created reality is considered the “first virtue” in the enactment of social institutions (Rawls, 1971), and the first form of criteria that emerges when political, legal, and other managerial authorities come under judgment (Kelman and Hamilton, 1989). From the perspective of the academician, social justice bears fruit in areas such as moral philosophy, theology, political science, law, social psychology, and many others. From the perspective of the citizen, social justice is at the heart of modern discourse on topics related to equal distributions of wealth (Piketty, 2014), equal distributions of healthy food (Alkon and Agyeman, 2011), and the general precept of human rights as the virtue of being able to achieve equal outcomes given equal effort (Cergy-Pontoise, 2005; Wilkinson and Pickett, 2010). Thus, social justice is instrumentalist an Pragmatist by its very nature (Fraser, 1998) as the psychology of social justice is predicated on the consequences and meanings of an action or an event in a social situation, and such meaning cannot be given in advance of experience (Denzin, 2012) in seeking a Pragmatism that addresses social justice issues (Denzin, 2012; West, 1995). To research social justice is to adopt an inherently moral aim (Denzin, 2012; West, 1995) where the outputs inherently carry political consequences. In turn, our ideas of social justice are not conceived not from a universalistic ontology, but from a psychologically-driven understanding of actions (Tyler, 1997). However, much of the research on social justice is predicated on organizational work from the industrial revolution (cite), and far less with regard to understanding social justice in the information age (Eubanks, 2011). As we transpose our understanding of the psychology of social justice to the information age, and offer new vistas for IS research, we conceptualize areas of research at the confluence of information, technology, societal systems, and praxis that emanates as just actions and lies beyond the organizational container (Winter et al., 2014). In this section, we draw on the psychology of social justice (Tyler, 1997; cite; cite), which has, in turn, drawn from the etymological, theological, and philosophical roots of organizational justice to elucidate four areas: relative deprivation, distributive justice, procedural justice, and retributive justice, that can be elucidated as platforms for Pragmatic social justice research in IS.

So far I have created the form with the two rich textboxes and also created the two buttons "Open File" and "Split File." I also have read the .txt file in when I click the "Open File" button.

Here is what my form looks like based off of what I have done so far:

My windows form

Here is my full code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace FileExercise
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void OpenFileButton_Click(object sender, EventArgs e)
        {
            StreamReader objstream = new StreamReader("C:\\Users\\Omie\\Desktop\\SocialJustice-SampleText.txt");

            richTextBox1.Text = objstream.ReadLine();
        }

        private void SplitFileButton_Click(object sender, EventArgs e)
        {

        }
    }
}

So I am having trouble with steps 2-5 and was wondering if anyone could provide me an example of how to go about doing it based on what I have worked on already.

Thank you.

EDIT: Updated Code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;

namespace FileExercise
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void OpenFileButton_Click(object sender, EventArgs e)
        {
            string TextFile = File.ReadAllText("C:\\Users\\Omie\\Desktop\\SocialJustice-SampleText.txt", Encoding.UTF8);
            richTextBox1.Text = TextFile;
        }    

        private void SplitFileButton_Click(object sender, EventArgs e)
        {

            string SplitFile = File.ReadAllText("C:\\Users\\Omie\\Desktop\\SocialJustice-SampleText.txt", Encoding.UTF8);
            string[] SplitFileBySentence = Regex.Split(SplitFile, ".");

            foreach (string Period in SplitFileBySentence)
            {
                richTextBox2.Text = Period;
            }
        }
    }
}
Matt
  • 74,352
  • 26
  • 153
  • 180
Omie
  • 5
  • 1
  • 9
  • OK, doesnt look like you got very far at all - so, we dont do coding for you - why not voice what isnt working, and what you thought it should do, and not just ask for us to basically do your homework for you – BugFinder Apr 26 '16 at 08:36
  • @BugFinder Well, I am stuck on splitting the file line by line for starters which is step 2. I'm not sure how to go about doing it. – Omie Apr 26 '16 at 08:42
  • I guess your google is broken http://stackoverflow.com/questions/1508203/best-way-to-split-string-into-lines, http://www.csharp-examples.net/read-text-file/ ... – BugFinder Apr 26 '16 at 09:08
  • @BugFinder Thanks for the examples. I'm still kind of confused though on how to split the .txt file by sentences using windows forms. I updated my code in my main post if you would like to take a look. The Open File button works still and reads in the file correctly, however, the Split File button doesn't do anything when I click it so I'm sure something is wrong with the Split Button click event. – Omie Apr 26 '16 at 09:38
  • well your foreach would mean you end up with only the last sentance in your textbox.. because its overwritten each time – BugFinder Apr 26 '16 at 10:04
  • @BugFinder Oh, I see. Do you know how I'll be able to make the whole text file split by sentences instead? And how to connect my richTextBox2.Text to to the loop? – Omie Apr 26 '16 at 10:30
  • You are splitting it by sentances by the looks of it. what do you expect your textbox to do? how do you expect it to show the sentances? – BugFinder Apr 26 '16 at 10:38
  • @BugFinder Well I eventually want my second textbox (the textbox on the right) to output the .txt file with it split up by each sentence and reversed so it looks like the picture in the image of "What the windows form should look like." But first I would need to split up the .txt file by sentences so it looks formatted before I reverse it and I'm not sure how to code that under my split file button click event. – Omie Apr 26 '16 at 19:04
  • @BugFinder I'm thinking of using a Regex.Split or Split.String method to do this but I'm not sure how. – Omie Apr 26 '16 at 19:13
  • But you already did split it by"." So that will have done that – BugFinder Apr 26 '16 at 21:19
  • @BugFinder Oh ok. The problem is that my textbox2 (right textbox) isn't outputting anything when I click the split file button. So I think there is a problem with how I connected my textbox2 when I said richTextBox2.Text = Period. Do you know how I can connect it properly so it will output the .txt file split by sentences? – Omie Apr 26 '16 at 22:15
  • Debug it, im guessing your last line is empty – BugFinder Apr 26 '16 at 22:22
  • @BugFinder I have but it still doesn't output anything when I click split file. Also, now that I look at my split file code, won't that just get rid of all the periods in the .txt file? I want it to sort of have a break in between the sentences instead. Sorry it's hard to explain, but if you look at the image under "What the Windows form should look like" you should see what I'm talking about. – Omie Apr 26 '16 at 23:08
  • No, it makes an array of strings, yes, without the "." but..currently, you're overwriting each sentance in your text box with the next one - you need to think that over. – BugFinder Apr 27 '16 at 06:51
  • Instead of editing your post to remove the question you should ether delete it or place an answer with the solution you found so other can read the solution and possibly get help from it in the feature. – Matthew Verstraete Apr 27 '16 at 20:34
  • @Matthew Thank you, I'm still new to the site but I clicked the check mark under the answer that helped me and the check mark turned green. Will this mark the post as solved now? – Omie Apr 27 '16 at 21:05
  • @Omie Yes the green check mark means you have accepted that answer as a good one that solver your problem, but the fact you edited your question to remove the question is still bad as anyone with a similar question can no longer know that is what you asked. I would recommend you edit your post to put your question back. – Matthew Verstraete Apr 28 '16 at 01:24
  • Please do not vandalize your post. Note that once you post a question or answer to this site, those posts become part of the collective efforts of others who have also contributed to that content. Posts that are potentially useful to others should not be removed except under extraordinary circumstances. Even if the post is no longer useful to the original author, that information is still beneficial to others who may run into similar problems in the future - this is the underlying philosophy of Stack Exchange. – Matt Apr 30 '16 at 12:24

1 Answers1

0

You can use

string[] readText = File.ReadAllLines("C:\\Users\\Omie\\Desktop\\SocialJustice-SampleText.txt") 

to read all lines into string array.

Then, process the each line in the loop. I hope you can write the code to reverse the line.

Undo
  • 25,519
  • 37
  • 106
  • 129
londondev
  • 231
  • 2
  • 13
  • I see. So this would be going into my Split String button click event, correct? Like this would be the first step into going about splitting the lines? Also, the code in my "Open File" button click is fine right? – Omie Apr 26 '16 at 09:00