1

I've just start C# (with CodeEval) but i've got a little problem with a StringArray when i execute my programme.

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

namespace CodeEval
{
    class Program
    {
        public static void      fizzBuzz(int x, int y, int n)
        {
            List<string>        list = new List<string>();
            int                 i = 1;

            while (i <= n)
            {
                if (i % x == 0 && i % y != 0)
                    list.Add("F");
                else if (i % x != 0 && i % y == 0)
                    list.Add("B");
                else if (i % x == 0 && i % y == 0)
                    list.Add("FB");
                else
                    list.Add(i.ToString());
                i++;
            }
            Console.WriteLine(string.Join(" ", list));
        }

        static int              Main(string[] args)
        {
            try
            {
                using (StreamReader file = new StreamReader("test1.txt"))
                {
                    while (!file.EndOfStream)
                    {
                        string[] line = file.ReadLine().Split(' ');
                        fizzBuzz(Convert.ToInt32(line[0]), Convert.ToInt32(line[1]), Convert.ToInt32(line[2]));
                    }
                }
            }
            catch (Exception e)
            {
                Console.Write("Le fichier ne peut pas être lu: ");
                Console.WriteLine(e.Message);
            }
            Console.ReadLine();
            return (0);
        }
    }
}

My error is in this line

fizzBuzz(Convert.ToInt32(line[0]), Convert.ToInt32(line[1]), Convert.ToInt32(line[2]));

The test1.txt file have this inside :

3 5 10
2 7 15

When i execute the programme, it works for the first line but then he tried the second line : "The input string format is incorrect"

How can it work the first time but not the second ? Need help to understand my problem.

Thanks everybody.

5
  • did you look at the values of line[0], line[1], line[2] in the debugger? What were they? Commented Nov 21, 2015 at 1:59
  • line[0] = "3" line[1] = "5" line[2] = "10" the first time. file[0] = "" the second time Commented Nov 21, 2015 at 2:03
  • No, "3 5 10\n2 7 15\0" Commented Nov 21, 2015 at 2:10
  • I think you will find that the /n is causing your problem. You need to change your split to remove it. Commented Nov 21, 2015 at 2:20
  • But the ReadLine() just take "3 5 10" and after "2 7 15". I thinks it's the problem too now, but why ? Commented Nov 21, 2015 at 2:26

1 Answer 1

1

What about do a little guard in your code to block unwanted white spaces.

while (!file.EndOfStream)
{
   String line = file.ReadLine();
   if (String.IsNullOrWhiteSpace(line))
       continue;
   string[] tokens = line.Split(new char[] {' '},StringSplitOptions.RemoveEmptyEntries);
   fizzBuzz(Convert.ToInt32(tokens[0]), Convert.ToInt32(tokens[1]), Convert.ToInt32(tokens[2]));
}
Sign up to request clarification or add additional context in comments.

3 Comments

@3per thanks for the edit. I totally forget to change line to tokens :-)
Works perfectly ! Thank you very much. So the ReadLine take the \n like a line, alone ?
I don't know how your input file looks like. But if you have \n\n it would read twice. (I guess) debug it and you'll see what's in the line variable

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.