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.
-
did you look at the values of line[0], line[1], line[2] in the debugger? What were they?Hogan– Hogan2015年11月21日 01:59:31 +00:00Commented Nov 21, 2015 at 1:59
-
line[0] = "3" line[1] = "5" line[2] = "10" the first time. file[0] = "" the second timeRaihli– Raihli2015年11月21日 02:03:05 +00:00Commented Nov 21, 2015 at 2:03
-
No, "3 5 10\n2 7 150円"Raihli– Raihli2015年11月21日 02:10:19 +00:00Commented 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.Mark Hall– Mark Hall2015年11月21日 02:20:39 +00:00Commented 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 ?Raihli– Raihli2015年11月21日 02:26:05 +00:00Commented Nov 21, 2015 at 2:26
1 Answer 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]));
}
answered Nov 21, 2015 at 2:28
TLJ
4,9854 gold badges34 silver badges47 bronze badges
Sign up to request clarification or add additional context in comments.
3 Comments
TLJ
@3per thanks for the edit. I totally forget to change line to tokens :-)
Raihli
Works perfectly ! Thank you very much. So the ReadLine take the \n like a line, alone ?
TLJ
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 variablelang-cs