Question: I'm writing this class called CharArrayProject_3 that removes repeating elements from an array of chars. However, when I run the driver class, it just outputs

I'm writing this class called CharArrayProject_3 that removes repeating elements from an array of chars. However, when I run the driver class, it just outputs two sets of dashed lines. What am I getting wrong? Is it the deleteRepeats() method?:

public class CharArrayProject_3 { private char[] array; privateint length; privateintnumberOfRepeats;

public CharArrayProject_3( char[] arr ) { length = arr.length; array = new char[ length ]; numberOfRepeats = 0; for( int k = 0; k < arr.length; k++ ) { array[k] = arr[k]; } }

public void deleteRepeats() { int j = 0;//for next element for (inti=0; i < length-1; i++){ if (array[i] == array[i+1]){ array[j++] = array[i]; numberOfRepeats++; } } array[j++] = array[length-1]; } }

public String toString() { String result; for (char value : array) { result += value; } String result = " Number of Repeats: " + numberOfRepeats; return result; } }

public class CharArrayProject_3_Driver { public static void main( String args[] ) { char a[] = { 'h','o','l','l','y', 'c','l','e','m','e','n','c','e' }; CharArrayProject_3 hello = new CharArrayProject_3( a ); hello.toString(); hello.deleteRepeats(); System.out.println(); hello.toString(); System.out.print( " ----------------------------- " + "----------------------------- "); char b[] = { 'a','b','c','b','b', 'c','a','d','a','d','c' }; hello = new CharArrayProject_3( b ); hello.toString(); hello.deleteRepeats(); System.out.println(); hello.toString(); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!