Read grades from file, incorrect format for converting string to double error












0














I am reading a string from file and I try to convert it to double, but it says:




1.55



Unhandled Exception: System.FormatException: Input string was not in a
correct format. at System.Number.ParseDouble(String value,
NumberStyles options, NumberFormatInfo numfmt) at
System.Convert.ToDouble(String value) at ConsoleApp6.Logic.read()
in C:UserssashkSourceReposConsoleApp6ConsoleApp6Class2.cs:line
123 at ConsoleApp6.Program.Main(String args) in
C:UserssashkSourceReposConsoleApp6ConsoleApp6Program.cs:line 19




It prints the first grade but it blocks on the second double, I don't know the reason why. Here is the code:



public void read(){
string textes = File.ReadAllLines(@"C:UserssashkSourceReposConsoleApp6ConsoleApp6save.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]);
// }
}


The content of the file is this :



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


It's seemingly formatted correctly but I don't know why it crashes? I checked for extra spaces but there aren't.










share|improve this question
























  • You should debug your app. In debug mode, it will stop when the exception is thrown. Then it's easy to see where and with which input data it fails.
    – Christoph Lütjen
    Nov 23 '18 at 15:01










  • your question is different from your code
    – preciousbetine
    Nov 23 '18 at 15:07










  • no it doesn't 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++; } } converts to the string to double and puts it in array called grades which is later assigned to the student?
    – Gopnik McSlav Mangal Minja
    Nov 23 '18 at 16:42










  • Change Console.WriteLine(newValue); to Console.WriteLine($"'{newValue}'"); and your issue will be more obvious... Also, without wanting to give the game away, try Console.WriteLine(System.Text.RegularExpressions.Regex.IsMatch("", @"^[a-zA-Zа-яА-Я ]+$"));
    – JohnLBevan
    Nov 23 '18 at 16:56






  • 1




    … and you may want to check if your test "if string contains other characters than a-z it must be a double" is ideal. After this you could use double.TryParse that allows you to easily log errors.
    – Christoph Lütjen
    Nov 23 '18 at 17:05
















0














I am reading a string from file and I try to convert it to double, but it says:




1.55



Unhandled Exception: System.FormatException: Input string was not in a
correct format. at System.Number.ParseDouble(String value,
NumberStyles options, NumberFormatInfo numfmt) at
System.Convert.ToDouble(String value) at ConsoleApp6.Logic.read()
in C:UserssashkSourceReposConsoleApp6ConsoleApp6Class2.cs:line
123 at ConsoleApp6.Program.Main(String args) in
C:UserssashkSourceReposConsoleApp6ConsoleApp6Program.cs:line 19




It prints the first grade but it blocks on the second double, I don't know the reason why. Here is the code:



public void read(){
string textes = File.ReadAllLines(@"C:UserssashkSourceReposConsoleApp6ConsoleApp6save.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]);
// }
}


The content of the file is this :



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


It's seemingly formatted correctly but I don't know why it crashes? I checked for extra spaces but there aren't.










share|improve this question
























  • You should debug your app. In debug mode, it will stop when the exception is thrown. Then it's easy to see where and with which input data it fails.
    – Christoph Lütjen
    Nov 23 '18 at 15:01










  • your question is different from your code
    – preciousbetine
    Nov 23 '18 at 15:07










  • no it doesn't 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++; } } converts to the string to double and puts it in array called grades which is later assigned to the student?
    – Gopnik McSlav Mangal Minja
    Nov 23 '18 at 16:42










  • Change Console.WriteLine(newValue); to Console.WriteLine($"'{newValue}'"); and your issue will be more obvious... Also, without wanting to give the game away, try Console.WriteLine(System.Text.RegularExpressions.Regex.IsMatch("", @"^[a-zA-Zа-яА-Я ]+$"));
    – JohnLBevan
    Nov 23 '18 at 16:56






  • 1




    … and you may want to check if your test "if string contains other characters than a-z it must be a double" is ideal. After this you could use double.TryParse that allows you to easily log errors.
    – Christoph Lütjen
    Nov 23 '18 at 17:05














0












0








0







I am reading a string from file and I try to convert it to double, but it says:




1.55



Unhandled Exception: System.FormatException: Input string was not in a
correct format. at System.Number.ParseDouble(String value,
NumberStyles options, NumberFormatInfo numfmt) at
System.Convert.ToDouble(String value) at ConsoleApp6.Logic.read()
in C:UserssashkSourceReposConsoleApp6ConsoleApp6Class2.cs:line
123 at ConsoleApp6.Program.Main(String args) in
C:UserssashkSourceReposConsoleApp6ConsoleApp6Program.cs:line 19




It prints the first grade but it blocks on the second double, I don't know the reason why. Here is the code:



public void read(){
string textes = File.ReadAllLines(@"C:UserssashkSourceReposConsoleApp6ConsoleApp6save.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]);
// }
}


The content of the file is this :



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


It's seemingly formatted correctly but I don't know why it crashes? I checked for extra spaces but there aren't.










share|improve this question















I am reading a string from file and I try to convert it to double, but it says:




1.55



Unhandled Exception: System.FormatException: Input string was not in a
correct format. at System.Number.ParseDouble(String value,
NumberStyles options, NumberFormatInfo numfmt) at
System.Convert.ToDouble(String value) at ConsoleApp6.Logic.read()
in C:UserssashkSourceReposConsoleApp6ConsoleApp6Class2.cs:line
123 at ConsoleApp6.Program.Main(String args) in
C:UserssashkSourceReposConsoleApp6ConsoleApp6Program.cs:line 19




It prints the first grade but it blocks on the second double, I don't know the reason why. Here is the code:



public void read(){
string textes = File.ReadAllLines(@"C:UserssashkSourceReposConsoleApp6ConsoleApp6save.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]);
// }
}


The content of the file is this :



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


It's seemingly formatted correctly but I don't know why it crashes? I checked for extra spaces but there aren't.







c# file-io console-application






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 16:41









kit

1,1063716




1,1063716










asked Nov 23 '18 at 14:47









Gopnik McSlav Mangal Minja

327




327












  • You should debug your app. In debug mode, it will stop when the exception is thrown. Then it's easy to see where and with which input data it fails.
    – Christoph Lütjen
    Nov 23 '18 at 15:01










  • your question is different from your code
    – preciousbetine
    Nov 23 '18 at 15:07










  • no it doesn't 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++; } } converts to the string to double and puts it in array called grades which is later assigned to the student?
    – Gopnik McSlav Mangal Minja
    Nov 23 '18 at 16:42










  • Change Console.WriteLine(newValue); to Console.WriteLine($"'{newValue}'"); and your issue will be more obvious... Also, without wanting to give the game away, try Console.WriteLine(System.Text.RegularExpressions.Regex.IsMatch("", @"^[a-zA-Zа-яА-Я ]+$"));
    – JohnLBevan
    Nov 23 '18 at 16:56






  • 1




    … and you may want to check if your test "if string contains other characters than a-z it must be a double" is ideal. After this you could use double.TryParse that allows you to easily log errors.
    – Christoph Lütjen
    Nov 23 '18 at 17:05


















  • You should debug your app. In debug mode, it will stop when the exception is thrown. Then it's easy to see where and with which input data it fails.
    – Christoph Lütjen
    Nov 23 '18 at 15:01










  • your question is different from your code
    – preciousbetine
    Nov 23 '18 at 15:07










  • no it doesn't 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++; } } converts to the string to double and puts it in array called grades which is later assigned to the student?
    – Gopnik McSlav Mangal Minja
    Nov 23 '18 at 16:42










  • Change Console.WriteLine(newValue); to Console.WriteLine($"'{newValue}'"); and your issue will be more obvious... Also, without wanting to give the game away, try Console.WriteLine(System.Text.RegularExpressions.Regex.IsMatch("", @"^[a-zA-Zа-яА-Я ]+$"));
    – JohnLBevan
    Nov 23 '18 at 16:56






  • 1




    … and you may want to check if your test "if string contains other characters than a-z it must be a double" is ideal. After this you could use double.TryParse that allows you to easily log errors.
    – Christoph Lütjen
    Nov 23 '18 at 17:05
















You should debug your app. In debug mode, it will stop when the exception is thrown. Then it's easy to see where and with which input data it fails.
– Christoph Lütjen
Nov 23 '18 at 15:01




You should debug your app. In debug mode, it will stop when the exception is thrown. Then it's easy to see where and with which input data it fails.
– Christoph Lütjen
Nov 23 '18 at 15:01












your question is different from your code
– preciousbetine
Nov 23 '18 at 15:07




your question is different from your code
– preciousbetine
Nov 23 '18 at 15:07












no it doesn't 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++; } } converts to the string to double and puts it in array called grades which is later assigned to the student?
– Gopnik McSlav Mangal Minja
Nov 23 '18 at 16:42




no it doesn't 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++; } } converts to the string to double and puts it in array called grades which is later assigned to the student?
– Gopnik McSlav Mangal Minja
Nov 23 '18 at 16:42












Change Console.WriteLine(newValue); to Console.WriteLine($"'{newValue}'"); and your issue will be more obvious... Also, without wanting to give the game away, try Console.WriteLine(System.Text.RegularExpressions.Regex.IsMatch("", @"^[a-zA-Zа-яА-Я ]+$"));
– JohnLBevan
Nov 23 '18 at 16:56




Change Console.WriteLine(newValue); to Console.WriteLine($"'{newValue}'"); and your issue will be more obvious... Also, without wanting to give the game away, try Console.WriteLine(System.Text.RegularExpressions.Regex.IsMatch("", @"^[a-zA-Zа-яА-Я ]+$"));
– JohnLBevan
Nov 23 '18 at 16:56




1




1




… and you may want to check if your test "if string contains other characters than a-z it must be a double" is ideal. After this you could use double.TryParse that allows you to easily log errors.
– Christoph Lütjen
Nov 23 '18 at 17:05




… and you may want to check if your test "if string contains other characters than a-z it must be a double" is ideal. After this you could use double.TryParse that allows you to easily log errors.
– Christoph Lütjen
Nov 23 '18 at 17:05












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53448782%2fread-grades-from-file-incorrect-format-for-converting-string-to-double-error%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53448782%2fread-grades-from-file-incorrect-format-for-converting-string-to-double-error%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

Calculate evaluation metrics using cross_val_predict sklearn

Insert data from modal to MySQL (multiple modal on website)