Home > OS >  Code doesn't work the way it's supposed to in c#
Code doesn't work the way it's supposed to in c#

Time:12-25

My code doesn't work the way it's supposed to. You should try it to find out what's wrong. Basically, all the strings don't work out the same way. This is my first time using the Split and Starts With method. I would like suggestion on how to improve this situation. Also, how do I split it again without the equal sign, just the content? Here is the current code:

using System;
using System.IO;

namespace C_
{
class Program
{
    static void Main(string[] args)
    {
        string title = "Naruto";
        string description = "Two edgy boys and an annoying girl"; 
        int totalEpisode = 720;
        int currentEpisode = 69;

        string folderPath = "D:/Folder"; // Write any path for folder
        string path = "D:/Folder/text.txt"; // Write the path of above folder   file name

        if(File.Exists(path)) 
        {
            File.Delete(path); // Deletes File to prevent overwriting of same text
        }

        if (!Directory.Exists(folderPath)) // Creates Folder
        {
            Directory.CreateDirectory(folderPath);
        }

        StreamWriter writer = new StreamWriter(path, true); // Writes

        writer.WriteLine(title   "_title = "   title   ";");
        writer.WriteLine(title   "_description = "   description   ";");
        writer.WriteLine(title   "_totalEpisode = "   totalEpisode   ";");
        writer.WriteLine(title   "_currentEpisode = "   currentEpisode   ";");

        writer.Close();

        StreamReader reader = new StreamReader(path, true); // Reads
        string allText = reader.ReadToEnd();
        string[] texts = allText.Split(';');
        string temp;

        for(int i = 0; i < texts.Length; i  )
        {
            temp = texts[i];

            if(temp.StartsWith(title   "_title = "))
            {
                title = temp;
                Console.WriteLine("The title is: "   title);
            }

            if (temp.StartsWith(title   "_description = "))
            {
                description = temp;
                Console.WriteLine("The description is: "   description);
            }

            if (temp.StartsWith(title   "_totalEpisode = "))
            {
                totalEpisode = System.Convert.ToInt32(temp);
                Console.WriteLine("The Total Episodes are: "   totalEpisode);
            }

            if (temp.StartsWith(title   "_currentEpisode = "))
            {
                currentEpisode = System.Convert.ToInt32(temp);
                Console.WriteLine("The Current Episode is: "   currentEpisode);
            }
        }
    reader.Close();
    }
}
}

CodePudding user response:

  1. temp = texts[i].Trim() to remove the whitespace that stays in the string from the split.
  2. When you check if temp.StartsWith(title etc..), it'll fail due to the fact that you reassign title to "Naruto_title = Naruto" in the first if branches, and thus title doesn't stay "Naruto". We can either create a new variable, or use the the temp variable directly as I've done below.
using System;
using System.IO;

namespace C_
{
class Program
{
    static void Main(string[] args)
    {
        string title = "Naruto";
        string description = "Two edgy boys and an annoying girl"; 
        int totalEpisode = 720;
        int currentEpisode = 69;

        string folderPath = "D:/Folder"; // Write any path for folder
        string path = "D:/Folder/text.txt"; // Write the path of above folder   file name

        if(File.Exists(path)) 
        {
            File.Delete(path); // Deletes File to prevent overwriting of same text
        }

        if (!Directory.Exists(folderPath)) // Creates Folder
        {
            Directory.CreateDirectory(folderPath);
        }

        StreamWriter writer = new StreamWriter(path, true); // Writes

        writer.WriteLine(title   "_title = "   title   ";");
        writer.WriteLine(title   "_description = "   description   ";");
        writer.WriteLine(title   "_totalEpisode = "   totalEpisode   ";");
        writer.WriteLine(title   "_currentEpisode = "   currentEpisode   ";");

        writer.Close();

        StreamReader reader = new StreamReader(path, true); // Reads
        string allText = reader.ReadToEnd();
        string[] texts = allText.Split(';');
        string temp;

        for(int i = 0; i < texts.Length; i  )
        {
            temp = texts[i].Trim();

            if(temp.StartsWith(title   "_title = "))
            {
                Console.WriteLine("The title is: "   temp);
            }

            if (temp.StartsWith(title   "_description = "))
            {
                Console.WriteLine("The description is: "   temp);
            }

            if (temp.StartsWith(title   "_totalEpisode = "))
            {
                Console.WriteLine("The Total Episodes are: "   System.Convert.ToInt32(temp.Split("= ")[1]));
            }

            if (temp.StartsWith(title   "_currentEpisode = "))
            {
                Console.WriteLine("The Current Episode is: "   System.Convert.ToInt32(temp.Split("= ")[1]));
            }
        }
    reader.Close();
    }
}
}

CodePudding user response:

it's because u used StartsWith(), which should be start with the same, but in your file, second lines and so on start with \r\n so other ifs never match you should either remove '\r\n\ from your strings or include them in your StartsWith("\r\n" whatever)

---edit as @jh316 suggested, you can use trim() on temp.

                if (temp.TrimStart().StartsWith( title   "_totalEpisode = ")){//youe code}

also in first if() you reassign title, you should rework that too

when you fix these problems, you should do something about your casts too.

totalEpisode = System.Convert.ToInt32(temp);

temp is = "\r\nNaruto_totalEpisode = 720" I assume you only need to convert 720, so you should do som splitting based on '=' then convert the second part

  • Related