我正在从文件中读取一个字符串,然后尝试将其转换为double,但是它说:
1.55
未处理的异常:System.FormatException:输入字符串不在 正确的格式。在System.Number.ParseDouble(字符串值, NumberStyles选项,NumberFormatInfo numfmt)位于 ConsoleApp6.Logic.read()上的System.Convert.ToDouble(String value) 在C:\ Users \ sashk \ Source \ Repos \ ConsoleApp6 \ ConsoleApp6 \ Class2.cs:line中 123在ConsoleApp6.Program.Main(String [] args)中 C:\ Users \ sashk \ Source \ Repos \ ConsoleApp6 \ ConsoleApp6 \ Program.cs:第19行
它打印一年级,但在第二个双关卡时,我不知道原因。这是代码:
public void read(){
string[] textes = File.ReadAllLines(@"C:\Users\sashk\Source\Repos\ConsoleApp6\ConsoleApp6\save.txt", Encoding.Default);
double[] gread = new double[40];
var excluded = new[] { "Name:", "FacultyNumber:", "Grades:", "AverageGrade:" };
var dot = "," ;
List<string> texts = new List<string>();
int i = 0;
int z = 0;
foreach (string text in textes){
string[] words = textes[i].Split(' ');
for (int j = 0; j < words.Length; j++){
if (!excluded.Contains(words[j])){
texts.Add(words[j]);
// Console.WriteLine(words[j]);
}
}
for (int j = 3; j < texts.Count; j++){
if (!System.Text.RegularExpressions.Regex.IsMatch(texts[j], @"^[a-zA-Zа-яА-Я ]+$")){
var newValue = texts[j].Replace(',', '.');
Console.WriteLine(newValue);
gread[z] = Convert.ToDouble(newValue);
z++;
}
}
addStudent(texts[1], Convert.ToInt32(texts[2]), gread);
i++;
}
//for (int j = 0; j < texts.Count; j++)
// {
// Console.WriteLine(texts[j]);
// }
}
文件的内容是这样的:
Name: as FacultyNumber: 4 Grades: 1,55 5,55 9,55 AverageGrade: 5
Name: asd FacultyNumber: 2 Grades: 1,23 4,56 7,89 AverageGrade: 4,56
Name: ad FacultyNumber: 3 Grades: 2,55 4,55 7,55 AverageGrade: 4,33
Name: ddd FacultyNumber: 1 Grades: 2,55 5,55 9,55 AverageGrade: 4
看似格式正确,但我不知道为什么它崩溃?我检查了多余的空间,但没有。