0

I'm new to Java, trying to learn more.

How do I identify a contiguous set of integers in a string?

For example, if I have the string "123hh h3ll0 wor1d" the program should output 4 as the answer.

Here's what I've worked on, and as a result, my program outputs 6. I understand why but I don't know how to implement what I want the program to do.

public static void main (String[] args) throws java.lang.Exception
    {
        String string = "123hh h3ll0 w0rld";

        int count = 0;

        if (string.isEmpty())
            count  = 0;

        for (int i = 0; i < string.length(); i++)
        {
            char c = string.charAt(i);

            if (Character.isDigit(c))
                count++;   
        }

        System.out.println(count);

    }
cb92
  • 33
  • 4

6 Answers6

0

Your program counts each occurrence of a digit within the input string; and as there 6 digits; 6 is your result. So no surprises there. You have to understand: if you are interested in sequences of digits, then just checking each character "are you a digit" isn't enough!

If you are interested in the length of the longest sequence, then your "counting" must be enhanced:

  1. While being within a sequence, you keep increasing the "currentSequenceLength" counter
  2. When hitting a non-digit, you stop counting; and you compare the length of the last sequence with the "maximum" that you also have to remember.

That should be enough to get you going; as for sure; the idea is not that we do the homework/learning part for you.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
0

Your program is a good start, but it counts all digits. You need to avoid count++ when you are in a contiguous group of digits. You can do it by adding a boolean flag which you set to true when you see a digit, and then to false when you see a non-digit:

boolean inDigits = false;
for (int i = 0; i < string.length(); i++)
{
    char c = string.charAt(i);

    if (Character.isDigit(c)) {
        if (!inDigits) count++;
        inDigits = true;
    } else {
        inDigits = false;
    }
}

Demo 1

A simpler way to find the number of groups is to split on \\d+ (a sequence of one or more digits), count the number of groups you get, and subtract one:

System.out.println(string.split("\\d+").length-1);

Demo 2

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0

Based on what you stated about contiguous, you want to reset the count every time you are not on a digit and store the maximum count achieved during this process.

Add int currentMaximum = 0 and when a non-digit is read in, check to see if count is greater than currentMaximum and set currentMaximum to count and then set count = 0. This will cause the count to reset at each non-digit and only count up when digits are contiguous.

D. Law.
  • 234
  • 1
  • 3
  • 8
0

So your code snippet is simply counting every digit in the string, which you said you knew. So you have to see which situation does it occur that you actually want to count. For contiguous digits, the situation you're looking for is when the current character is a digit, and the next is not. That is when the chain of contiguous digits is broken. I have edited your for loop to use this technique to find the number of contiguous digit sets:

public static void main (String[] args) throws java.lang.Exception {

        String string = "123hh h3ll0 w0rld";
        int count = 0;

        if (string.isEmpty()) {

            count  = 0;
        } else {

            for (int i = 0; i < string.length() - 1; i++){

                char current = string.charAt(i);
                char next = string.charAt(i+1);
                if (Character.isDigit(current) && !Character.isDigit(next)){

                    count++;
                }
            }

            System.out.println(count);
        }
    }
0

I would target the numbers in the string via pre-processing OR real time, your problem statement doesn't define a requirement for either, and then refer to the related problem: here and additionally here (which provides a c++ and java sample too).

There's not too much to elaborate on because you haven't set up the problem space to define other factors (more in-depth problem statement by OP would help answers reflect more accurate responses). Try to specify such things as:

  • does/should it reset when encountering non-digits?

  • can you use objects like sets?

  • are you reading in simple test strings or large data amounts?

etc.

Without more info, I think there will be a varying response of answers here. Cheers

Community
  • 1
  • 1
Nick Bell
  • 516
  • 3
  • 16
0

This code will generate count = 6 and group = 4.

enter     public static void main (String[] args) throws java.lang.Exception
{
    String string = "123hh h3ll0 w0rld";
    boolean isGroup = false;

    int count = 0;
    int group = 0;

    if (string.isEmpty()) {
        count  = 0;
    } else {
        for (int i = 0; i < string.length(); i++) {
            char c = string.charAt(i);

            if (Character.isDigit(c)) {
                count++;
                if (!isGroup) {
                    group++;
                    isGroup = true;
                }
            } else {
                isGroup = false;
            }

        }
    }

    System.out.println(count);
    System.out.println(group);

}
PEF
  • 207
  • 1
  • 4
  • 11