Reputation: 161
I'm writing a program that's supposed to replace all of the instances of a single letter with another letter. I have some restrictions on the code though, I'm only allowed to use the String methods .length, .substring, .indexOf, and .equals. I can concatenate things using + instead of .concat.
My problem right now is, when I'm printing my results to the program, the answer has to be contained in a variable. I was previously using this code before I realized this:
if (userCommand.equalsIgnoreCase("replace all")) {
System.out.println("Enter the character to replace");
String replace = keyboard.nextLine();
System.out.println("Enter the new character");
String replaceWith = keyboard.nextLine();
int count = 0;
System.out.print("The new string is: ");
while (lastCharacter >= 0) {
char nextCharacter = userString.charAt(count);
count++;
String nextCharacterString = nextCharacter + "";
if (nextCharacterString.equals(replace)) {
nextCharacterString += replaceWith;
}
System.out.print(nextCharacterString);
lastCharacter--;
}
System.out.println("");
}
As you can see, this prints each character to the console one by one, instead of as a variable that can be manipulated later. The code I'm using right now (it is nowhere near working) is:
if (userCommand.equalsIgnoreCase("replace all")) {
System.out.println("Enter the character to replace");
String replaceString = keyboard.nextLine();
char replace = replaceString.charAt(0);
System.out.println("Enter the new character");
String replaceWithString = keyboard.nextLine();
char replaceWith = replaceWithString.charAt(0);
String allLetters = "";
int count = 0;
int indexOfReplacement = 0;
for (int i = 0; i < length; i++) {
char nextCharacter = userString.charAt(count);
count++;
if (replace == nextCharacter) {
indexOfReplacement = count;
nextCharacter = replaceWith;
}
}
String sub1 = userString.substring(0, indexOfReplacement);
System.out.println(sub1);
}
Any help would be very much appreciated.
Upvotes: 0
Views: 50
Reputation: 3874
Here is a small snippet I wrote up. I made some assumptions about the contents of variables to make it be able to run, but it works, replacing all instances of replace
with replaceWith
:
String your_string = ""; // Variable to copy the new string into
String userString = "this_string"; // String to do replacement on
int lastCharacter = userString.length() - 1; // Length of inputted string
String replace = "g"; // Thing to replace
String replaceWith = "d"; // Thing to replace with
int count = 0; // Index of character in string
while(lastCharacter >= 0) {
char nextCharacter = userString.charAt(count);
String nextCharacterString = nextCharacter + "";
if (nextCharacterString.equals(replace)) {
nextCharacterString = replaceWith; // I changed += to = because we want to replace, not add
}
System.out.print(nextCharacterString);
your_string = your_string + nextCharacterString; // This is where we add the correct character to the string
lastCharacter--;
count++;
}
Upvotes: 1
Reputation: 128
you can use an arraylist
ArrayList<String> list=new ArrayList<String>();
while(lastCharacter >= 0)
{
char nextCharacter = userString.charAt(count);
count ++;
String nextCharacterString = nextCharacter + "";
if (nextCharacterString.equals(replace))
{
nextCharacterString += replaceWith;
}
System.out.print(nextCharacterString);
list.add(nextCharacterString);
lastCharacter --;
}
so you can manipulate it later
Upvotes: 0