Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialGiannis Kataras
1,279 PointsStuck in last challenge
Bummer! System.ArgumentNullException: Value cannot be null. Parameter name: String at System.Number.StringToNumber (System.String str, System.Globalization.NumberStyles options, System.Number+NumberBuffer& number, System.Globalization.NumberFormatInfo info, System.Boolean parseDecimal) [0x00054] in /builddir/build/BUILD/mono-4.8.1/mcs/class/referencesource/mscorlib/system/number.cs:1074 at System.Number.ParseInt32 (System.String s, System.Globalization.NumberStyles style, System.Globalization.NumberFormatInfo info) [0x00014] in /builddir/build/BUILD/mono-4.8.1/mcs/class/referencesource/mscorlib/system/number.cs:745 at System.Int32.Parse (System.String s) [0x00000] in /builddir/build/BUILD/mono-4.8.1/mcs/class/referencesource/mscorlib/system/int32.cs:120 at Treehouse.CodeChallenges.Program.Main () [0x00010] in <127eb559eb9944a6ad1a80cc101f8094>:0 at MonoTester.Run () [0x00208] in /workdir/MonoTester.cs:140
I get this error code what is the fault in my code?
using System;
namespace Treehouse.CodeChallenges
{
class Program
{
static void Main()
{
while(true)
{
Console.Write("Enter the number of times to print \"Yay!\": ");
string input=Console.ReadLine();
try
{ var number = int.Parse(input);
while (number!=0)
{ Console.WriteLine("Yay");
number-=1;}
if (number==0)
{break;}
}
catch (FormatException)
{ Console.WriteLine("you must enter a whole number");
continue;}
}
}
}
}
3 Answers
David Tomko
7,744 PointsSorry in advance for the sloppy written code...
using System;
namespace Treehouse.CodeChallenges
{
class Program
{
static void Main()
{
try{
Console.Write("Enter the number of times to print \"Yay!\": ");
string numberOfTimes = Console.ReadLine();
int i = 0;
if(int.Parse(numberOfTimes) >= 0){
while(i < int.Parse(numberOfTimes))
{
Console.Write("Yay");
i++;
}
}
else{
Console.WriteLine("You must enter a positive number.");
}
}
catch(FormatException){
Console.WriteLine("You must enter a whole number.");
}
}
}
}
Giannis Kataras
1,279 PointsThanks for the answer, but this part of code its a bit tricky to understand can you explain me :
while(i < int.Parse(numberOfTimes))
{
Console.Write("Yay");
i++;
}
Thanks a lot!
David Tomko
7,744 PointsOkay so this is saying while i (which is just a counter for the while loop) is less than numberOfTimes(but here we are Parsing the data type from a string to an int so we can compare it with our other int value aka the counter 'i' because you can't compare mismatching data types) print "YAY" then add 1 to i. I hope this helped if not lmk.
Giannis Kataras
1,279 PointsThank you david!