?
Solved

Convert string to int

Posted on 2006-05-11
7
Medium Priority
?
249 Views
Last Modified: 2008-02-01
Hi

I'd like to convert string to int and make logical check like if(i < 0). Can you please help?

Thanks
0
Comment
Question by:hasozduru
7 Comments
 
LVL 11

Expert Comment

by:fargo
ID: 16662013
String test="23";

int tt =  Integer.valueOf(test).intValue();

if(tt>0){
// i m greater than 0
}
0
 
LVL 92

Accepted Solution

by:
objects earned 1200 total points
ID: 16663249
int i = Integer.parseInt(s);
if (i<0){
   // less than 0
}


0
 
LVL 23

Assisted Solution

by:Siva Prasanna Kumar
Siva Prasanna Kumar earned 400 total points
ID: 16663906
try{
int i = Integer.parseInt(s);
}
catch(NumberFormatException e)
{
e.printStackTrace();
}

if(i<0)
{
//do something.
}

This is just an addon to Objects Comments as You need to handle Number Format Exception which may be casued if the String is not a valid integer. in the sense String num = "123a3" or so.

Thank You.
0
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!

 
LVL 8

Expert Comment

by:hiteshgupta1
ID: 16665995
Whats wrong with solution suggesed by Fargo??
0
 
LVL 23

Expert Comment

by:Siva Prasanna Kumar
ID: 16666096
Integer.valueOf(test).intValue();

nothing is wrong actually its a 2 steps first coverts String to Integer (wrapper Class ) and then to integer form Integer.

Its also good & effective.
0
 
LVL 11

Assisted Solution

by:fargo
fargo earned 200 total points
ID: 16666161
No. parseInt is more effective and better to use.

see here
http://www.javaworld.com/javaworld/javaqa/2000-11/03-qa-1117-integer.html
0
 
LVL 23

Assisted Solution

by:basicinstinct
basicinstinct earned 200 total points
ID: 16666553
the try/catch block from shivaspk is essential - you should definitely incorporate that
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Often times it's very very easy to extend a volume on a Linux instance in AWS, but impossible to shrink it. I wanted to contribute to the experts-exchange community a way of providing a procedure that works on an AWS instance. It can also be used on…
Social messanging services like WhatsApp and Facebook can help businesses in ways that many owners don't even imagine, giving new opportunities to connect with customers. Discover some of the most innovative things they can do for your company.
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

840 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