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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started