In: Computer Science
Using the provided code (found here), write a program using the main method where the user enters Strings and the program echoes these strings to the console until the user enters “quit”. When user quits the program should print out, “Goodbye”. You may assume that the case is ignored when the user enters, “quit”, so “quit”, “QUIT”, “Quit”,“qUiT”, etc. are all acceptable ways to end the program. The results should be printed out to the console in the following format:
<<Input00>>
“Echo: ”<<Input00>>
<<Input01>>
“Echo: ”<<Input01>>
…
“Quit”
“Goodbye”
Values denoted in “<< >>” represent variable values, and strings in quotations denote literal values (make sure to follow spelling, capitalization, punctuation, and spacing exactly).
Solution Tests:
Echo: Hello!
Echo: Greetings!
Echo: Salutations!
Goodbye
(This only shows the program’s output and has omitted the greeting message and the user’s input in the console)
Echo: 1
Echo: 2
Echo: 3
Echo: 4
Echo: 5
Goodbye
(This only shows the program’s output and has omitted the greeting message and the user’s input in the console)
Echo: STOP
Echo: end
Echo: Halt
Goodbye
(This only shows the program’s output and has omitted the greeting message and the user’s input in the console)
/Do not alter----------------------------------------------------------------------- import java.util.Scanner; public class Question02 { public static void main(String[] args) { Scanner keyboard; if(args == null || args.length == 0) { keyboard = new Scanner(System.in); System.out.println("Welcome to the Echo Program! I'll echo whatever is said until you enter \"quit\""); } else { keyboard = new Scanner(args[0]); } //----------------------------------------------------------------------------------- //Write your solution here }//Do not alter this //Space for other methods if necessary----------------------------------------------- //Write those here if necessary //----------------------------------------------------------------------------------- }//Do not alter this /*Solution Description * */
import java.util.Scanner;
public class Question2 {
/*
* @author name
*/
public static void main(String[] args) {
Scanner keyboard;
if(args == null || args.length == 0)
{
keyboard = new Scanner(System.in);
System.out.println("Welcome to the Echo Program! I'll echo whatever is said until you enter \"quit\"");
}
else
{
keyboard= new Scanner(args[0]);
}
String s=null;
while(true) {
s=keyboard.next();
if(s.equalsIgnoreCase("Quit")) {
System.out.println("Goodbye");
break;
}
System.out.println("Echo:"+s);
}
}//Do not alter this
//Space for other methods if necessary-----------------------------------------------
//Write those here if necessary
//-----------------------------------------------------------------------------------
}//Do not alter this
/*Solution Description
*
* In this code I just take a while loop while always true inside it
* it will make your loop running continuously and you can break it anytime you want
* SO i put base condition i.e. if I enter quit then it'll break from the loop else
* it'll print the echo the same string which you entered.
*/
Yes the above code is compiled I'll attach the screenshot of output with the code.
I hope you liked the solution If you do so then support us by pressing upvote button.