如何处理不正确格式的EXCEPTION INPUT STRING [英] How to handle the EXCEPTION INPUT STRING NOT in CORRECT FORMAT

查看:148
本文介绍了如何处理不正确格式的EXCEPTION INPUT STRING的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的代码中,当我检查我的复选框时,旁边的文本框启用,所以当我使用数组在文本框中一起添加值时,它会一直给我一个异常警告,说输入字符串格式不正确





我尝试过:



 const double Price_Friedrice = 200; 
const double Price_Jollofrice = 200;
const double Price_Spags = 200;
const double Price_Beans = 200;
const double Price_Fries = 600;
const double Price_Salad = 100;
const double Price_Chicken = 300;
const double Price_Plantain = 50;
const double Price_Egg = 50;
const double Price_Fish = 100;
const double Price_Cocorice = 300;
const double Price_Turkey = 500;
const double Price_Moin = 50;
const double Price_breadandegg = 200;
const double Price_ewagee = 200;
const double Price_Whiterice = 200;
const double Price_Ofada = 200;
const double Price_Gizzard = 50;
const double Price_Bole = 500;
const double Price_Burritos = 500;
const double Price_Smallchops = 500;
const double Price_Pizza = 600;
const double Price_Sharwama = 600;
const double Price_Suya = 400;
const double Price_Waffles = 500;
const double Price_Water = 50;
const double Price_Exotic = 400;
const double Price_Coke = 120;
const double Price_Fanta = 120;
const double Price_Pepsi = 120;
const double Price_Mirinda = 100;
const double Price_Slush = 200;
const double Price_Juice = 250;
const double Price_Sobo = 100;
const double Price_Eba = 100;
const double Price_Poundoyam = 100;
const double Price_Semo = 50;
const double Price_Egusi = 0;
const double Price_Vegetable = 0;
const double Price_Ewedu = 0;
const double Price_Okra = 0;
const double Price_WhiteSoup = 0;
const double Tax_rate = 20;

