简体   繁体   English

System.FormatException: '输入字符串的格式不正确。' 在 C# 黄皮书教科书中

[英]System.FormatException: 'Input string was not in a correct format.' in C# Yellowbook textbook

I am following an example in C# Programming Yellow Book by Rob Miles.我正在关注 Rob Miles 在 C# 编程黄书中的一个例子。 I copied and pasted this example from the e-book.我从电子书中复制并粘贴了这个例子。

  • Here is the error:这是错误:

     width = double.Parse(widthString); <- this gives the error of: System.FormatException: 'Input string was not in a correct format.' <- Why???
  • Full code:完整代码:

     double width, height, woodLength, glassArea; string widthString, heightString; widthString = Console.ReadLine(); width = double.Parse(widthString); <- this gives the error of: System.FormatException: 'Input string was not in a correct format.' <- Why??? heightString = Console.ReadLine(); height = double.Parse(heightString); woodLength = 2 * (width + height) * 3.25; glassArea = 2 * (width * height); Console.WriteLine("The length of the wood is " + woodLength + " feet"); Console.WriteLine("The area of the glass is " + glassArea + " square metres"); Console.ReadLine(); }

The code reads a string from the console.该代码从控制台读取一个字符串。 The string is expected to be a valid double (ie 1.2, 4.3, etc) but you seem to be entering something that is not a valid double.该字符串应为有效的双精度值(即 1.2、4.3 等),但您似乎输入了无效的双精度值。

I'd recommend adding some prompts to instruct the user what they're expected to enter.我建议添加一些提示来指导用户他们期望输入的内容。 For example:例如:

    double width, height, woodLength, glassArea; string widthString, heightString;

    Console.Write("Enter the width (as a decimal number): "); // <- Add this
    widthString = Console.ReadLine(); 
    width = double.Parse(widthString); <- this gives the error of: System.FormatException: 'Input string was not in a correct format.' <- Why??? 

    Console.Write("Enter the height (as a decimal number): "); // <- And this
    heightString = Console.ReadLine(); 
    height = double.Parse(heightString);

    woodLength = 2 * (width + height) * 3.25;

    glassArea = 2 * (width * height);

    Console.WriteLine("The length of the wood is " + woodLength + " feet"); 
    Console.WriteLine("The area of the glass is " + glassArea + " square metres");

    Console.ReadLine();
}

Cheers, Ian干杯,伊恩

If your input string is empty or non numeric(or too large for the scope of a double var) if will throw that error.如果您的输入字符串为空或非数字(或对于双变量范围而言太大),则 if 将引发该错误。 In these situations I prefer to use TryParse.在这些情况下,我更喜欢使用 TryParse。

double width, height, woodLength, glassArea; string widthString, heightString;

widthString = Console.ReadLine();
heightString = Console.ReadLine();

double.TryParse(widthString, out width);
double.TryParse(heightString, out height);

woodLength = 2 * (width + height) * 3.25;

glassArea = 2 * (width * height);

Console.WriteLine("The length of the wood is " + woodLength + " feet");
Console.WriteLine("The area of the glass is " + glassArea + " square metres");

Console.ReadLine();

this will output an updated value for height / width if the input is able to be parsed.. If not it will default to whatever it was set to before the parsing attempt.如果输入能够被解析,这将输出高度/宽度的更新值。如果不是,它将默认为解析尝试之前设置的任何值。

As mentioned in the comments below (thanks for the feedback) you would also want to validate the input.正如下面的评论中提到的(感谢您的反馈),您还需要验证输入。 You could do that in many ways.你可以通过多种方式做到这一点。 Below is an example on how to do so:以下是有关如何执行此操作的示例:

        double width, height, woodLength, glassArea; string widthString, heightString;

        width = GetValidDblInput("Please enter the width:");
        height = GetValidDblInput("Please enter the height:");
        woodLength = 2 * (width + height) * 3.25;

        glassArea = 2 * (width * height);

        Console.WriteLine("The length of the wood is " + woodLength + " feet");
        Console.WriteLine("The area of the glass is " + glassArea + " square metres");

        Console.ReadLine();

    }

    static double GetValidDblInput(string inputRequest)
    {
        bool IsValid = false;
        double val = 0;
        while(!IsValid)
        {
            Console.WriteLine(inputRequest);
            if(double.TryParse(Console.ReadLine(), out val))
            {
                IsValid = true;
            }
            else
            {
                Console.WriteLine("Invalid input. Please enter a valid value (e.g. 1, 3.25, 400)");
            }
        }
        return val;
    }

Make sure that you use the correct decimal separator when entering the value on the console.在控制台上输入值时,请确保使用正确的小数点分隔符。 If you don't specify a specific format when using parsing methods, the framework usually falls back to the regional format of the operation system the program is running on.如果在使用解析方法时没有指定特定格式,框架通常会回退到程序运行所在操作系统的区域格式。

If you want to use a specific format, you can do so by using one of the following overloads:如果要使用特定格式,可以使用以下重载之一:

  • Parse(String, NumberStyles)
  • Parse(String, IFormatProvider)
  • Parse(String, NumberStyles, IFormatProvider)

Check out the docs here .此处查看文档。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 System.FormatException:输入字符串的格式不正确。 c# - System.FormatException: Input string was not in a correct format. c# C# 错误 System.FormatException: &#39;输入字符串的格式不正确。&#39; 在数字输入上 - C# Error System.FormatException: 'Input string was not in a correct format.' on number input c# 如何修复:“未处理的异常:System.FormatException:输入字符串的格式不正确。” - c# How Can I Fix: “Unhandled Exception: System.FormatException: Input string was not in a correct format.” C# Excel combobox 中的空行(导致错误“System.FormatException:输入字符串格式不正确。”) - C# Excel empty rows in combobox (causing error “System.FormatException:Input string was not in a correct format.”) System.FormatException: &#39;输入字符串的格式不正确。&#39; - System.FormatException: 'Input string was not in a correct format.' System.FormatException: &#39;输入字符串的格式不正确。&#39; 数据网格 - System.FormatException: 'Input string was not in a correct format.' data grid System.FormatException:&#39;输入字符串的格式不正确。 - System.FormatException: 'The input string does not have the correct format.' System.FormatException: '输入字符串的格式不正确。' WinForms - System.FormatException: 'Input string was not in a correct format.' WinForms C#System.FormatException:输入字符串的格式不正确 - C# System.FormatException: Input string was not in a correct format C#当我使用TryParse时,为什么会收到“未处理的异常:System.FormatException:输入字符串的格式不正确。” - C# Why I get “Unhandled Exception: System.FormatException: Input string was not in a correct format.” when I use TryParse?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM