Jump to content

  • Log In with Google      Sign In   
  • Create Account


[java] string to integer??!?!?!??!?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 HearYouMe   Members   -  Reputation: 122

Like
Likes
Like

Posted 25 May 2001 - 10:45 AM

I have something like this.. String a = "1"; int b = "0"; how can i get the value of b to equal a''s value? Is there like a string to integer value command or something.. i dunno. im new to java but i really need to do this. im making a tic-tac-toe game where the user enters a row and col and then i go like board[col][row]="X"; but it wont allow that cause col and row are strings

Sponsor:

#2 jwalker   Members   -  Reputation: 122

Like
Likes
Like

Posted 25 May 2001 - 01:15 PM

string to integer

int a = Integer.parseInt("1");


integer to string

String a = new String("" + 1);



{ Stating the obvious never helped any situation !! }

#3 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

Likes

Posted 25 May 2001 - 07:52 PM

no need to call the constructor, just do ""+1. It is probably faster and it''s also a little easier to read

#4 OctDev   Members   -  Reputation: 158

Like
Likes
Like

Posted 25 May 2001 - 09:04 PM

Why not just read in the values as integers?

--OctDev

#5 jwalker   Members   -  Reputation: 122

Like
Likes
Like

Posted 26 May 2001 - 02:46 AM

its the same , since String class is immutable.

String a = new String("a");

and

String a = "a";

both are the same because string class will copy itself to create a new copy. immutable.

{ Stating the obvious never helped any situation !! }

#6 c_wraith   Members   -  Reputation: 122

Like
Likes
Like

Posted 26 May 2001 - 12:39 PM

Actually,

String a = new String("a");

and

String a = "a";

are not the same in java, if you look at the language specification. Constants in the source code are compiled into the constant table in the class file. With String objects, this means they will be part of the JVM''s intern pool for strings, meaning that ("a" == "a") will always evaluate to true. However, putting new in the source code guarantees that a new object will be allocated, meaning that ("a" == new String("a")) will be false.

And by the way, the best way to convert an integer to a string is

int i = 1468;
String s = Integer.toString(i);

The s = "" + i approach is compiled as

s = new StringBuffer("").append(i).toString();

which is less than fast, as in addition to creating the new Stringbuffer and calling toString() on it when it''s done, StringBuffer''s append(int) method calls String.valueOf(int), which calls Integer.toString(int, 10), which calls Integer.toString(int).

Digging through the source code distributed with the jdk can be fun. =)

#7 jwalker   Members   -  Reputation: 122

Like
Likes
Like

Posted 26 May 2001 - 01:04 PM

issit ?? . i didnt realise ..
i knew that literal''s were compiled into the class, but i though immutable objects always duplicated themselves ..

anyway, sorry for the mixup .



{ Stating the obvious never helped any situation !! }




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS