This week's book giveaways are in the Scala and Android forums.
We're giving away four copies each of Machine Learning Systems: Designs that scale and Xamarin in Action: Creating native cross-platform mobile apps and have the authors on-line!
See this thread and this one for details.
Win a copy of Machine Learning Systems: Designs that scale this week in the Scala forum
or Xamarin in Action: Creating native cross-platform mobile apps in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

how to convert string to int  RSS feed

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how to convert string to int without using any standard function in java.
write an algorithm that implements parseInt?
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Depends on what you mean by standard functions. Off the top of my head you could take a String, examine it to be sure that is contains all digits (simple if you use a regular expression, but is that allowed?), reverse the String, examine each digit, convert it to an int (or Integer) using some sort of mapping, multipliy it by the appropriate power of 10, and add the result to the total. Sounds pretty easy, but I'd rather use Integer.parseInt().
 
author and iconoclast
Sheriff
Posts: 24219
40
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Sukhadev Patil:
how to convert string to int without using any standard function in java.
write an algorithm that implements parseInt?



This is very clearly a homework or interview question, as are all the other questions you've posted this morning. Folks, please don't provide a direct answer.

The way things work around here is you show us how far you've gotten, and we give you hints to help when you're stuck.
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, supposing your number is stored in a String object, like this:

String number = "123456";

Then you could say that "123456" is the same as:

6 x 1 = 6
5 x 10 = 50
4 x 100 = 400
3 x 1000 = 3000
2 x 10000 = 20000
1 x 100000 = 100000

Total = 123456

Right?

Well, now give it a look at the java.lang.Character documentation and read about the digit(char ch, int radix) method.

Good luck!
 
please buy this thing and then I get a fat cut of the action:
Rocket Oven Kickstarter - from the trailboss
https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!