1

I cant quite work out what i am doing wrong. I am trying to count the amount of digits in an inputted string but for example, if the number is 21 i want to count that as 1 inputted digit in the string.

So far i have the following but i can't work out how to count it as 1 integer rather than 2.

public static int countIntegers(String input) { 
    int count = 0;
    for (int i = 0; i < input.length(); i++) {
        if (Character.isDigit(input.charAt(i))) {
            count++;
        }
    }
    return count;
}

My issue is I want it to a string because there is a certain format the string has to be inputted in.

user3411748
  • 55
  • 1
  • 1
  • 6
  • 1
    What do you mean "count that as one inputted digit"? "21" is not a "digit", it's a number. What do you actually want to do? If you want to count the numbers in a string ("23 1234 5" = 3 numbers) that's a different problem altogether. – markspace Oct 03 '16 at 00:35
  • Ok maybe i am thinking of it in the wrong way. For example the inputted string could be "home,21,car,30,bus,13" which the count should come to 3 whereas at the moment it is coming to 6. Obviously because i am counting the digits – user3411748 Oct 03 '16 at 00:44
  • Do you have any guidance you could give me with this? Maybe point me in the correct direction – user3411748 Oct 03 '16 at 00:48
  • [How to extract numbers from a string and get an array of ints?](//stackoverflow.com/q/2367381) – Tom Oct 03 '16 at 00:50
  • You want to count groups of consecutive digits, not just each digit in the string. You'll need to use a second accumulator to keep track if the last char was a digit. – Carcigenicate Oct 03 '16 at 00:51
  • It doesnt have to be an array as such. I just need a count of how many ints there are – user3411748 Oct 03 '16 at 00:54

2 Answers2

3

Basically, a number — a contiguous sequence of digits.
So, the idea of the algorithm is to count number of such sequences.

I would like to propose the following implementation of the algorithm:

public static int countIntegers(String input) {
    int count = 0;
    boolean isPreviousDigit = false;

    for (int i = 0; i < input.length(); i++) {
        if (Character.isDigit(input.charAt(i))) {
            if (!isPreviousDigit) {
                count++;
                isPreviousDigit = true;
            }
        } else {
            isPreviousDigit = false;
        }
    }
    return count;
}
1

You want to split the input, then check each substring is an int.

public static int countIntegers(String input) {
    String[] parsed = input.split(","); // , is the delimiter
    int count = 0;
    for (String s : parsed) {
        if (s.trim().matches("^-?\\d+$")) { // A slow regex method, but quick and dirty
            count++;
        }
    }

    return count;
}

Example input: home,21,car,30,bus,13

Output: 3

xes_p
  • 501
  • 4
  • 14
  • Say for example if i had an input that had "home,21home, car,30bus, 13" would that return the answer as 3 still? – user3411748 Oct 03 '16 at 01:09
  • I had just inputted it and was trying to work out why it outputted 0. Wasnt sure if my code was wrong. I presume you are meaning the code you posted outputs 0? Because mine seems to – user3411748 Oct 03 '16 at 01:12
  • I'm confused as to why your input is so inconsistent, but adding a `.trim()` (see revised answer) results in `1`. It outputted `0` before because the space character in ` 30` causes the regex expression to consider it a String. – xes_p Oct 03 '16 at 01:15