Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> ..what Happens If
8bit
post Feb 19 2005, 12:41 AM
Post #1


Member [Level 2]
*****

Group: Members
Posts: 85
Joined: 27-December 04
Member No.: 2,976



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!?
Go to the top of the page
 
+Quote Post
leiaah
post Feb 19 2005, 04:07 PM
Post #2


Super Member
*********

Group: Members
Posts: 436
Joined: 21-January 05
From: Koronadal City, Philippines
Member No.: 3,358



QUOTE(8bit @ Feb 19 2005, 08:41 AM)
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!?
*




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);
Go to the top of the page
 
+Quote Post
osknockout
post Feb 19 2005, 11:22 PM
Post #3


Super Member
*********

Group: Members
Posts: 399
Joined: 14-November 04
From: Elysium
Member No.: 2,280



QUOTE
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 laugh.gif Just a thought leeiah.
QUOTE
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.
Go to the top of the page
 
+Quote Post
dexter
post Feb 21 2005, 11:00 AM
Post #4


Advanced Member
*******

Group: Members
Posts: 142
Joined: 24-December 04
From: Queensland, Australia
Member No.: 2,902



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.
Go to the top of the page
 
+Quote Post
8bit
post Feb 24 2005, 12:31 AM
Post #5


Member [Level 2]
*****

Group: Members
Posts: 85
Joined: 27-December 04
Member No.: 2,976



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?
Go to the top of the page
 
+Quote Post
osknockout
post Feb 24 2005, 11:28 PM
Post #6


Super Member
*********

Group: Members
Posts: 399
Joined: 14-November 04
From: Elysium
Member No.: 2,280



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.
QUOTE
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.
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

 



- Lo-Fi Version Time is now: 29th August 2008 - 08:48 PM