mercredi 22 août 2018

Simple C# Code to solve

I have a simple I think C# problem for you but have run out of ideas why it doesn't work. My code simply removes the "*" and should change the "," into a "." . After I run the program and provide the filepath application shows me the number of characters and then closes ( ReadKey works as it should ). What I should do with returns in my conditionals to make my program work?

Here's the code :

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace cutter
{
class Program
{
    static void Main(string[] args)
    {
        ///*
        string test;
        test = Console.ReadLine();
        //test = args[0];
        string test2 = test;
        StreamReader sr = new StreamReader(test,
            Encoding.GetEncoding("Windows-1250"));
        test = sr.ReadLine();
        test.Replace("*", "");
        int dl = test.Length;
        int position;
        int previous;
        int next;
        int result;
        int result2;
        Console.Write(dl);
        Console.ReadKey();
        ///*
        foreach (char c in test)
        {
            if (c.Equals(","))
            {
                position = test.IndexOf(",");
                previous = position - 1;
                next = position + 1;
                if (String.IsNullOrEmpty(previous.ToString()) ||
                    String.IsNullOrEmpty(next.ToString()))
                {
                    return;
                }
                else
                {
                    Int32.TryParse(test.ElementAt(previous).ToString(), out result);
                    Int32.TryParse(test.ElementAt(next).ToString(), out result2);
                    ///*
                    if (result.Equals(0) && result2.Equals(0))
                    {
                        return;
                    }
                    else
                    {
                        test.Replace(test.ElementAt(position).ToString(), ".");
                    }
                }
            }
            else
            {
                return;
            }
            //*/
        }
        //*/
        //test = test.Replace(",", ".");
        sr.Close();
        StreamWriter sw = new StreamWriter(test2);
        sw.Write(test);
        sw.Close();
        //*/
        ///*
        string test3;
        sr = new StreamReader(test2, Encoding.GetEncoding("windows-1250"));
        test3 = sr.ReadLine();
        sr.Close();
        Console.Write(test3);
        Console.ReadKey();

        //*/
        }
    }
}

Aucun commentaire:

Enregistrer un commentaire