0

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.

2
  • 2
    So what is your problem? If you just want to save to a variable instead of printing, just replace System.out.print(...) with myVariable = myVariable + ... with myVariable being a String Commented Oct 5, 2017 at 21:35
  • Where are lastCharacter and userString defined? Commented Oct 5, 2017 at 21:38

2 Answers 2

1

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++;
}
Sign up to request clarification or add additional context in comments.

Comments

0

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

2 Comments

Why add them to an ArrayList when we have an entire library devoted to strings?
Jake stated that he needs to manipulate the strings later. So adding them to the list would do that. There could be many solutions though

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.