Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

string converts to an integer,not using Integer.parseInt

  Asked By: Nisha    Date: May 06    Category: Java    Views: 3427
  

I just started learning java.I need help to get a string from
keyboard and convert it to an integer without using the
Integer.parseInt(). I have tried using the s.length() and the char
charAt() method. But i can't seem to get do out all the requirements
without using the integer.parseInt().
Requirments
1) no + sign but can have - sign
2)no spacing in front..if have -123 456..it will only appear 123
3)Error: illegal input for abce...

Share: 

 

2 Answers Found

 
Answer #1    Answered By: Francis Riley     Answered On: May 06

try
{
int i = new Integer(s).toInt();
}
catch (Exception e)
{
e.printStackTrace();
}

 
Answer #2    Answered By: Alan Palmer     Answered On: May 06

oops
int i = new Integer(s).intValue();

 
Didn't find what you were looking for? Find more on string converts to an integer,not using Integer.parseInt Or get search suggestion and latest updates.




Tagged: