This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes how to convert string to short Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "how to convert string to short" Watch "how to convert string to short" New topic
Author

how to convert string to short

Sanjul Jain
Greenhorn

Joined: Oct 21, 2004
Posts: 14
how can i convert string to short in java.

thanks in advance,
sanjul
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

short s = Short.parseShort(String);

Rene


Regards, Rene Larsen
Dropbox Invite
Sanjul Jain
Greenhorn

Joined: Oct 21, 2004
Posts: 14
thanks for your reply, but its not working. this is what i have done:



I need to print the value of s.


Regards,
Sanjul
Giovanni De Stefano
Ranch Hand

Joined: Aug 17, 2004
Posts: 144
Hi Sanjul,
please make up your mind with what you are trying to do.

You asked in your first post how to convert a string into a short, and Rene told you how to do it properly.
Then, in your second post you say that you need the value of 's' BUT you are parsing a string into a Short WITHOUT noticing that the string itself CANNOT be recognized as a number because it is 'ltest' !!!

Here is a WORKING example:


Try with g = "24" and, for example, g = "24a".

Giovanni


SCJP 1.4
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Sanjul Jain:
thanks for your reply, but its not working. this is what i have done:



I need to print the value of s.


Regards,
Sanjul

You should post what happens when you run this example. I would guess that you are seeing the output when from the catch clause because an exception is thrown. The problem is that the String "1test" cannot be parsed to a short because it has non-numerical characters in it.

For future reference, it would be more helpful to do something like

in the catch clause. This will give you some helpful information when you an exception occurs unexpectedly in your program.

HTH

Layne


Java API Documentation
The Java Tutorial
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: how to convert string to short
 
Similar Threads
String array to arraylist
how to convert byte array into short??
Converting Strings to a Short datatype
How to convert String to org.w3c.dom.Document
how to convert Byte to the String