Link to home
Start Free TrialLog in
Avatar of sjain1203
sjain1203

asked on

converting string to short in java

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
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sjain1203
sjain1203

ASKER

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

--Sid
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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 
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial