0

I'm quite new to C# and had been looking for solutions regarding converting string to integer using Int32.Parse, Int32.TryParse, Convert.Int32 etc. However I couldn't find the right solutions to get near what I wanted. I had found a similar post here: Operator '>=' cannot be applied to operands of type 'string' and 'string' , but still couldn't find the right solutions.

Here's a brief of what I'm trying to code.

  1. Open an Excel file named "compare.xls", read file and write to a text file named "tline.txt"

  2. Then open text file "tline.txt", read text line by line and only write to a text file named "mkmatlab.txt" with conditional statements. Conditional statement: If the number read from the line is 25 <= x <= 50, write it to "mkmatlab.txt" (only write to the file if it's an integer).

An idea of the contents in the Excel file:

A1..|  B1....... |  C1........ | D1........| E1........|      
0.1 |10.2000    |53.6000    |52.7894    |24.9608                                        
0.2 |26.8209    |55.0851    |56.4726    |35.8431                                    
0.3 |10.1009    |56.0314    |56.5013    |27.8922                                    
0.4 |17.7008    |60.0054    |59.7650    |37.8018    

*The symbol .. and | is just to act as spacing here, the Excel file or text file doesn't contain any of these symbols.

Explanations: First row contain A1 to E1. For Column A1 contain 0.1 to 0.4, and so on for column B1 to E1.

I have place comment on the line

if (words1[t] >= 25 && words1[t] <= 50)

where I need help with.

StreamReader fidin = File.OpenText(@"C:\Users\Student\Downloads\compare.xls");
StreamWriter tline = File.CreateText(@"C:\Users\Student\Downloads\tline.txt"); 

string F = fidin.ReadToEnd();
fidin.Close();

string[] words = F.Split(';');
Array.Sort(words);

for (int f = 0; f < words.Length; f++)
     tline.WriteLine(words[f]);
tline.Close();

StreamReader tline1 = File.OpenText(@"C:\Users\Student\Downloads\tline.txt");
StreamWriter fidout = File.CreateText(@"C:\Users\Student\Downloads\mkmatlab.txt");

string T = tline1.ReadToEnd();
            tline1.Close();

            string[] words1 = T.Split(';');
            Array.Sort(words1);

            int counter = 0;
            string line;

            while ((line = tline1.ReadLine()) != null)
            {
                System.Console.WriteLine(line);
                fidout.WriteLine(line);
                counter++;
                Console.ReadKey();
            }

YPCor
  • 43
  • 6
  • 4
    and what `"some string" >= 25` should mean for compiler? where you tried use `int.Parse` ? – Selvin Dec 28 '19 at 15:02
  • But, is this an (old) Excel file, `.xls` (binary) or is this a `.csv` (text) file? If it's actually a CSV file, use an existing parser. if not, how do you read it with `File.OpenText()`? You can use an `ACE.OleDb` Provider for both formats. – Jimi Dec 28 '19 at 15:28
  • @Selvin Sorry I don't quite understand your question and unable to answer your questions right now... I had just started to learn C# through reading documentations (almost 2 weeks) and still couldn't fully understand some of the C# syntax and keywords, but I did tried using double.Parse suggested by Soner Gonul. Now I'm able to use it for the if statement. – YPCor Dec 29 '19 at 15:41
  • @Jimi Yes, it's an old Excel file 97-2003. Thank you for your suggestion and help! – YPCor Dec 29 '19 at 15:42

2 Answers2

5

Your values inside your excel file looks like double instead of int, so, you might want to consider to parse it double instead of int like;

double myDoubleWord = double.Parse(words1[t]);
if (myDoubleWord >= 25 && myDoubleWord <= 50) 

Comparing double with integers is fine with >= and <= operators.

Also be aware the cultural difference, since your values has . as a NumberDecimalSeparator, double.Parse uses your CurrentCulture settings which might not fit for your values.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
  • Hi Soner Gonul, thank you for your help! Now I'm able to use it for the if statement. While I run the compiler, I run into 'unhandled exception input string was not in a correct format'. So I used try catch to fix the error. Since the first line was not integers (A1, B1, C1 D1), FormatException was thrown when reading the first line and only A1 was reconstructed, where B1, C1, D1 remain unchanged. The rest of the integers was not filtered, it stop somehow after FormatException was thrown. Am I coding it the right way? Can you please help to enlighten me..? I had edited the above code. – YPCor Dec 29 '19 at 15:53
  • 1
    @YPCor What is the value of your `words1[t]` when you debug it? And what is your `CurrentCulture` exactly? Put a breakpoint in `double tline2 = double.Parse(words1[t]);` line tell what is the value of `words1[t]` exactly. You can use the QuickWatch window while you debugging for this as well. https://learn.microsoft.com/en-us/visualstudio/debugger/watch-and-quickwatch-windows?view=vs-2019 – Soner Gönül Dec 29 '19 at 16:00
  • Gonul Hi again, thanks for the help! The breakpoint and QuickWatch are really useful! They helped me in understanding what's the output value for each variable in the current executed line. The execution operation stop after FormatException was thrown due to the ReadToEnd, the whole text was pasted as one line at one operation, causing the first Text (A1) reconstructed. Now I had edited the code using ReadLine and placed in a while loop, now it's able to read the text line by line. As for CurrentCulture, I'm not sure if I'm using it right but it return en-US. – YPCor Dec 29 '19 at 18:51
0

Here is a good solution for parsing numbers.

if (int.TryParse(words1[t], out int num))  
{ 
     //we see test if the string is an number and if it is wi output is as a new int.
     if (num >= 25 && num <= 50) //now we can safely do your int comparison without number
     {
         fidout.WriteLine(words1[t]);
     }
}
else //if however our string wasn't capable of being an int we do something else
Console.WriteLine("couldn't parse string ");
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364