-2

Today I was given this interview question. Can someone please help me with the solutions?

/**
 * Return the sum of all integers from a random string. Continuous Integers must be considered as one number. 
 * Given the following inputs, we expect the corresponding output:
 * 
 * "1a2b3c"  ==>6 (1+2+3)
 * "123ab!45c" ==> 168 (123+45)
 * "abcdef" ==> 0 (no integers in String)
 * "0123.4" ==> 127 (0123+4)
 * "dFD$#23++++12@#T1234;/..10" => 1279 (23+12+1234+10)
 * "12a-10b" => 2 (12-10)
 */

private static long SumOfNumbers (String str){
    return null;
afzalex
  • 8,598
  • 2
  • 34
  • 61
  • 1
    How did you try to solve it? And where were you blocked? – Codebender Sep 04 '15 at 04:18
  • 1
    Maybe a `Scanner` can help, or a regular expression to split the `String` or a `for-loop`.... – MadProgrammer Sep 04 '15 at 04:19
  • Possible Duplicate - http://stackoverflow.com/questions/28227772/return-the-sum-of-all-integers-from-a-random-string-without-using-regex – Arun Xavier Sep 04 '15 at 04:22
  • http://stackoverflow.com/questions/28223991/write-java-program-to-sum-of-all-integers-from-a-random-string – Arun Xavier Sep 04 '15 at 04:23
  • 2
    What the interviewers are looking for is how "you" might solve the question. You give it a shot and see what you can come up with and the post those results over at [codereview exchange](http://codereview.stackexchange.com/) – MadProgrammer Sep 04 '15 at 04:32

3 Answers3

0

We cannot give the whole solution for you when you haven't tried any research. But to give a hint on how to solve,

  • Use a scanner to read input
  • Use regex to remove the non-numerics except math operators from read input

This should be your approach of what you should do. Then comes how you do it. Try your research and then post questions in case of technical difficulty.

Karthik R
  • 5,523
  • 2
  • 18
  • 30
0

You can do this on a character by character basis, just by accumulating the digits (and sign) and summing them when you detect end of number.

The algorithm would go something like this:

set sign to 1.
set total to 0.
set accum to 0.
for each character:
    if character is digit:
        accum = accum * 10 + value(character)
    elif accum > 0:
        total = total + sign * accum
        accum = 0
        if character is '-':
            sign = -1
        else:
            sign = 1
    else:
        if character is '-':
            sign = -sign

if accum > 0:
    total = total + sign * accum

Basically, if you get a digit, you add it to the accumulator (which is used to build the current number).

Whenever you find a non-digit with a non-zero accumulator, you add that accumulator to the total and set the sign based on whether the current character is - or something else (but it can't be a digit at this point).

Where you have a non-digit and nothing in the accumulator, you just have to treat - signs as a special case, so as to pick up things like --10 as 10.

At the end, you once again add a non-zero accumulator to take into account you might end on a digit.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
0

Try below code

private static long SumOfNumbers(String str) {
    int digit = 0, number = 0;
    long sum = 0;
    // Iterate through each character in the input string
    for (int i = 0; i < str.length(); i++) {

        // Convert the character to integer
        digit = Integer.valueOf(str.charAt(i)) - '0';

        // Check if the character is a integer
        // add the digits till a non-integer is encountered
        if (digit >= 0 && digit <= 9) {
            number = number * 10 + digit;
        } else {
            sum += number;
            number = 0;
        }
    }
    sum += number;
    return sum;
}
  • That's a good *start* but won't handle negative numbers such as the final case `"12a-10b" => 2 (12-10)`, – paxdiablo Sep 04 '15 at 05:20