Reeggiie
Reeggiie

Reputation: 822

How to compare elements in a single string?

I need to print the indexes of the elements in a string of 1s and 0s if and only if the 1s and 0s repeat more than once side by side.

For example: input: "0010011" output: "0, 3, 5"

A repeat starts at indexes 0 , 3, and 5.

Here is what I currently have

Scanner keyboard = new Scanner(System.in);
    ArrayList runs = new ArrayList();


    System.out.println("Enter non empty string of 1s and 0s");
    String input = keyboard.nextLine();
    char[] array = input.toCharArray();

    for(int i = 0; i < input.length(); i++)
    {
        if(array[i] == array[i++])
        {
            runs.add(i);
        }

    }
    for(int i = 0; i<= array.length; i++)
    {
        System.out.println(runs);
    }

When I test this, I try input of "00100" and get an output of "1,3,5". Another test input from above "0010011" and get "1,3,5,7". It seems to be printing odd numbers, not the indexes where numbers start to repeat. Can anyone spot what I am doing wrong? I have a feeling its coming from my comparing in the first for loop.

Upvotes: 0

Views: 151

Answers (4)

Rohit Jain
Rohit Jain

Reputation: 213243

There are 3 immediate issues that I can point out:

  • First is obvious. You are using i++, while you should be using i + 1. i++ will increment the value of i, so you're missing out an index there for next iteration.
  • Second, you should iterate till input.length() - 2, else you will get an ArrayIndexOutOfBounds exception, when you access array[i + 1] for the last index.
  • Your current logic will fail if you've more than 2 0's or 1's in sequence. It will print both 0 and 1 index for say, 00010.

For the third point, what you should do is, once you find two consecutive characters same, you should skip upcoming character that are same. You will need an inner loop here. Probably a do-while.

You should modify your for loop to this:

for(int i = 0; i < input.length() - 1; i++)
{
    if(array[i] == array[i+1])
    {
        System.out.println(i);
        do {
            i++;
        } while (i < input.length() - 1 && array[i] == array[i + 1]);
    }
}

Also, ArrayList doesn't have a length attribute. You should use size() method to get maximum size.


BTW, this can also be done using regex. Well, you might not have been taught about this yet, but this is just for another possible way:

Pattern pattern = Pattern.compile("0{2,}|1{2,}");
Matcher matcher = pattern.matcher(input);

while (matcher.find()) {
    System.out.println(matcher.start());
}

Upvotes: 1

PlasmaPower
PlasmaPower

Reputation: 1878

This should work:

Scanner keyboard = new Scanner(System.in);

System.out.println("Enter non empty string of 1s and 0s");
String input = keyboard.nextLine();
char lastChar = '~'

for(int i=0; i<input.length() - 1; i++) {
    if(input.charAt(i) == input.charAt(i + 1) && (i == 0 || input.charAt(i) != input.charAt(i - 1))) {
        System.out.println(i);
    }
}

Upvotes: 2

user3331198
user3331198

Reputation: 184

Try this :-

Scanner keyboard = new Scanner(System.in);
    ArrayList runs = new ArrayList();


    System.out.println("Enter non empty string of 1s and 0s");
    String input = keyboard.nextLine();
    char[] array = input.toCharArray();

    for(int i = 0; i < input.length()-1; i++)
    {
        if(array[i] == array[i+1])
        {
            runs.add(i);
        }

    }
    for(int i = 0; i<= array.length; i++)
    {
        System.out.println(runs);
    }

Upvotes: -1

zw324
zw324

Reputation: 27190

Two problems there:

First, when the postfix ++ return the value before take effect, so you are comparing array[i] with itself.

Also, you are doing two plus 1 each loop, so there are only odd numbers.

Upvotes: 0

Related Questions