Parsing string into int

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:

var tempUserData : String [] = loginReader.text.Split("|"[0]);
Debug.Log(tempUserData[0]);
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

in c# you can do it that way…not sure about JavaScript

 string[] variable = new string[2];
            variable[0] = "var1";

            int value;
                Int32.TryParse(variable[0],out value);

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.

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

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.

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.

I’m not a UnityScript user, but shouldn’t

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

be

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.

You know you could do:

loginReader.text.Split('|');

single quotes denote ‘char’.

1 Like

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.

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.

1 Like

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.

So I’ve created a new neat project with a single scene and a single cube with a single script.

#pragma strict

function Start () {

    var text : String;

    text = "24|gomartis@ub.edu";

    var tempUserData : String[] = text.Split("|"[0]);
    var value : int;
    int.TryParse(tempUserData[0], value);
    Debug.Log(tempUserData[0]);
    Debug.Log(value);


}

function Update () {

}

I get no exceptions and two beautiful 24’s in the console.

Hence, there’s something wrong with the input?

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.

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.

Just in case…what next line returns? Debug.Log(tempUserData[0]);

“24”

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?

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.

Ouch. Kind of maddening isn’t it?

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.

Kinda off-topic, but you should switch to C# as soon as possible, JS is not officially supported anymore.