Checking user input for invalid characters
i have a console application that asks user for their sales figures over a month. I have made the program reject entries below zero and ask the user to enter their sales figures again. But now I want the same thing to happen when the user enters a letter or any other character that is not a number The code i currently have is:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace FIRST_ACTUAL_PROJECT
{
class Program
{
static void Main(string[] args)
{
FileStream fin; // this is declaring that you are using a filestream.
String s;
int LineNum = 0;
double seventy_percent_value;
double thirty_percent_value;
const i开发者_如何学编程nt max_num_of_items = 20; // this means that there will always be a maximum of 20 sales figures because there is a maximum of 20 customers
double[] sales_figures = new double[max_num_of_items]; // this is the array for the sales figures
string[] customer = new string[max_num_of_items]; // this is the array for the customers
double[] licence_fee_in_percent = new double[max_num_of_items]; // this is the array for the licence fee
double[] fee_payable = new double[max_num_of_items]; // array for the fees payable in pounds.
const double MIN_SALES_FIGURE = 0;
try
{
fin = new FileStream("customer list.txt", FileMode.Open);// this is opening the file.
}
catch (IOException exc)
{
Console.WriteLine(exc.Message + "cannot find file!"); // error message if it does'nt find the file or something went wrong.
Console.ReadLine();
return;
}
StreamReader fstr_in = new StreamReader(fin); // this is telling the streamreader which file to read.
try
{
while ((s = fstr_in.ReadLine()) != null) // this is reading the file until the end.
{
Console.WriteLine(s);
customer[LineNum] = s.Split(',')[0];
licence_fee_in_percent[LineNum] = double.Parse(s.Split(',')[1]);
LineNum = LineNum + 1;
}
}
catch (IOException exc)
{
Console.WriteLine(exc.Message);
}
for (int CustPos = 0; CustPos < LineNum; CustPos = CustPos + 1) // this determines what the loop does.
{
Console.Write("enter sales figures for" + customer[CustPos] + " "); // this asks the user to enter the sales figures
sales_figures[CustPos] = Double.Parse(Console.ReadLine()); // this is user's input is read in and stored.
while (sales_figures[CustPos] < MIN_SALES_FIGURE) // this is if the user enters a number below zero.
{
Console.WriteLine("");
Console.WriteLine("entry invalid");
Console.WriteLine("");
Console.WriteLine("enter sales figures for" + customer[CustPos] + " ");
sales_figures[CustPos] = Double.Parse(Console.ReadLine());
}
Console.WriteLine(" ");
fee_payable[CustPos] = (sales_figures[CustPos] / 100.0) * licence_fee_in_percent[CustPos];
Console.WriteLine(customer[CustPos] + " ----------- " + fee_payable[CustPos]);
Console.WriteLine("Licence fee to be paid in GBP is :" + fee_payable[CustPos]); //this section displays the cust name, sales figure 70/30.
seventy_percent_value = ((fee_payable[CustPos] / 10.0) * 7);
Console.WriteLine("70 percent of this fee is" + seventy_percent_value);
thirty_percent_value = ((fee_payable[CustPos] / 10.0) * 3);
Console.WriteLine("30 percent of this fee is" + thirty_percent_value);
Console.WriteLine(" ");
}
}
Console.WriteLine("Customer name" + "\t" + "sales" + "\t" + "fee paid" + "\t" + "70% value" + "\t" + "30% value" + "\t");
for (int DisplayPos = 0; DisplayPos < LineNum; DisplayPos = DisplayPos + 1)
{
seventy_percent_value = ((fee_payable[DisplayPos] / 10.0) * 7);
thirty_percent_value = ((fee_payable[DisplayPos] / 10.0) * 3);
Console.WriteLine(customer[DisplayPos] + "\t" + sales_figures[DisplayPos] + "\t" + fee_payable[DisplayPos] + "\t\t" + seventy_percent_value + " \t\t" + thirty_percent_value + "\t");
}
Console.WriteLine(" ");
Console.WriteLine("Press enter to finish");
Console.ReadLine();
}
}
}
Instead of using Double.Parse
, use Double.TryParse
- that will return whether or not the number was parsed successfully.
Better yet, use Decimal.TryParse
- you shouldn't be using double
for currency values.
Additional recommendations:
- Fix the namespace to one which complies with .NET naming conventions
- You've got one enormous method - break it up into several methods which each performs one small task
- Consider using
List<T>
instead of an array - that way you don't need to pre-allocate everything - You don't happend to have any particular naming convention for your variables; it would be good to be consistent
- Generally prefer to declare local variables at the point of first use, rather than declaring everything at the top of the method
- Use
using
statements to close your resources such as streams and readers. (Currently I don't think you close anything, ever.)
I think would probably use regular expressions to check the input is in the correct form.
use TryParse instead of Parse:
Replace:
sales_figures[CustPos] = Double.Parse(Console.ReadLine()); // this is user's input is read in and stored.
With:
bool isValidDouble = Double.TryParse(Console.ReadLine(), out sales_figures[CustPos] );
then check for isValidDouble later.
精彩评论