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

In Java, write a program (main method) that prompts the user for a “doubled” Str

ID: 3832152 • Letter: I

Question

In Java, write a program (main method) that prompts the user for a “doubled” String, where every
character is immediately duplicated. It prints the un-doubled version. Include a loop so that the user is repeatedly prompted. Below is an example transcript.

Please enter a doubled string:
aabbcc
Undoubled is abc
Do you want to play again? (y/n)
y
Please enter a doubled string:
SShhaarrkk88mmee
Undoubled is Shark8me
Do you want to play again? (y/n)
n

Extend the previous solution so that it verifies that the input is doubled. If so, it behaves as above.
If not, it prints an appropriate error message.

Explanation / Answer

DoubledString.java

import java.util.Scanner;

public class DoubledString {

   public static void main(String[] args) {
       //Scanner to read the input from console
       Scanner in=new Scanner(System.in);
       System.out.println("Please enter a doubled string: ");
      
       //counter to check if entered for first time or not
       int j=0;
       while(in.hasNext()){
           String undoubled="";
           String text=in.next();
           //check if first time entered into while loop
           if(j==0){
               //call method to remove duplicates and return the undoubled String or error message
               undoubled=undouble(text);
               System.out.println(undoubled);
           }else if(text.equalsIgnoreCase("y")){//If not first time check of y to play again
               System.out.println("Please enter a doubled string:");
               String doubled=in.next();
               undoubled=undouble(doubled);
               System.out.println(undoubled);
           }else if(text.equalsIgnoreCase("n")){//If entered value is n
               System.out.println("Bye..");
               in.close();//close the scanner
               System.exit(1);//Exit the application
           }else{
               System.out.println("Enter valid value y/n");
           }
          
          
           System.out.println("Do you want to play again? (y/n)");
           j++;//Increase the counter to make sure the while is not for the first time
       }
      
       in.close();//close the scanner

   }
   public static String undouble(String doubled){
       String undoubled="";
       char[] array=doubled.toCharArray();//convert the string to array of characters
       for(int i=0;i<array.length-1;i=i+2){
           if(array[i]==array[i+1]){//check if the two chars are equal.
               undoubled+=array[i];//assign first char to output
              
           }else{
               return "String "+ doubled+" is not doubled for char "+array[i];//return the error message if does not contain double chars
           }
       }
       return "Undoubled is "+undoubled;//Return undoubled String
   }

}

Sampleoutput:

Please enter a doubled string:
aabbcc
Undoubled is abc
Do you want to play again? (y/n)
y
Please enter a doubled string:
SShhaarrkk88mmee
Undoubled is Shark8me
Do you want to play again? (y/n)
y
Please enter a doubled string:
Test
String Test is not doubled for char T
Do you want to play again? (y/n)
n
Bye..

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