String to Integer

Posted on 2006-04-05
Last Modified: 2010-03-31
How to convert a string variable to integer variable without using Integer.parseInt() fuction
Question by:aeltech
    LVL 92

    Accepted Solution

    Loop thru the characters from last to first
    for each character convert it to a number,  int n = (int) c - '0'
    and mutlipy it by a multiplier which is initially 1, and add the result to your result value
    for every loop, multipy the multiplier by 10

    let me know if u get stuick :)
    LVL 30

    Assisted Solution

    Use valueOf () ;-)

    Oops, this has been asked before:
    LVL 30

    Expert Comment

    That is somebody's homework done.
    LVL 92

    Expert Comment

    > Aks him how many of his  comments I deleted

    Not that many that I can recall :) But some yes.
    LVL 30

    Expert Comment

    He he he he.... there is a reason why objects did not post full-code. You think him or I couldn't have written it ;-) ?
    LVL 13

    Expert Comment

    Hi aeltech,

    See link posted by mayankeagle:
    for each digit : value=value*10+(digit-'0');

    And if you have signed values, check if first character is '+' or '-' or digit, if '-' then return -value

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Join & Write a Comment

    For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    This video teaches viewers about errors in exception handling.
    This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

    734 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

    19 Experts available now in Live!

    Get 1:1 Help Now