双iTax,iSubtotal,iTotal;



 private void Total_btn_Click(object sender,EventArgs e)
{
double [] cost_of_Item = new double [41];
cost_of_Item [0] = Convert.ToDouble(friedricetxt.Text)* Price_Friedrice;
cost_of_Item [1] = Convert.ToDouble(jolloftxt.Text)* Price_Jollofrice;
cost_of_Item [2] = Convert.ToDouble(spagstxt.Text)* Price_Spags;
cost_of_Item [3] = Convert.ToDouble(Beanstxt.Text)* Price_Beans;
cost_of_Item [4] = Convert.ToDouble(Fries_txt.Text)* Price_Fries;
cost_of_Item [5] = Convert.ToDouble(salad_txt.Text)* Price_Salad;
cost_of_Item [6] = Convert.ToDouble(chicken_txt.Text)* Price_Chicken;
cost_of_Item [7] = Convert.ToDouble(plantain_txt.Text)* Price_Plantain;
cost_of_Item [8] = Convert.ToDouble(Egg_txt.Text)* Price_Egg;
cost_of_Item [9] = Convert.ToDouble(fish_txt.Text)* Price_Fish;
cost_of_Item [10] = Convert.ToDouble(Cocorice_txt.Text)* Price_Cocorice;
cost_of_Item [11] = Convert.ToDouble(Turkey_txt.Text)* Price_Turkey;
cost_of_Item [12] = Convert.ToDouble(moin_txt.Text)* Price_Moin;
cost_of_Item [13] = Convert.ToDouble(Breadegg_txt.Text)* Price_breadandegg;
cost_of_Item [14] = Convert.ToDouble(whiterice_txt.Text)* Price_Whiterice;
cost_of_Item [15] = Convert.ToDouble(Ofada_txt.Text)* Price_Ofada;
cost_of_Item [16] = Convert.ToDouble(Gizzard_txt.Text)* Price_Gizzard;
cost_of_Item [17] = Convert.ToDouble(Bole_txt.Text)* Price_Bole;
cost_of_Item [18] = Convert.ToDouble(Burritos_txt.Text)* Price_Burritos;
cost_of_Item [19] = Convert.ToDouble(Smallchops_txt.Text)* Price_Smallchops;
cost_of_Item [20] = Convert.ToDouble(pizza_txt.Text)* Price_Pizza;
cost_of_Item [21] = Convert.ToDouble(Sharwama_txt.Text)* Price_Sharwama;
cost_of_Item [22] = Convert.ToDouble(Suya_txt.Text)* Price_Suya;
cost_of_Item [23] = Convert.ToDouble(Waffles_txt.Text)* Price_Waffles;
cost_of_Item [24] = Convert.ToDouble(Water_txt.Text)* Price_Water;
cost_of_Item [25] = Convert.ToDouble(Exotic_txt.Text)* Price_Exotic;
cost_of_Item [26] = Convert.ToDouble(Coke_txt.Text)* Price_Coke;
cost_of_Item [27] = Convert.ToDouble(Fanta_txt.Text)* Price_Fanta;
cost_of_Item [28] = Convert.ToDouble(Pepsi_txt.Text)* Price_Pepsi;
cost_of_Item [29] = Convert.ToDouble(Mirinda_txt.Text)* Price_Mirinda;
cost_of_Item [30] = Convert.ToDouble(Slush_txt.Text)* Price_Slush;
cost_of_Item [31] = Convert.ToDouble(Fuitjuicetxt.Text)* Price_Juice;
cost_of_Item [32] = Convert.ToDouble(Sobo_txt.Text)* Price_Sobo;
cost_of_Item [33] = Convert.ToDouble(eba_txt.Text)* Price_Eba;
cost_of_Item [34] = Convert.ToDouble(poundo_txt.Text)* Price_Poundoyam;
cost_of_Item [35] = Convert.ToDouble(Semo_txt.Text)* Price_Semo;
cost_of_Item [36] = Convert.ToDouble(egusi_txt.Text)* Price_Egusi;
cost_of_Item [37] = Convert.ToDouble(Vegetable_txt.Text)* Price_Vegetable;
cost_of_Item [38] = Convert.ToDouble(Ewedu_txt.Text)* Price_Ewedu;
cost_of_Item [39] = Convert.ToDouble(Okra_txt.Text)* Price_Okra;
cost_of_Item [40] = Convert.ToDouble(Whitesoup_txt.Text)* Price_WhiteSoup;

双倍成本,变化;
if(method_payment.Text ==Cash)
{
iSubtotal = cost_of_Item [0] + cost_of_Item [1] + cost_of_Item [2] + cost_of_Item [3] + cost_of_Item [4] + cost_of_Item [5] +
cost_of_Item [6] + cost_of_Item [7] + cost_of_Item [8] + cost_of_Item [9] + cost_of_Item [10] + cost_of_Item [11] + cost_of_Item [12]
+ cost_of_Item [13] + cost_of_Item [14] + cost_of_Item [15] + cost_of_Item [16] + cost_of_Item [17] + cost_of_Item [18] + cost_of_Item [19]
+ cost_of_Item [20] + cost_of_Item [21] + cost_of_Item [ 22] + cost_of_Item [23] + cost_of_Item [24] + cost_of_Item [25] + cost_of_Item [26]
+ cost_of_Item [27] + cost_of_Item [28] + cost_of_Item [29] + cost_of_Item [30] + cost_of_Item [31 ] + cost_of_Item [32] + cost_of_Item [33]
+ cost_of_Item [34] + cost_of_Item [35] + cost_of_Item [36] + cost_of_Item [37] + cost_of_Item [38] + cost_of_Item [39] + cost_of_Item [40] ;

Subtotlbl.Text = Convert.ToString(iSubtotal);
iTax =(iSubtotal * Tax_rate)/ 100;
Taxlbl.Text = Convert.ToString(iTax);
iTotal =(iSubtotal + iTax);
Totallbl.Text = Convert.ToString(iTotal);
Change = Convert.ToDouble(method_payment.Text);
cost = Change - (iTotal);

解决方案

表示您正在尝试转换不是有效数字格式的字符串。

使用 Double.TryParse方法(系统) [ ^ ]



ex:

 double friedrice; 
if(double.TryParse(friedricetxt.Text,out value))
{
cost_of_Item [0] = friedrice * Price_Friedrice;
}
其他
{
//显示输入非数值格式的错误消息
}


in my code when i check my checkboxes the textbox beside it enables so when i add the values together in the textboxes using an array it keeps on giving me an exception warning saying input string format not correct


What I have tried:

const double Price_Friedrice = 200;
        const double Price_Jollofrice = 200;
        const double Price_Spags = 200;
        const double Price_Beans = 200;
        const double Price_Fries = 600;
        const double Price_Salad = 100;
        const double Price_Chicken= 300;
        const double Price_Plantain = 50;
        const double Price_Egg = 50;
        const double Price_Fish = 100;
        const double Price_Cocorice = 300;
        const double Price_Turkey = 500;
        const double Price_Moin = 50;
        const double Price_breadandegg = 200;
        const double Price_ewagee = 200;
        const double Price_Whiterice = 200;
        const double Price_Ofada = 200;
        const double Price_Gizzard = 50;
        const double Price_Bole = 500;
        const double Price_Burritos = 500;
        const double Price_Smallchops = 500;
        const double Price_Pizza = 600;
        const double Price_Sharwama = 600;
        const double Price_Suya = 400;
        const double Price_Waffles = 500;
        const double Price_Water = 50;
        const double Price_Exotic = 400;
        const double Price_Coke = 120;
        const double Price_Fanta = 120;
        const double Price_Pepsi = 120;
        const double Price_Mirinda = 100;
        const double Price_Slush = 200;
        const double Price_Juice = 250;
        const double Price_Sobo = 100;
        const double Price_Eba = 100;
        const double Price_Poundoyam = 100;
        const double Price_Semo = 50;
        const double Price_Egusi = 0;
        const double Price_Vegetable = 0;
        const double Price_Ewedu = 0;
        const double Price_Okra = 0;
        const double Price_WhiteSoup = 0;
        const double Tax_rate = 20;

        double  iTax, iSubtotal, iTotal;


private void Total_btn_Click(object sender, EventArgs e)
       {
           double[] cost_of_Item = new double[41];
           cost_of_Item[0] = Convert.ToDouble(friedricetxt.Text) * Price_Friedrice;
           cost_of_Item[1] = Convert.ToDouble(jolloftxt.Text) * Price_Jollofrice;
           cost_of_Item[2] = Convert.ToDouble(spagstxt.Text) * Price_Spags;
           cost_of_Item[3] = Convert.ToDouble(Beanstxt.Text) * Price_Beans;
           cost_of_Item[4] = Convert.ToDouble(Fries_txt.Text) * Price_Fries;
           cost_of_Item[5] = Convert.ToDouble(salad_txt.Text) * Price_Salad;
           cost_of_Item[6] = Convert.ToDouble(chicken_txt.Text) * Price_Chicken;
           cost_of_Item[7] = Convert.ToDouble(plantain_txt.Text) * Price_Plantain;
           cost_of_Item[8] = Convert.ToDouble(Egg_txt.Text) * Price_Egg;
           cost_of_Item[9] = Convert.ToDouble(fish_txt.Text) * Price_Fish;
           cost_of_Item[10] = Convert.ToDouble(Cocorice_txt.Text) * Price_Cocorice;
           cost_of_Item[11] = Convert.ToDouble(Turkey_txt.Text) * Price_Turkey;
           cost_of_Item[12] = Convert.ToDouble(moin_txt.Text) * Price_Moin;
           cost_of_Item[13] = Convert.ToDouble(Breadegg_txt.Text) * Price_breadandegg;
           cost_of_Item[14] = Convert.ToDouble(whiterice_txt.Text) * Price_Whiterice;
           cost_of_Item[15] = Convert.ToDouble(Ofada_txt.Text) * Price_Ofada;
           cost_of_Item[16] = Convert.ToDouble(Gizzard_txt.Text) * Price_Gizzard;
           cost_of_Item[17] = Convert.ToDouble(Bole_txt.Text) * Price_Bole;
           cost_of_Item[18] = Convert.ToDouble(Burritos_txt.Text) * Price_Burritos;
           cost_of_Item[19] = Convert.ToDouble(Smallchops_txt.Text) * Price_Smallchops;
           cost_of_Item[20] = Convert.ToDouble(pizza_txt.Text) * Price_Pizza;
           cost_of_Item[21] = Convert.ToDouble(Sharwama_txt.Text) * Price_Sharwama;
           cost_of_Item[22] = Convert.ToDouble(Suya_txt.Text) * Price_Suya;
           cost_of_Item[23] = Convert.ToDouble(Waffles_txt.Text) * Price_Waffles;
           cost_of_Item[24] = Convert.ToDouble(Water_txt.Text) * Price_Water;
           cost_of_Item[25] = Convert.ToDouble(Exotic_txt.Text) * Price_Exotic;
           cost_of_Item[26] = Convert.ToDouble(Coke_txt.Text) * Price_Coke;
           cost_of_Item[27] = Convert.ToDouble(Fanta_txt.Text) * Price_Fanta;
           cost_of_Item[28] = Convert.ToDouble(Pepsi_txt.Text) * Price_Pepsi;
           cost_of_Item[29] = Convert.ToDouble(Mirinda_txt.Text) * Price_Mirinda;
           cost_of_Item[30] = Convert.ToDouble(Slush_txt.Text) * Price_Slush;
           cost_of_Item[31] = Convert.ToDouble(Fuitjuicetxt.Text) * Price_Juice;
           cost_of_Item[32] = Convert.ToDouble(Sobo_txt.Text) * Price_Sobo;
           cost_of_Item[33] = Convert.ToDouble(eba_txt.Text) * Price_Eba;
           cost_of_Item[34] = Convert.ToDouble(poundo_txt.Text) * Price_Poundoyam;
           cost_of_Item[35] = Convert.ToDouble(Semo_txt.Text) * Price_Semo;
           cost_of_Item[36] = Convert.ToDouble(egusi_txt.Text) * Price_Egusi;
           cost_of_Item[37] = Convert.ToDouble(Vegetable_txt.Text) * Price_Vegetable;
           cost_of_Item[38] = Convert.ToDouble(Ewedu_txt.Text) * Price_Ewedu;
           cost_of_Item[39] = Convert.ToDouble(Okra_txt.Text) * Price_Okra;
           cost_of_Item[40] = Convert.ToDouble(Whitesoup_txt.Text) * Price_WhiteSoup;

           double cost, Change;
           if (method_payment.Text == "Cash")
           {
               iSubtotal = cost_of_Item[0] + cost_of_Item[1] + cost_of_Item[2] + cost_of_Item[3] + cost_of_Item[4] + cost_of_Item[5] +
               cost_of_Item[6] + cost_of_Item[7] + cost_of_Item[8] + cost_of_Item[9] + cost_of_Item[10] + cost_of_Item[11] + cost_of_Item[12]
               + cost_of_Item[13] + cost_of_Item[14] + cost_of_Item[15] + cost_of_Item[16] + cost_of_Item[17] + cost_of_Item[18] + cost_of_Item[19]
               + cost_of_Item[20] + cost_of_Item[21] + cost_of_Item[22] + cost_of_Item[23] + cost_of_Item[24] + cost_of_Item[25] + cost_of_Item[26]
               + cost_of_Item[27] + cost_of_Item[28] + cost_of_Item[29] + cost_of_Item[30] + cost_of_Item[31] + cost_of_Item[32] + cost_of_Item[33]
               + cost_of_Item[34] + cost_of_Item[35] + cost_of_Item[36] + cost_of_Item[37] + cost_of_Item[38] + cost_of_Item[39] + cost_of_Item[40];

               Subtotlbl.Text = Convert.ToString(iSubtotal);
               iTax = (iSubtotal * Tax_rate)/100;
               Taxlbl.Text = Convert.ToString(iTax);
               iTotal = (iSubtotal + iTax);
               Totallbl.Text = Convert.ToString(iTotal);
               Change = Convert.ToDouble(method_payment.Text);
               cost = Change - (iTotal);

解决方案

Means you are trying to convert a string which is not in a valid numeric format.
use Double.TryParse Method (System)[^]

ex:

double friedrice;
            if( double.TryParse(friedricetxt.Text, out value))
            {
                 cost_of_Item[0] = friedrice * Price_Friedrice;
            }
            else
            {
                 // show the error message  for entering  non numeric value format
            }


这篇关于如何处理不正确格式的EXCEPTION INPUT STRING的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