Need help with array manipulation
Hey there. I'm writing a piece of code that basically removes a specific character from the array and here is the method for it. What it's supposed to do is get input from the user on what character to remove form the array, and once that character is inputted, the program should find the first occurrence of the character and remove it (and only remove the first occurence of the character).
Once the character has been removed, the array storing the all the characters (that has been extracted from a string) should be shifted to the left to make up for the blank value that is now stored where the specified character was.
The code I have written does indeed remove the character and shifts the array one to the left. However, it does not shift the very last character in the array.
For example: if the array stores the characters "hello" which has been inputted from a string (String mainString)
where:
w[0]=h
w[1]=e
w[2]=l
w[3]=l
w[4]=o
if i wanted to remove the "e" in hello through this method, the program would remove it but would leave the array like this:
"hlllo"
Any help would be appreciated, thank you
/* METHOD PURPOSE: Removes a specified character from the array and shifts
the array one unit to the left */
private void removeCharacter() {
int i; // initialize counter -- needed to know when to start shifting the contents of the array back left
char characterRemove = KeyIn.readChar("Please enter the character you would like to remove (in lowercase letters):", 'a', 'z'); // prompt for desired character to be removed (letter must be lowercase and must be between a and z)
for (i = 0; i <= mainString.length()-1; i++) { // scan through until specified character is found
if (wordArray == characterRemove) // if the character matches
break; // get out of loop and retain i value
}
for (int b=i; b <= (mainString.length()-1); b++) { // start at the i value and end when the end of the string is reached
wordArray = wordArray[i+1]; // shift array one to the left
}
String tempString = "";
for (int d = 0; d <= (mainString.length()-1); d++) { // store array contents in a string
tempString += wordArray[d]; // construct string from array
}
mainString = tempString; // store the temporary string back into the data field
commandLine(); // return to the command line to await another command
} // END METHOD
[2552 byte] By [
geniewiza] at [2007-10-2 5:23:57]

