I'm not allowed to use the replace() method and I basically have to have a part of my program that does this:
-Ask the user to type in a sentence to be manipulated. -Then ask the user to enter which character to be replaced (i.e. "e" from "hello"). -Then ask which character they would like to it to be replaced with.
After it does all of this, it will output to the screen the manipulated string.
EXAMPLE:
- console: Please input a string:
- user: Hi there
- console: please enter the character to be replaced:
- user: e
- console: please enter a character to replace it with:
- user: l
- console: the new string is: Hi thlrl
I basically am able to do this, using this code:
int count = 1;
char replaceAll = 'a';
char newChar = 'b';
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter the string to be manipulated");
String sentence = keyboard.nextLine();
int count = 0;
System.out.println("Enter the character to replace");
replaceAll = keyboard.nextLine().charAt(0);
System.out.println("Enter the new character");
newChar = keyboard.nextLine().charAt(0);
System.out.print("The new sentence is: ");
for(int c = 0; c < sentence.length(); c++){
if(sentence.charAt(c) == replaceAll)
System.out.print(newChar);
else
System.out.print(sentence.charAt(c));
MAIN PROBLEM: The only problem is that this won't save the manipulated string as a new string. It will only print the letters in the correct order for output (it prints one character at a time, not one string). I need the manipulated string (i.e. Hi thlrl) to be saved as a new string so that I can do more manipulations afterwards to that new string.