I am new to programming and this is my first semester in programming introduction class. I am working on this project and came across few problems. I am only limited to use string methods like (length, concat, +, charAt, substring, and equals (or equalsIgnoreCase)) and I am prohibited to use StringBuilder, indexOf, Arrays. Any help would be very much appreciated...as I am just lost.
Here is my code that manipulates an entered string in various ways. I have made it to loop the whole process until the user enters "quit". When looping through consecutively, the resulting string is doubled. It seems like the result from the first loop is printed altogether with strings modified in the next loops. The string does not need to be cleared and reset but, it has to be kept modified by user until quitted.
GOOD EXAMPLE: 1st loop:"esrever"(word reverse is reversed) and 2nd loop: "srvr" (all char e is removed) and so on.
There are two problems that I am facing:
when the do-while loop is repeated, the command prompt
System.out.println("Enter your command (reverse, replace first, replace last, remove all, remove, quit)");
sometimes prints double. can you tell me what is wrong with what I am doing?when the do-while loop is repeated, the new result is concatenated to the result right before. for example, when I reverse my string "reverse", I get "esrever" and when the do while produces second result of reverse, the newly reversed string is concatenated before "esrever" resulting in, "esreveresrever"
I googled and found out that clearing my scanner would solve, so I added keyboard.nextLine();
like this:
do { System.out.println("Enter your command (reverse, replace first, replace last, remove all, remove, quit)"); command = keyboard.nextLine(); keyboard.nextLine();
but this did not solve my problem. Instead, it was creating an unnecessary prompt to press enter to proceed. What is wrong with my method?
Attempted to solve
do {
System.out.println("Enter your command (reverse, replace first, replace last, remove all, remove, quit)");
command = keyboard.nextLine();
}while(!command.equalsIgnoreCase("quit"));
Attempted to solve by adding nextLine
do {
System.out.println("Enter your command (reverse, replace first, replace last, remove all, remove, quit)");
command = keyboard.nextLine();
keyboard.nextLine();
}while(!command.equalsIgnoreCase("quit"));
This is my whole code
import java.util.Scanner;
public class StringFun {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
String originalString;
String modifiedString="";
String command="";
char enterReplaceChar;
char enterNewChar;
char enterRemoveChar;
int selectedCharToRemove=0;
int whichToRemove;
int stopOrGo=0;
System.out.println("Enter the string to be manupulated");
originalString = keyboard.nextLine();
do {
System.out.println("Enter your command (reverse, replace first, replace last, remove all, remove, quit)");
command = keyboard.nextLine();
//reverse command
if (command.equalsIgnoreCase("reverse")) {
for (int i = originalString.length()-1; i>=0; i--) {
modifiedString = modifiedString + originalString.charAt(i);}
System.out.println("The new sentence is: " + modifiedString);}
//replace first command
else if (command.equalsIgnoreCase("replace first")) {
System.out.println("Enter the character to replace");
enterReplaceChar = keyboard.next().charAt(0);
System.out.println("Enter the new character");
enterNewChar = keyboard.next().charAt(0);
for(int i=0; originalString.length() > i ;i++) {
if (stopOrGo < 1)
if (originalString.charAt(i)== enterReplaceChar) {
stopOrGo++;
modifiedString = modifiedString + enterNewChar;}
else {modifiedString = modifiedString + originalString.charAt(i);}
else {modifiedString = modifiedString + originalString.charAt(i);}}
if (modifiedString.equals(originalString)) {
System.out.println("The letter was not found in the word");
}else {
System.out.println("The new sentence is:"+ modifiedString);}
}
else if (command.equalsIgnoreCase("replace last")) {
System.out.println("Enter the character to replace");
enterReplaceChar = keyboard.next().charAt(0);
System.out.println("Enter the new character");
enterNewChar = keyboard.next().charAt(0);
for(int i=originalString.length()-1; i >= 0 ;i--) {
if (stopOrGo < 1)
if (originalString.charAt(i)== enterReplaceChar) {
stopOrGo++;
modifiedString = enterNewChar + modifiedString;}
else {modifiedString = originalString.charAt(i) + modifiedString;}
else {modifiedString = originalString.charAt(i) + modifiedString;}}
if (modifiedString.equals(originalString)) {
System.out.println("The letter was not found in the word");
}else {
System.out.println("The new sentence is:"+ modifiedString);}
}
//remove all loop
else if (command.equalsIgnoreCase("remove all")) {
System.out.println("Enter the character to remove");
enterRemoveChar = keyboard.next().charAt(0);
for(int i=0; originalString.length() > i ;i++)
if (originalString.charAt(i)== enterRemoveChar) {
modifiedString = modifiedString + originalString.substring(i,i);}
else {modifiedString = modifiedString + originalString.substring(i,i+1);}
if (modifiedString.equals(originalString)) {
System.out.println("Error: the letter you are trying to remove does not exist");
}else {
System.out.println("The new sentence is:"+ modifiedString);}
}
//remove selected loop
else if (command.equalsIgnoreCase("remove")) {
System.out.println("Enter the character to remove");
enterRemoveChar = keyboard.next().charAt(0);
System.out.println("Enter the " + enterRemoveChar + " you would like to remove (Not the index-1=1st, 2=2nd, etc.):");
whichToRemove = keyboard.nextInt();
for(int i=0; originalString.length() > i ;i++) {
if (originalString.charAt(i)==enterRemoveChar) {
selectedCharToRemove++;
if (selectedCharToRemove==whichToRemove) {
modifiedString = modifiedString + originalString.substring(i,i);
}
else {modifiedString = modifiedString + originalString.substring(i,i+1);}}
else {modifiedString = modifiedString + originalString.substring(i,i+1);}}
if (modifiedString.equals(originalString) || (whichToRemove > selectedCharToRemove)) {
System.out.println("Error: the letter you are trying to remove does not exist");
}else {
System.out.println("The new sentence is:"+ modifiedString);}
}
}while(!command.equalsIgnoreCase("quit"));
System.out.println("...Execution Ends...");
keyboard.close();
System.exit(0);
} }