In: Computer Science
Use the Main.java file below to read in phrases (entire lines!) and print underscores "_" for each character. Main.java already imports the Scanner class, creates your Scanner object, and reads in the first phrase from the keyboard. Your task is as followed:
import java.util.Scanner;
public class Main {
public static void main(String[] args)
{
Scanner keyboard = new
Scanner(System.in);
String phrase =
keyboard.nextLine();
//Your code here!
}
}
Write a nested loop that allows the user to continuously enter phrases and output underscores for each character of the phrase. End when the user enters "done" (ignoring case).
Ex 1 - Sample Input:
banana done
Output:
______
Step 2: Once you have this completed, modify your code so that if the character is whitespace, you print a space " " instead of an underscore - Hint: use Character.isWhitespace(YOURCHARHERE) //returns a boolean.
Ex 2 - Sample input:
Go Dawgs Sic em Woof Woof Woof dOnE
Output:
__ _____ ___ __ ____ ____ ____
Code for step1:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
String phrase = keyboard.nextLine();
while(!phrase.toLowerCase().equals("done")) {
for (int i = 0; i < phrase.length(); i++) {
System.out.print("_");
}
System.out.println();
phrase = keyboard.nextLine();
}
}
}
output for step1:
Code screenshot for step1:
Step2:
Code:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
String phrase = keyboard.nextLine();
while (!phrase.toLowerCase().equals("done")) {
for (int i = 0; i < phrase.length(); i++) {
if (Character.isWhitespace(phrase.charAt(i))) {
System.out.print(" ");
} else {
System.out.print("_");
}
}
System.out.println();
phrase = keyboard.nextLine();
}
}
}
Output:
Code Screenshot: