converting string to short in java

Posted on 2005-05-08
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);
} catch (NumberFormatException e){}    

I need to print the value of s.

Thanks in advance,
Question by:sjain1203
    LVL 92

    Accepted Solution

    could use something like:

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

    Author Comment

    no, this does not work when i change string g to "fred"

    LVL 13

    Assisted Solution

    offcourse it will not work, u can only take the first character of the string and put it into the short and Object pointed
    LVL 15

    Assisted Solution

    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);
    LVL 2

    Expert Comment

    String str;

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

    if u want detail of above Convert u can refer
    LVL 86

    Assisted Solution

    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
    By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
    Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
    The viewer will learn how to implement Singleton Design Pattern in Java.

    761 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

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now