You can't change the size of an array once you've created it, so the design is somewhat flawed. Can you change it to allow you to copy into a new array which is one character shorter?
sounds like an off by one error
int x = //index of c in arr
if (x >= 0) {
for (int i = x + 1; i < len; i++)
arr[i - 1] = arr[i];
len--;
}
I don't understand your code.
private void removeCharacter() {
int i;
char characterRemove = KeyIn.readChar("Please enter the character you would " +
"like to remove (in lowercase letters):", 'a', 'z');
for (i = 0; i <= mainString.length()-1; i++) {
if (wordArray == characterRemove) // You're comparing an array to a character!?
break;
}
for (int b=i; b <= (mainString.length()-1); b++) {
wordArray = wordArray[i+1]; // You set an array to an element of that array?
}
String tempString = "";
for (int d = 0; d <= (mainString.length()-1); d++) {
tempString += wordArray[d];
}
mainString = tempString;
commandLine();
}
I would expect it to look more like:
public boolean remove(char c) {
int i = 0;
while (i < wordArray.length) {
if (c == wordArray[i])
break;
i++;
}
if (i == wordArray.length - 1) {
wordArray[i] = null;
return true;
}
if (i != wordArray.length) {
System.arraycopy(wordArray, i + 1, wordArray, i, wordArray.length - i - 1);
wordArray[wordArray.length - 1] = null;
return true;
}
return false;
}
Assuming wordArray is a char[] that contains your characters. This would shift everything in the array that is to the right of the character being removed to the left, overwriting the character in the process and then removing the extraneous character at the end by setting it to null. Furthermore to avoid unnecessary copies it checks to see if the character is at the end already before doing it. This method will return true if the character is found and removed and false if it is not found.
It's true an array can never get smaller, but that doesn't mean it's contents can't be collected. The expense of creating a new array of a specific size and copying is probably not worth saving a tiny bit of memory.
thanks for the reply, but there is a problem with the code you have given me:i get the following error message for these two lines:wordArray = null;wordArray[wordArray.length - 1] = null;error: incompatible types; found: null, required: char.
sorry about that, i think there was something wrong when I pasted the code, some of the code was left out here is the code:
/* METHOD PURPOSE: Removes a specified character from the array and shifts
the array one unit to the left */
private void removeCharacter() {
int i; // initialize counter -- needed to know when to start shifting the contents of the array back left
char characterRemove = KeyIn.readChar("Please enter the character you would like to remove (in lowercase letters):", 'a', 'z'); // prompt for desired character to be removed (letter must be lowercase and must be between a and z)
for (i = 0; i <= mainString.length()-1; i++) { // scan through until specified character is found
if (wordArray == characterRemove) // if the character matches
break; // get out of loop and retain i value
}
for (int b=i; b <= (mainString.length()-1); b++) { // start at the i value and end when the end of the string is reached
wordArray = wordArray[i+1]; // shift array one to the left
}
String tempString = "";
for (int d = 0; d <= (mainString.length()-1); d++) { // store array contents in a string
tempString += wordArray[d]; // construct string from array
}
mainString = tempString; // store the temporary string back into the data field
commandLine(); // return to the command line to await another command
} // END METHOD
sorry, i guess u're not allowed to put a character inside the square brackets in this forum without spaces
here is the actual code
/* METHOD PURPOSE: Removes a specified character from the array and shifts
the array one unit to the left */
private void removeCharacter() {
int i; // initialize counter -- needed to know when to start shifting the contents of the array back left
char characterRemove = KeyIn.readChar("Please enter the character you would like to remove (in lowercase letters):", 'a', 'z'); // prompt for desired character to be removed (letter must be lowercase and must be between a and z)
for (i = 0; i <= mainString.length()-1; i++) { // scan through until specified character is found
if (wordArray[ i ] == characterRemove) // if the character matches
break; // get out of loop and retain i value
}
for (int b=i; b <= (mainString.length()-1); b++) { // start at the i value and end when the end of the string is reached
wordArray[ i ] = wordArray[ i+1 ]; // shift array one to the left
}
String tempString = "";
for (int d = 0; d <= (mainString.length()-1); d++) { // store array contents in a string
tempString += wordArray[d]; // construct string from array
}
mainString = tempString; // store the temporary string back into the data field
commandLine(); // return to the command line to await another command
} // END METHOD
Thanks a lot guys but I got it working! Special thanks to kablair because your System.array copy command is the one that did the trick.
thanks again,
here is the final code for the method
/* METHOD PURPOSE: Removes a specified character from the array and shifts
the array one unit to the left */
private void removeCharacter() {
int i; // initialize counter -- needed to know when to start shifting the contents of the array back left
char characterRemove = KeyIn.readChar("Please enter the character you would like to remove (in lowercase letters):", 'a', 'z'); // prompt for desired character to be removed (letter must be lowercase and must be between a and z)
for (i = 0; i <= mainString.length()-1; i++) { // scan through until specified character is found
if (wordArray[ i ] == characterRemove) // if the character matches
break; // get out of loop and retain i value
}
/*for (int b=i; b <= (mainString.length()-1); b++) { // start at the i value and end when the end of the string is reached
wordArray[ i ] = wordArray[ i+1 ]; // shift array one to the left
} */
System.arraycopy(wordArray, i + 1, wordArray, i, wordArray.length - i - 1);
String tempString = "";
for (int d = 0; d <= (mainString.length()-2); d++) { // store array contents in a string
tempString += wordArray[d]; // construct string from array
}
mainString = tempString; // store the temporary string back into the data field
commandLine(); // return to the command line to await another command
} // END METHOD
//for (int b=i; b <= (mainString.length()-1); b++) {
//wordArray[ i ] = wordArray[ i+1 ]; // shift array one to the left
//}
Replace [ i ] with [ b ].
Also, you are going to get an array index out of bounds exception.
Look at the example code I posted for shifting.
Oops. Forgot we were dealing with a primitive. You'd have to use the Character wrapper instead of char for that example.