?
Solved

converting string to short in java

Posted on 2005-05-08
8
Medium Priority
?
491 Views
Last Modified: 2006-11-18
this is what i have done:


  public static void main (String[] args) throws IOException {

        
         short s= 0;
         String g = "1test";    
try {
  s = Short.parseShort(g);
  //s = (short)Integer.parseInt(g.trim(), 16);
  System.out.println(s);
} catch (NumberFormatException e){}    
      System.out.println("exception");
       
        }
 

I need to print the value of s.

Thanks in advance,
Sid
0
Comment
Question by:sjain1203
6 Comments
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 13955745
could use something like:

s = Short.parseShort(g.substring(0, 1));
0
 

Author Comment

by:sjain1203
ID: 13955761
no, this does not work when i change string g to "fred"

--Sid
0
 
LVL 13

Assisted Solution

by:petmagdy
petmagdy earned 500 total points
ID: 13955773
offcourse it will not work, u can only take the first character of the string and put it into the short and Object pointed
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 15

Assisted Solution

by:aozarov
aozarov earned 500 total points
ID: 13956248
I hope it is clear to you that you can not get a unique short value for strings longer then 2 characters.
If you don't care about that then you can do
s = (short) g.charAt(0);
0
 
LVL 2

Expert Comment

by:patbin
ID: 13957548
String str;
str=g.subString(0,1);

short val = Convert.stringToShort (digitString, (short) defaultValue);

if u want detail of above Convert u can refer

http://www.cems.uwe.ac.uk/~lrlang/java_html/docs/uwejava/Convert.html 
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 500 total points
ID: 13958019
You can do something like

BigInteger bi = new BigInteger(1, "fred".toByteArray());
int i = bi.intValue();
short s = (short)bi.intValue();


but you probably don't want short really as it's a signed value
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month16 days, 14 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question