Refactoring to remove try/catch - c#

Any ideas on a good way to refactor this so that my code acts the same, but without the whole throwing and catching my own exception?
public Int32 ChooseNextColor(Int32 numColors)
{
int? nextColor = null;
while (nextColor == null)
{
Console.Write("Please enter your next color selection: ");
string input = Console.ReadLine();
try
{
nextColor = Convert.ToInt32(input);
if (nextColor > numColors || nextColor < 0)
throw new ArgumentOutOfRangeException();
}
catch
{
nextColor = null;
Console.WriteLine("Unrecognized input: " + input);
Console.WriteLine("Please enter a value between 0 and " + numColors + ".");
}
}
return (nextColor.Value);
}
EDIT: The try/parse method is exactly what I am looking for.
In response to John's title edit -> I should have posted more information to begin with, and that would have been "getting rid of the try/catch all together is best". So with that in mind, I changed the title.

Try
int nextColor;
input = Console.ReadLine();
while( ! Int32.TryParse( input, out nextColor )
|| nextColor > numColors
|| nextColor < 0 )
{
Console.WriteLine("Unrecognized input: " + input);
Console.WriteLine("Please enter a value between 0 and " + numColors + ".");
input = Console.ReadLine();
}

warning, not tested!
public int ChooseNextColor(int numColors)
{
while (true)
{
Console.Write("Please enter your next color selection: ");
string input = Console.ReadLine();
int color;
if (!int.TryParse(input, out color) || color > numColors || color < 0)
{
Console.WriteLine("Unrecognized input: " + input);
Console.WriteLine("Please enter a value between 0 and " + numColors + ".");
}
else
{
return color;
}
}
}

.NET provides TryParse for just this reason.

You can use Int32.TryParse() or
if (nextColor > numColors || nextColor < 0)
{
Console.WriteLine("Unrecognized input: " + input);
Console.WriteLine("Please enter a value between 0 and " + numColors + ".");
return null;
}

If you want to avoid exception, you should use int.TryParse method instead of Convert.ToInt32().

public Int32 ChooseNextColor(Int32 numColors)
{
var success = false;
while (!success)
{
Console.Write("Please enter your next color selection: ");
int nextColor;
var input = Console.ReadLine();
success = int.TryParse(input, out nextColor);
if (success && nextColor > 0 && nextColor < numColors) return nextColor;
Console.WriteLine("Unrecognized input: " + input);
Console.WriteLine("Please enter a value between 0 and " + numColors + ".");
}
throw new ApplicationException("The thing that should not be.");
}

Related

Sum up all numbers between two numbers

