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 8784843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:12:24+00:00 2026-06-13T21:12:24+00:00

I am writing an Atoi function in Java. It runs fine for +ve integers.

  • 0

I am writing an Atoi function in Java. It runs fine for +ve integers. But what I want is when I enter a negative integer it should give me an error. So I tried including continue statement in my class Atoi. The class implemented is:

class Atoi {

    int atoi(String tmp) {

    int result = 0;

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

            char digit = (char)(tmp.charAt(i) - '0');

        if(digit == '-')

        continue;
        }

        else {

            result += (digit * Math.pow(10, (tmp.length() - i - 1)));
        }

    return result;

    }
}   

But unfortunately it gives me the negative equivalent of the character i.e for -12 it gives me 655312! Help.

EDIT: Suppose I need to check for floating point numbers what should I do? If I enter 12.1 or 123.2 it should return 12.1 and 123.2 repectively!!

  • 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-13T21:12:25+00:00Added an answer on June 13, 2026 at 9:12 pm

    Quick fix for the obvious problem: the order of the logic was wrong…

    Instead of

        char digit = (char)(tmp.charAt(i) - '0');
        if(digit=='-')
        continue;
    

    try

        char origChar=tmp.charAt(i);
        if(origChar=='-')
            continue;
        char digit = (char)(origChar - '0');
    

    But there are two more problems:

    • it does not negate the value, in case of a ‘-‘ character is present!
    • what if this is the input string: -1-2-3-4-5? The result will be interesting! EDIT: try this input also: ‘répa’… Even more interesting result!

    Don’t forget to test with incorrect inputs too, and as @Klaus suggested, don’t hesitate to throw an exception, (preferably IllegalArgumentException) with a correct error message, if an incorrect input is given to the function…

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

Sidebar

Related Questions

Writing a recursive function, I want one fn that executes when the list has
Writing a python program, and I came up with this error while using the
I am mostly a Python/MIT Scheme programmer, but I want to learn C because
Writing a large GUI based application in Java and was wondering if there were
Writing a templated function, I declared: template <typename T> T invertible(T const& container, T::size_type
Writing a shell script and I want to do something like this: cp myfile.ext
I am writing a simple Objective - C console app. I only want to
Writing some test scripts in IronPython, I want to verify whether a window is
Writing a dictionary application for android. Want to set translation direction in accord with
writing somewhat of a css hack, styling for :hover {} works interestingly, but browsers

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.