Jump to content
xisto Community
8bit

..what Happens If

Recommended Posts

What happens if I assigned a string to an integer? Because I used a try statement for it and the catch didn't get invoked! Does it just assign a to 0!?

51835[/snapback]


I suppose you're using Java.

 

You can easily assign a String to an Integer by using parseInt.

 

String s = "777";

int x = Integer.parseInt(s);

Share this post


Link to post
Share on other sites

I suppose you're using Java.
You can easily assign a String to an Integer by using parseInt.

String s = "777";
int x = Integer.parseInt(s);

Ok, let's think about this. This is the C/C++ programming forum. What 8bit just said makes perfect sense in C++ (exception handling), so it's probably not Java ;) Just a thought leeiah.

What happens if I assigned a string to an integer? Because I used a try statement for it and the catch didn't get invoked! Does it just assign a to 0!?

Could you please state what compiler you're using (version also) and perhaps the source code? Normally that would not even compile with what I'm using.

Share this post


Link to post
Share on other sites

I can't see it being an accidental thing, as if it were, without casting it, the compiler would complain.So, if you did cast it... like int x = (int)"777"; then it wouldn't be caught because it wouldn't fail. It wouldn't be correct, but it definately wouldn't fail...I'm still trying to fathom the need for crossing types in this case... if you want to assign a the value in a string to an int, you'd just use atoi().I'm really interested to see why this is necessary. More details would be nice.

Share this post


Link to post
Share on other sites

I used MingW and wrote the source with: CONText...but I might be able to find out the problem. And uh...leeiah this is a C/C++ board...why would I post Java stuff here? Very hard question, eh?

Share this post


Link to post
Share on other sites

I mean source code as in what you wrote...
MingW eh? I only use plain old gcc, so I can't help you with that one.

Very hard question, eh?

Well yes and no, see, it's not defined in the standard as to whether or not you can do so. Why don't you compile it and give it a try? We're all stumped.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.