Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Parsing string into int

Discussion in 'Scripting' started by Arkkonnen, May 26, 2016.

  1. Arkkonnen

    Arkkonnen

    Joined:
    Apr 27, 2016
    Posts:
    18
    Hello,

    So I've been having an issue with parsing an string into an integer using the function int.Parse(int number).

    I am working in unity script language (because i have to), and i get the following exception:

    FormatException: Input string was not in the correct format
    System.Int32.Parse (System.String s) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System/Int32.cs:629)

    Here's my code:

    Code (JavaScript):
    1. var tempUserData : String [] = loginReader.text.Split("|"[0]);
    2. Debug.Log(tempUserData[0]);
    3. GetComponent(GlobalControl).globalUserId = Int.Parse(tempUserData[0]);
    The thing is I set a breakpoint right after the first line in there and the tempUserData[0] contains a "24" value at that point, so i don't know why is it failing at all.

    Thank you
     
  2. x3r

    x3r

    Joined:
    May 25, 2016
    Posts:
    46
    in c# you can do it that way...not sure about JavaScript

    Code (CSharp):
    1.  string[] variable = new string[2];
    2.             variable[0] = "var1";
    3.  
    4.             int value;
    5.                 Int32.TryParse(variable[0],out value);
     
  3. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,376
    That avoids the exception being thrown, but 'value' will be set to 0 if an exception did occur, meaning they won't get the 24 they expect.

    Thing is, they shouldn't be getting the exception in the first place... parsing "24" should work just fine.

    I don't suspect Int.Parse to have that glaring of a bug, and I don't know anything else about the data that they're parsing, so really with what information I have I can't tell what's wrong.

    OP - can recreate this in a trimmed down project? Say a single scene, single gameobject, single script, and a single text value to be parsed. If you can, upload that and let us take a look.
     
  4. x3r

    x3r

    Joined:
    May 25, 2016
    Posts:
    46
    Are you sure that tempUserData contains only numbers?
    It will throw exception in c# if there no number,or as lordofduct wrote it will return 0 if its alphanumeric
     
  5. Arkkonnen

    Arkkonnen

    Joined:
    Apr 27, 2016
    Posts:
    18
    That's what i thought. TryParse would make the exception not to be thrown, but would not fix the problem itself.

    Here's a screenshot of the debugger at the point it runs that line.

    upload_2016-5-26_20-53-52.png

    Edit: As you can see at the bottom of the image, the value of tempUserData[0] is set to "24". The exception raises when TryParse is called.
     

    Attached Files:

  6. Dave-Carlile

    Dave-Carlile

    Joined:
    Sep 16, 2012
    Posts:
    967
    I'm not a UnityScript user, but shouldn't

    Code (csharp):
    1. var tempUserData :String[]= loginReader.text.Split("|"[0]);
    be

    Code (csharp):
    1. var tempUserData : String[] = loginReader.text.Split("|")[0];
    ?

    The key being the placement of the bracket. "|"[0] is just going to give you the first character of the string "|" I believe - at least that's what it does in C# - so you're just parsing the pipe. Moving it outside of the call to Split will give you the first array element which I believe is what you're looking for.

    Based on that, I'm not sure how you would see the 24 in tempUserData with the code you've shown us here.
     
  7. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,376
    You know you could do:

    Code (csharp):
    1.  
    2. loginReader.text.Split('|');
    3.  
    single quotes denote 'char'.
     
    Dave-Carlile likes this.
  8. Arkkonnen

    Arkkonnen

    Joined:
    Apr 27, 2016
    Posts:
    18
    As I've posted upside, I do see the 24, so I assumed ther code was just ok. I must add it's not my code, it's someone else's code and i have to work on it. I would've chosen C# if I could.
     
  9. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    No - Split takes a character. The indexer gives you back the first character in the given string. Your replacement would tell you that you can't cast from String to String[] because the first index in Split is not a string array.
     
    Dave-Carlile likes this.
  10. Dave-Carlile

    Dave-Carlile

    Joined:
    Sep 16, 2012
    Posts:
    967
    Oh, yeah, that's what that is for - I was thinking OP was trying to grab the first element after the split. Nothing to see here, please move along.
     
  11. Arkkonnen

    Arkkonnen

    Joined:
    Apr 27, 2016
    Posts:
    18
    So I've created a new neat project with a single scene and a single cube with a single script.

    Code (JavaScript):
    1. #pragma strict
    2.  
    3. function Start () {
    4.  
    5.     var text : String;
    6.  
    7.     text = "24|gomartis@ub.edu";
    8.  
    9.     var tempUserData : String[] = text.Split("|"[0]);
    10.     var value : int;
    11.     int.TryParse(tempUserData[0], value);
    12.     Debug.Log(tempUserData[0]);
    13.     Debug.Log(value);
    14.  
    15.  
    16. }
    17.  
    18. function Update () {
    19.  
    20. }
    I get no exceptions and two beautiful 24's in the console.

    Hence, there's something wrong with the input?
     
  12. Dave-Carlile

    Dave-Carlile

    Joined:
    Sep 16, 2012
    Posts:
    967
    Sure seems like something odd about the original input. In the original project try setting tempUserData[0] = "24" and see what you get. If it works then there is apparently something about the data that the debugger isn't displaying properly.

    Spaces maybe? Or something non-printable? Try logging the length of it.
     
  13. Arkkonnen

    Arkkonnen

    Joined:
    Apr 27, 2016
    Posts:
    18
    I've checked the project again and debugged. That is the loginReader.text variable that I am splitting right before the split does happen. I just don't see any format issue with that string.
    upload_2016-5-26_21-13-39.png
     
  14. x3r

    x3r

    Joined:
    May 25, 2016
    Posts:
    46
    Just in case...what next line returns? Debug.Log(tempUserData[0]);
     
  15. Arkkonnen

    Arkkonnen

    Joined:
    Apr 27, 2016
    Posts:
    18
    "24"
     
  16. Dave-Carlile

    Dave-Carlile

    Joined:
    Sep 16, 2012
    Posts:
    967
    This. And try something like var id = tempUserData[0]; Log the length of id.

    There are also some culture settings involved in the parsing. Generally this would only be an issue with commands/periods for thousands separators and decimal points and such which shouldn't apply here - but we're grasping at this point. Have you changed anything with the default cultural settings in the other project?
     
  17. Arkkonnen

    Arkkonnen

    Joined:
    Apr 27, 2016
    Posts:
    18
    I guess I'll never know, but I just closed Unity, restarted my PC, and it was working. Something to do with cache memory? No idea.
     
  18. Dave-Carlile

    Dave-Carlile

    Joined:
    Sep 16, 2012
    Posts:
    967
    Ouch. Kind of maddening isn't it?
     
  19. Bux777

    Bux777

    Joined:
    May 2, 2017
    Posts:
    7
    I'm having the same problem ... it only bugs out every now n then ... same code .. nothing changed .. it works fine 99%.
    The cache memory theory is interesting.
     
  20. N00MKRAD

    N00MKRAD

    Joined:
    Dec 31, 2013
    Posts:
    210
    Kinda off-topic, but you should switch to C# as soon as possible, JS is not officially supported anymore.