user3759307
user3759307

Reputation: 1

convert string to int error in c#

Hello I am trying to convert String to Integer.

The code below shows a part from where I am trying to convert my string to integer.

if (other.gameObject.CompareTag("PickUp"))
{
    if ( checkpointboolean == false)
    {
        string pickupName = other.ToString(); //other = Pickup4
        //Remove first 6 letters thus remaining with '4'
        string y = pickupName.Substring(6); 
        print(y); // 4 is being printed
        int x = 0;
        int.TryParse(y, out x);
        print (x); // 0 is being printed

I also tried the below code and instead of '0' I am getting the following error:

if (other.gameObject.CompareTag("PickUp"))
{
    if ( checkpointboolean == false)
    {
        //Get Object name ex: Pickup4
        string pickupName = other.ToString();
        //Remove first 6 letters thus remaining with '4'
        string y = pickupName.Substring(6); 
        print(y);
        int x = int.Parse(y);

FormatException: Input string was not in the correct format System.Int32.Parse (System.String s)

Upvotes: 0

Views: 4492

Answers (3)

Igor
Igor

Reputation: 62298

int.TryParse returns a boolean, true if it succeeded and false if not. You need to wrap this in a if block and do something with that logic.

if(int.TryParse(y, out x))
    print (x); // y was able to be converted to an int
else
    // inform the caller that y was not numeric, your conversion to number failed

As far as why your number is not converted I could not say until you post what the string value is.

Upvotes: 1

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186833

First of all, ToString() usually uses for debug purpose so there's no guarantee that

 other.ToString()

will return the expected "Pickup4" in the next version of the software. You, probably, want something like

  int x = other.gameObject.SomeProperty;
  int x = other.SomeOtherProperty;
  int x = other.ComputePickUp();
  ...

If you, however, insist on .ToString() you'd rather not hardcode six letters (the reason is the same: debug information tends to change from version to version), but use regular expressions or something:

  var match = Regex.Match(other.ToString(), "-?[0-9]+");

  if (match.Success) {
    int value; 

    if (int.TryParse(match.Value, out value))
      print(value); 
    else
      print(match.Value + " is not an integer value"); 
  }
  else
    print("Unexpected value: " + other.ToString());

Upvotes: 0

sujith karivelil
sujith karivelil

Reputation: 29036

Your first attempt is the best for this case, that code works fine, The int.TryParse() giving 0 to the out parameter and returns false means the conversion is failed. The input string is not in the correct format/ it cannot be converted to an integer. You can check this by using the return value of the int.TryParse(). For this what you need to do is :-

 if(int.TryParse(y, out x))
     print (x); // 
 else
     print ("Invalid input - Conversion failed");

Upvotes: 0

Related Questions