Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

What do I add to this code to exit the program when the user enter \"done\"? imp

ID: 3584961 • Letter: W

Question

What do I add to this code to exit the program when the user enter "done"?

import java.util.*;// import this library to invoke scanner class
public class ReverseString{

public static void main(String args[])
{
String original, reverse = "";
Scanner in = new Scanner(System.in); // invoke scanner class to read user input

System.out.println("Enter a string to reverse");
original = in.nextLine(); //store original string

int length = original.length();// get the string length
// this forloop scans the string from end (length - 1) to begining (0) to get the reverse string and prints each //charecter
for ( int i = length - 1 ; i >= 0 ; i-- )
reverse = reverse + original.charAt(i);//charAt funtion is used to convert the string to char

System.out.println("Reverse of entered string is: "+reverse);

System.out.println("Enter a string to reverse");
original = in.nextLine(); //store original string


// this forloop scans the string from end (length - 1) to begining (0) to get the reverse string and prints each //charecter
for ( int i = length - 1 ; i >= 0 ; i-- )
reverse = reverse + original.charAt(i);//charAt funtion is used to convert the string to char

System.out.println("Reverse of entered string is: "+reverse);

}
  
}

Explanation / Answer

ReverseString.java

import java.util.*;// import this library to invoke scanner class

public class ReverseString{

public static void main(String args[])

{

String original, reverse = "";

Scanner in = new Scanner(System.in); // invoke scanner class to read user input

System.out.println("Enter a string to reverse");

original = in.nextLine(); //store original string

int length = original.length();// get the string length

// this forloop scans the string from end (length - 1) to begining (0) to get the reverse string and prints each //charecter

while(!original.equalsIgnoreCase("done")) {

reverse= "";

for ( int i = length - 1 ; i >= 0 ; i-- )

reverse = reverse + original.charAt(i);//charAt funtion is used to convert the string to char

System.out.println("Reverse of entered string is: "+reverse);

System.out.println("Enter a string to reverse");

original = in.nextLine(); //store original string

}

}

  

}

Output:

Enter a string to reverse
abcd
Reverse of entered string is: dcba
Enter a string to reverse
erty
Reverse of entered string is: ytre
Enter a string to reverse
done

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote