convert.ToInt32
-
line = "==XXXT08746=05/01/09 15:13 112 4 IT 0:01'22 0 #2 ";
string Duration = line.Substring(64, 9).Replace("#","[#]");
int s = Convert.ToInt32(Duration.Substring(1, 2));
int s = Convert.ToInt32(Duration.Substring(1, 2));
int y = Convert.ToInt32(Duration.Substring(3, 2));
int l = Convert.ToInt32(Duration.Substring(6, 2));
int NDura = s + y + l;---->>>> Error System.FormatException: Input string was not in a correct format. So what should I do with this ?
-
line = "==XXXT08746=05/01/09 15:13 112 4 IT 0:01'22 0 #2 ";
string Duration = line.Substring(64, 9).Replace("#","[#]");
int s = Convert.ToInt32(Duration.Substring(1, 2));
int s = Convert.ToInt32(Duration.Substring(1, 2));
int y = Convert.ToInt32(Duration.Substring(3, 2));
int l = Convert.ToInt32(Duration.Substring(6, 2));
int NDura = s + y + l;---->>>> Error System.FormatException: Input string was not in a correct format. So what should I do with this ?
This is very silly mistake which u can trace it out.
yueru wrote:
int s = Convert.ToInt32(Duration.Substring(1, 2)); int s = Convert.ToInt32(Duration.Substring(1, 2)); int y = Convert.ToInt32(Duration.Substring(3, 2)); int l = Convert.ToInt32(Duration.Substring(6, 2));
In above four lines, check wat is the value of (Duration.Substring()); it may be null or may not be in format to convert to integer.
-
line = "==XXXT08746=05/01/09 15:13 112 4 IT 0:01'22 0 #2 ";
string Duration = line.Substring(64, 9).Replace("#","[#]");
int s = Convert.ToInt32(Duration.Substring(1, 2));
int s = Convert.ToInt32(Duration.Substring(1, 2));
int y = Convert.ToInt32(Duration.Substring(3, 2));
int l = Convert.ToInt32(Duration.Substring(6, 2));
int NDura = s + y + l;---->>>> Error System.FormatException: Input string was not in a correct format. So what should I do with this ?
yueru wrote:
int s = Convert.ToInt32(Duration.Substring(1, 2)); int s = Convert.ToInt32(Duration.Substring(1, 2));
I don't understand how the code has got compiled! :doh:
yueru wrote:
So what should I do with this ?
Submit it to Microsoft critical bugs department :-D
yueru wrote:
Error System.FormatException: Input string was not in a correct format.
Finally what is the value you are expecting in the
Duration
string ? Do the debug and verify if it has the desired value or not...
"Don't worry if it doesn't work right. If everything did, you'd be out of a job." (Mosher's Law of Software Engineering)
-
line = "==XXXT08746=05/01/09 15:13 112 4 IT 0:01'22 0 #2 ";
string Duration = line.Substring(64, 9).Replace("#","[#]");
int s = Convert.ToInt32(Duration.Substring(1, 2));
int s = Convert.ToInt32(Duration.Substring(1, 2));
int y = Convert.ToInt32(Duration.Substring(3, 2));
int l = Convert.ToInt32(Duration.Substring(6, 2));
int NDura = s + y + l;---->>>> Error System.FormatException: Input string was not in a correct format. So what should I do with this ?
The quick answer:
int s = Convert.ToInt32(Duration.Substring(1, 1));
Why? Try changing your code to this:
String line = "==XXXT08746=05/01/09 15:13 112 4 IT 0:01'22 0 #2 ";
string Duration = line.Substring(64, 9).Replace("#", "[#]");
string a = Duration.Substring(1, 2);
string b = Duration.Substring(3, 2);
string c = Duration.Substring(6, 2);
int s = Convert.ToInt32(a);
int y = Convert.ToInt32(b);
int l = Convert.ToInt32(c);
int NDura = s + y + l;You will discover that duration equals " 0:01'22 " and so the string you are trying to parse is "0:" - thats why you keep getting the error. Also this line:
string Duration = line.Substring(64, 9).Replace("#", "[#]");
Is kind of wierd as the Replace has no effect - as I mentioned Duration == " 0:01'22 ". No "#" there...
-
This is very silly mistake which u can trace it out.
yueru wrote:
int s = Convert.ToInt32(Duration.Substring(1, 2)); int s = Convert.ToInt32(Duration.Substring(1, 2)); int y = Convert.ToInt32(Duration.Substring(3, 2)); int l = Convert.ToInt32(Duration.Substring(6, 2));
In above four lines, check wat is the value of (Duration.Substring()); it may be null or may not be in format to convert to integer.
Int32.Parse(String)
battulga
-
The quick answer:
int s = Convert.ToInt32(Duration.Substring(1, 1));
Why? Try changing your code to this:
String line = "==XXXT08746=05/01/09 15:13 112 4 IT 0:01'22 0 #2 ";
string Duration = line.Substring(64, 9).Replace("#", "[#]");
string a = Duration.Substring(1, 2);
string b = Duration.Substring(3, 2);
string c = Duration.Substring(6, 2);
int s = Convert.ToInt32(a);
int y = Convert.ToInt32(b);
int l = Convert.ToInt32(c);
int NDura = s + y + l;You will discover that duration equals " 0:01'22 " and so the string you are trying to parse is "0:" - thats why you keep getting the error. Also this line:
string Duration = line.Substring(64, 9).Replace("#", "[#]");
Is kind of wierd as the Replace has no effect - as I mentioned Duration == " 0:01'22 ". No "#" there...
Thank you I 've got it even if I've made alot of mistake like u said now is my code
string Duration = line.Replace("'", "[']");
string LineOO = line.Replace("#", "[#]");
string Date = line.Substring(12, 8);
if (line.Substring(11, 1) == "=")
{
string Time = line.Substring(21, 5);
string Linein = line.Substring(27, 10);
string Lineout = LineOO.Substring(38, 1);if ((string.Compare(Lineout, "0") > 0 || string.Compare(Lineout, "0") == 0) && (string.Compare(Lineout, "9") < 0 || string.Compare(Lineout, "9") == 0)) { //int g = line.Length; //string Number = line.Substring(40, g - 40); string Number = line.Substring(40, 20); string Status = line.Substring(61, 3); string NDuration = Duration.Substring(64, 10); // string a = Duration.Substring(1, 2); string b = NDuration.Substring(3, 2); string c = NDuration.Substring(6, 2); // int s = Convert.ToInt32(a); int y = Convert.ToInt32(b); int l = Convert.ToInt32(c); int NDura = y + l; // reject a but if a has value 00 it mean that I can't use it right? string Cost = line.Substring(75, 3); string Detail = line.Substring(79, 2); string Network = line.Substring(82, 8);
:) and it work Thx
-
Thank you I 've got it even if I've made alot of mistake like u said now is my code
string Duration = line.Replace("'", "[']");
string LineOO = line.Replace("#", "[#]");
string Date = line.Substring(12, 8);
if (line.Substring(11, 1) == "=")
{
string Time = line.Substring(21, 5);
string Linein = line.Substring(27, 10);
string Lineout = LineOO.Substring(38, 1);if ((string.Compare(Lineout, "0") > 0 || string.Compare(Lineout, "0") == 0) && (string.Compare(Lineout, "9") < 0 || string.Compare(Lineout, "9") == 0)) { //int g = line.Length; //string Number = line.Substring(40, g - 40); string Number = line.Substring(40, 20); string Status = line.Substring(61, 3); string NDuration = Duration.Substring(64, 10); // string a = Duration.Substring(1, 2); string b = NDuration.Substring(3, 2); string c = NDuration.Substring(6, 2); // int s = Convert.ToInt32(a); int y = Convert.ToInt32(b); int l = Convert.ToInt32(c); int NDura = y + l; // reject a but if a has value 00 it mean that I can't use it right? string Cost = line.Substring(75, 3); string Detail = line.Substring(79, 2); string Network = line.Substring(82, 8);
:) and it work Thx
There is still room for some improvement. ;) Instead of getting a one character string, you can get a character. You can change this:
if (line.Substring(11, 1) == "=")
to:
if (line[11] == '=')
You can use the >= operator instead of doing both > and == comparisons. You can change this:
if ((string.Compare(Lineout, "0") > 0 || string.Compare(Lineout, "0") == 0) && (string.Compare(Lineout, "9") < 0 || string.Compare(Lineout, "9") == 0))
to:
if (string.Compare(Lineout, "0") >= 0 && string.Compare(Lineout, "9") <= 0)
However, you can use a char instead of a one character string there too:
char Lineout = Line00[38];
if (Lineout >= '0' && Lineout <= '9')or simply:
if (Char.IsDigit(Line00, 38))
Despite everything, the person most likely to be fooling you next is yourself.
-
There is still room for some improvement. ;) Instead of getting a one character string, you can get a character. You can change this:
if (line.Substring(11, 1) == "=")
to:
if (line[11] == '=')
You can use the >= operator instead of doing both > and == comparisons. You can change this:
if ((string.Compare(Lineout, "0") > 0 || string.Compare(Lineout, "0") == 0) && (string.Compare(Lineout, "9") < 0 || string.Compare(Lineout, "9") == 0))
to:
if (string.Compare(Lineout, "0") >= 0 && string.Compare(Lineout, "9") <= 0)
However, you can use a char instead of a one character string there too:
char Lineout = Line00[38];
if (Lineout >= '0' && Lineout <= '9')or simply:
if (Char.IsDigit(Line00, 38))
Despite everything, the person most likely to be fooling you next is yourself.
-
Wowwww It's so short and simply but I've never knoww. :) thx very much Code is ok but I 've just want to know how to decrease time to do it coz I have 10,100 data and it took a long time.
I while back I wrote this method that you can use to replace Int32.Parse, and that is about ten times faster. That could help to speed up it a bit.
public static int ParseInt32(string text) {
long value = 0;
long sign = 1;
bool first = true;
foreach (char c in text) {
if (c >= '0' && c <= '9') {
value = value * 10 + c - '0';
} else if (c == '-' && first) {
sign = -1;
} else {
throw new FormatException();
}
first = false;
}
value *= sign;
if (value < int.MinValue || value > int.MaxValue) throw new OverflowException();
return (int)value;
}Despite everything, the person most likely to be fooling you next is yourself.