I am a beginner in C# programming and trying to code a method that ask the user to give a start and an end integer number, then sum up all numbers from the start to the end and in case the given start number is greater than the end number, swap the values so that the start number becomes the end number and the end number gets the value of the start number.
This what I have done so far but I'm not getting the right answer when running the app:
private void SumNumbers()
{
int startNumber, endNumber;
Console.WriteLine("\nplease enter a start number: ");
startNumber = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("\nplease enter an end number: ");
endNumber = Convert.ToInt32(Console.ReadLine());
int result = 0;
for (int i=0; i<=startNumber; i=i+1)
{
result = result + i;
Console.WriteLine(i);
}
Console.ReadLine();
Console.WriteLine("The sum of Numbers between " + startNumber + " and " + endNumber + " is: " + result.ToString());
Console.ReadLine();
}
I'm getting this result: The sum of Numbers between 12 and 23 is: 78 when the result actually need to 210.
for (int i=0;i<=startNumber;i=i+1)
You are iterating from 0 to startNumber, when really you want to iterate like startNumber to endNumber.
Try
for (int i = startNumber; i <= endNumber; i = i+1)
Below is the working example.
I also added some logic to handle the checking of the input (whether it is correct or no) so the application doesn't break. This way the user experience is much better.
Here is the live working example: code
private void SumNumbers()
{
int startNumber, endNumber;
Console.WriteLine("\nplease enter a start number: ");
do
{
var input1 = Console.ReadLine();
if (Regex.IsMatch(input1, #"^\d+$"))
{
startNumber = Convert.ToInt32(input1); break;
}
else
{
Console.WriteLine("Input provided is invalid. Please enter a correct number: ");
}
} while (true);
Console.WriteLine("\nplease enter an end number: ");
do
{
var input2 = Console.ReadLine();
if (Regex.IsMatch(input2, #"^\d+$"))
{
endNumber = Convert.ToInt32(input2); break;
}
else
{
Console.WriteLine("Input provided is invalid. Please enter a correct number: ");
}
} while (true);
int min = Math.Min(startNumber, endNumber);
int max = Math.Max(startNumber, endNumber);
int result = 0;
for (int i = min; i <= max; i++)
{
result = result + i;
Console.WriteLine(i);
}
Console.WriteLine("The sum of Numbers between " + min + " and " + max + " is: " + result.ToString());
Console.ReadLine();
}

Number Guessing Game in c#

I'm working on Visual Studio about binary search in c#. My project about the computer find the user's guess number. So, I use tihs code in the main;
int min = 0; // minimum number in the array
int max = 100; // maximum number in the array
int middle = 50; // middle number in the array
int counter = 1;
string name, input;
int guess_number;
Console.WriteLine("Hello, this is a game that finding the number of in your mind. If you want to play so let me know you! ");
name = Console.ReadLine();
Console.WriteLine("Awesome welcome to the game " + name + " guess a number between " + min + " and " + max + " Please! ");
Console.WriteLine("Is your guess " + middle + " ?\nIf it's your guess then write (0) please!\nIf it's too high then write (1) please!\nIf it's too low then write (2) please!");
input = Console.ReadLine();
guess_number = Convert.ToInt32(input);
Console.WriteLine(" You select " + guess_number + " so, ");
do
{
counter += 1;
if (guess_number == 2)
{
min = middle + 1;
}
else if (guess_number == 1)
{
max = middle - 1;
}
else if (guess_number != 1 || guess_number != 2 || guess_number != 0)
{
Console.WriteLine(" Please write 0, 1 or 2 " + name);
}
middle = (min + max) / 2;
Console.WriteLine("Is your guess " + middle + " ?\nIf it's your guess then write (0) please!\nIf it's too high then write (1) please!\nIf it's too low then write (2) please!");
Console.WriteLine(counter + " times I tried for finding your number ");
} while (guess_number != 0);
Console.ReadKey();
However, output always repeat after the user write anything, why the reason about that, is there anyway to get the number?
from your description, I think you need to let user input new value to guess_number variable in the loop end otherwise the loop will not end from the condition guess_number != 0.
do
{
counter += 1;
if (guess_number == 2)
{
min = middle + 1;
}
else if (guess_number == 1)
{
max = middle - 1;
}
else if (guess_number != 1 || guess_number != 2 || guess_number != 0)
{
Console.WriteLine(" Please write 0, 1 or 2 " + name);
}
middle = (min + max) / 2;
Console.WriteLine("Is your guess " + middle + " ?\nIf it's your guess then write (0) please!\nIf it's too high then write (1) please!\nIf it's too low then write (2) please!");
Console.WriteLine(counter + " times I tried for finding your number ");
input = Console.ReadLine(); // let user key in new value.
guess_number = Convert.ToInt32(input);
} while (guess_number != 0);
the last readKey should be inside the while.
do
{
counter += 1;
if (guess_number == 2)
{
min = middle + 1;
}
else if (guess_number == 1)
{
max = middle - 1;
}
else if (guess_number != 1 || guess_number != 2 || guess_number != 0)
{
Console.WriteLine(" Please write 0, 1 or 2 " + name);
}
middle = (min + max) / 2;
Console.WriteLine("Is your guess " + middle + " ?\nIf it's your guess then write (0) please!\nIf it's too high then write (1) please!\nIf it's too low then write (2) please!");
input = Console.ReadLine();
guess_number = Convert.ToInt32(input);
Console.WriteLine(counter + " times I tried for finding your number ");
} while (guess_number != 0);

while adding two number if user enter string how to restrict him

Here i'm writing a simple c#program Addding two number but if user Enter string sting value how to say him Enter only integer value
int x;
int y;
int result;
string Res2;
Console.Write("\n Enter the first number to be added: ");
x = Convert.ToInt32(Console.ReadLine());
Console.Write("\n Enter the second number to be added: ");
y = Convert.ToInt32(Console.ReadLine());
if (x != null && y != null)
{
result = x + y;
Console.Write("\n The sum of two numbers is: " + result);
}
you could put something like
int x;
Console.Write("\n Enter the first number to be added: ");
while(!int.TryParse(Console.ReadLine(),out x))
{
Console.Write("\nPlease, enter a valid number: ");
}
Try below if you want to continue the program after invalid inputs
string x,y;
int a,b;
int result;
bool flag = false;
do{
if(flag)
Console.Write("\n Please enter integer values");
Console.Write("\n Enter the first number to be added: ");
x = Console.ReadLine();
Console.Write("\n Enter the second number to be added: ");
y = Console.ReadLine();
flag = true;
}
while(!int.TryParse(x, out a) || !int.TryParse(y, out b));
if (x != null && y != null)
{
result = a + b;
Console.Write("\n The sum of two numbers is: " + result);
}

How do I use Readline input to write a IF/ELSE statement?

I am trying to write a program that accepts users numerical input then output predetermined messages. My question is after I convert the user input from a string to int, how do I use their input in a IF/ELSE statement.
Here's what I have so far:
string UserInput;
Console.Write ("Enter a random number? ");
UserInput =Console.ReadLine();
int x = Convert.ToInt32 (UserInput);
Console.WriteLine (" You entered: " + UserInput);
int x;
if (x < 0)
{
Console.WriteLine (" Error message: Out of range: Enter a number between 0 and 200");
}
{ else if (x >100)
Console.WriteLine (" You are above average");
}
{ else if (x == 100)
Console.WriteLine (" You are average");
}
{
else if (x < 100)
Console.WriteLine (" Sorry but you are below average");
}
The opening parentheses for the else statements should be on the line after the else, not before it. Also your initial if statement needs to check for > 200 too, according to the message printed.
You can also add error checking by using the Int32.TryParse method like this.
string UserInput;
int x;
Console.Write ("Enter a random number? ");
UserInput =Console.ReadLine();
Console.WriteLine (" You entered: " + UserInput);
if (! Int32.TryParse(UserInput, out x))
{
Console.WriteLine ("Invalid data input");
}
else if ((x < 0) || (x>200))
{
Console.WriteLine (" Error message: Out of range: Enter a number between 0 and 200");
}
else if (x >100)
{
Console.WriteLine (" You are above average");
}
else if (x == 100)
{
Console.WriteLine (" You are average");
}
else if (x < 100)
{
Console.WriteLine (" Sorry but you are below average");
}
Let me suggest one way. This way is cleaner and contains error handling. I think you should pass a C# tutorial. Cheers.
public static void Main(string[] args)
{
Console.Write("Enter a random number? ");
string userInput = Console.ReadLine();
Console.WriteLine(" You entered: " + userInput);
try
{
int input = int.Parse(userInput);
PrintMessage(input);
}
catch (Exception)
{
Console.WriteLine(" Error message: Your input is not a number");
}
}
private static void PrintMessage(int input)
{
if (input < 0)
{
Console.WriteLine(" Error message: Out of range: Enter a number between 0 and 200");
}
else if (input > 100)
{
Console.WriteLine(" You are above average");
}
else if (input == 100)
{
Console.WriteLine(" You are average");
}
else
{
Console.WriteLine(" Sorry but you are below average");
}
}
Did you try to input directly in to the function :
if (Convert.ToInt32 (UserInput) < 0)
{
Console.WriteLine (" Error message: Out of range: Enter a number between 0 and 200");
}
else if Convert.ToInt32 (UserInput) >100) {
Console.WriteLine (" You are above average");
}
else if (Convert.ToInt32 (UserInput) == 100) {
Console.WriteLine (" You are average");
}
else if (Convert.ToInt32 (UserInput) < 100) {
Console.WriteLine (" Sorry but you are below average");
}

C# Console Output Formatting

I'm trying to get a factorial to be displayed as for example (factorial of 5 is 5*4*3*2*1)
I'm using a method for the factorial, but it doesn't accept the line Console.Write(i + " x "); in my code.
Any help would be great.
here is my code.
//this method asks the user to enter a number and returns the factorial of that number
static double Factorial()
{
string number_str;
double factorial = 1;
Console.WriteLine("Please enter number");
number_str = Console.ReadLine();
int num = Convert.ToInt32(number_str);
// If statement is used so when the user inputs 0, INVALID is outputed
if (num <= 0)
{
Console.WriteLine("You have entered an invalid option");
Console.WriteLine("Please enter a number");
number_str = Console.ReadLine();
num = Convert.ToInt32(number_str);
//Console.Clear();
//topmenu();
//number_str = Console.ReadLine();
}
if (num >= 0)
{
while (num != 0)
{
for (int i = num; i >= 1; i--)
{
factorial = factorial * i;
}
Console.Write(i + " x ");
Console.Clear();
Console.WriteLine("factorial of " + number_str.ToString() + " is " + factorial);
factorial = 1;
Console.WriteLine("(please any key to return to main menu)");
Console.ReadKey();
Console.Clear();
topmenu();
}
}
return factorial;
}
Thank you!
The problem is that your for loop isn't using braces, so the scope is just one line.
Try adding braces appropriately:
for (int i = num; i >= 1; i--)
{
factorial = factorial * i;
Console.Write(i.ToString() + " x ");
}
Console.WriteLine("factorial of " + number_str.ToString() + " is " + factorial);
Without the braces, the i variable only exists on the next statement (factorial = factorial * i;), and no longer exists in scope by the time you call Console.Write.
You will likely also want to remove the call to Console.Clear immediately following this Write, or you will not see it.
here's a solution to consider
public static void Main()
{
Console.WriteLine("Please enter number");
int input;
while (!int.TryParse(Console.ReadLine(), out input) || input <= 0)
{
Console.WriteLine("You have enter an invald option");
Console.WriteLine("Please enter number");
}
Console.Write("Factorial of " + input + " is : ");
int output = 1;
for (int i = input; i > 0; i--)
{
Console.Write((i == input) ? i.ToString() : "*" + i);
output *= i;
}
Console.Write(" = " +output);
Console.ReadLine();
}
int.TryParse() will be beneficial for you, so the program doesn't crash if the user inputs a non-integer
also, you may want something besides an integer. Factorials get very large very fast - anything over 16 will return a wrong result.

Categories

Resources