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 trialFrancisco Mic贸 Juan
1,156 PointsThe CodeChallenge throws me that mistake: System.ArgumentOutOfRangeException: startIndex cannot be larger than length.
Hello the CodeChallenge throws me that message:
System.ArgumentOutOfRangeException: startIndex cannot be larger than length of string. Parameter name: startIndex. See output for stack trace.
I try this code in the console and works it well. I don't know how is the problem.
using System;
namespace Treehouse.CodeChallenges
{
class Program
{
static void Main()
{
string read = Console.ReadLine();
try
{
int time = int.Parse(read);
int count = 0;
while(count <= time){
Console.WriteLine("Enter the number of times to print \"Yay!\": ");
count += 1;
}
}
catch(FormatException)
{
Console.Write("You must enter a whole number.");
}
catch(ArgumentOutOfRangeException)
{
Console.Write("You must enter a whole number.");
}
}
}
}
1 Answer
tobiaskrause
9,160 PointsThis might help you (I will comment it soon just wait). In t the next task you just have to check if time smaller than 0
using System;
namespace Treehouse.CodeChallenges
{
class Program
{
static void Main()
{
int count = 0;
int time = 0;
// obviously we have to ask BEFORE the user writes any information
Console.WriteLine("Enter the number of times to print \"Yay!\": ");
// we get the information
string read = Console.ReadLine();
try
{
// we parse it....-1 so we get the corret amout of times....if time = 1 you dont need the -1
time = Int32.Parse(read) - 1;
}
catch(FormatException)
{
Console.Write("You must enter a whole number.");
}
// after all the validations and the exceptions we can run our loop for the output
while(count <= time){
Console.WriteLine("Yay!");
count += 1;
}
}
}
}
EDIT: of course you can also do in the the try catch...the solution above is jsut my personal preference
try
{
// we parse it....in this case count = 1
time = Int32.Parse(read) ;
while(count <= time){
Console.WriteLine("Yay!");
count += 1;
}
//other stuff
Francisco Mic贸 Juan
1,156 PointsFrancisco Mic贸 Juan
1,156 PointsHi!!
The line: " Console.WriteLine("Enter the number of times to print \"Yay!\": "); " Solve my problem, otherwise If -1 to property time. I have a new problem: I need 5 prints and I get only 4. For that reason I don't use that line.
Now with the same code as before. I have this new problem: "I entered 2.5. I expected "You must enter a whole number.", but got "ust enter a whole number." instead.
I think the console don't understand the line "You must enter a whole number" but I don't know why.
Thanks for your helping.
Francisco Mic贸 Juan
1,156 PointsFrancisco Mic贸 Juan
1,156 PointsNow works! well thanks again!!