Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8570369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:32:32+00:00 2026-06-11T18:32:32+00:00

Suppose i want to create a method that takes a number as a string

  • 0

Suppose i want to create a method that takes a number as a string and return its number form. Like

getNumber("123456");

public int getNumber(String number) {

    //No library function should used. Means i can't do Integer.parseInteger(number).

} //end of getNumber()

How can i implement that method like

public int getNumber(String number) {

    for (int i=0; i<number.length; i++) {

    char c = number.getCharacter(i);
    ///How can i proceed further

    } //end of for()

} //end of getNumber()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T18:32:34+00:00Added an answer on June 11, 2026 at 6:32 pm

    Without using a library function, subtract the character '0' from each numeric character to give you its int value, then build up the number by multiplying the current sum by 10 before adding the next digit’s ìnt value.

    Java 7

    public static int getNumber(String number) {
        int result = 0;
        for (int i = 0; i < number.length(); i++) {
            result = result * 10 + number.charAt(i) - '0';
        }
        return result;
    }
    

    Java 8

    public static int getNumber(String number) {
        return number.chars().reduce(0, (a, b) -> 10 * a + b - '0');
    }
    

    This works primarily because the characters 0-9 have consecutive ascii values, so subtracting '0' from any of them gives you the offset from the character '0', which is of course the numeric equivalent of the character.


    Disclaimer: This code does not handle negative numbers, arithmetic overflow or bad input.

    You may want to enhance the code to cater for these. Implementing such functionality will be instructive, especially given this is obviously homework.


    Here’s some test code:

    public static void main(String[] args) {
        System.out.println(getNumber("12345"));
    }
    

    Output:

    12345
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's suppose I want to create a javascript class/object/function which have a method that
Suppose I want to create trees of a certain set depth, that is, the
Suppose that i have a city that has many buildings. I want to create
Suppose I want to have a class like Java Date . Its only data
I want to create sub-domains using PHP on the fly. Suppose a user registers
Suppose I want to store N samples (each sample takes up a significant portion
Suppose I want to put objects that identify a server into a stl set
Suppose I want to implement an interpreter for a functional language. I would like
Suppose I want to create a (stateless) WCF service with three methods exposed on
Suppose I want to create an abstract class in Python with some methods to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.