So far I have written code to check if a file name exists and outputs an error if no file.
//does the file exist?
if (!System.IO.File.Exists(fileName))
{
MessageBox.Show("Error: No such file.");
return;
}
Now I am suppose to check to see if the file contains integers, and if there are no integers in the file, then I need to output a warning saying that the file does not contain integers. I do not know where to start when it comes to this code. Is there a specific command that just automatically checks for integers?
So far I have written this code to convert the strings to integers from a file (that i created that contains integers)
// convert each string into an integer and store in "eachInt[]"
string fileContents = System.IO.File.ReadAllText(fileName);
string[] eachString = fileContents.Split(new char[] { ' ', '\t', '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
int[] eachInt = new int[eachString.Length];
for (int i = 0; i < eachString.Length; i++)
eachInt[i] = int.Parse(eachString[i]);
You can use:
if(fileContents.Any(char.IsDigit))
Since you have already read the file contents in a string.
If you don't want to load all the file in memory then you can do
foreach (var line in File.ReadLines("filePath"))
{
if (line.Any(char.IsDigit))
{
//number found.
return;//return found etc
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.