Question

In: Computer Science

Use the Main.java file below to read in phrases (entire lines!) and print underscores "_" for...

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:

__ _____
___ __
____ ____ ____

Solutions

Expert Solution

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:


Related Solutions

You have to write a program that will read an array from a file and print...
You have to write a program that will read an array from a file and print if the numbers in the file are right truncatable primes. A right truncatable prime is a prime number, where if you truncate any numbers from the right, the resulting number is still prime. For example, 3797 is a truncatable prime number number because 3797, 379, 37, and 3 are all primes. Input-Output format: Your program will take the file name as input. The first...
search the file "myfile.txt" for any occurrences of "Some target string" and print the lines where...
search the file "myfile.txt" for any occurrences of "Some target string" and print the lines where the string is found. In this assignment, write your own java version of this program: Name the program LastnameFirstname1 (this is not really an appropriate name for such a program, but following this convention greatly eases the grading process). Example: WhiteMax1 Allow the user to specify the string they are searching for and the file they wish to search with command line arguments. To...
Design a program that will read each line of text from a file, print it out...
Design a program that will read each line of text from a file, print it out to the screen in forward and reverse order and determine if it is a palindrome, ignoring case, spaces, and punctuation. (A palindrome is a phrase that reads the same both forwards and backwards.) Example program run: A Toyota's a Toyota atoyoT a s'atoyoT A This is a palindrome! Hello World dlroW olleH This is NOT a palindrome! Note: You are only designing the program...
1)     The provided sched.cpp file is missing a few lines to read in some of the data....
1)     The provided sched.cpp file is missing a few lines to read in some of the data. The comments in the code describe how there is yet another stream library that works like iostream and fstream, but for processing string’s. Fill in the necessary statements and uncomment the indicated cout statements to get the output below for the attached “sched.txt” file: CS100 Section 1 has 17 open seats It is held on MW from 8:00A to 9:15A in SC-S146 It is...
How to read and print all contents in a binary file using a Binary Search Tree...
How to read and print all contents in a binary file using a Binary Search Tree inorder traversal in C. Additionally, how to search using a Binary Search Tree to display the specific Athlete and his/her information. An example would be a sports record binary file that consist of name of athlete, height , weight, championships won. Athlete: Michael Jordan Height: 6’6” Weight : 205 lbs Championships won: 6 =================== Athlete: LeBron James Height: 6’8” Weight: 250 lbs Championships won:...
For the following questions, list the entire path for the indicated file. If you use the...
For the following questions, list the entire path for the indicated file. If you use the variables ORACLE_BASE or ORACLE_HOME, be sure to list their values as well. Find the location of the instance of your database (ORCL). Find the location of the file init.ora . What purpose does this file serve? Find the location of the files listener.ora and tnsnames.ora. Identify which file is on the client and which file is on the server. Find the location of the...
C++ Write a whole program to read 50 salaries from the file “Salaries.txt” and print them...
C++ Write a whole program to read 50 salaries from the file “Salaries.txt” and print them on the screen (each value on a separate line), you have to also save the grades (each value on a separate line) into another file “SalariesWithBonus.txt” after you add a bonus of two percent to each salary (example For the salary 100000 a bonus of 2000 will be added as 100000 X 0.02 = 2000). Your code should check whether the file “Salaries.txt” opened...
Visit this url link below in the Federal Reserve website and read the entire transcript of...
Visit this url link below in the Federal Reserve website and read the entire transcript of the speech given given by the Fed’s Chair Janet Yellen on October 20, 2017 at the National Economics Club regarding the US monetary policy perspective in the context of the major challenges in the decade ahead that the Fed is facing. After highlighting her key points on nonconventional monetary policy measures, critically give her key recommendations for monetary policy to cope up with future...
Consider the Happy Cruise Lines Sailor file shown below. It lists all of the sailors on...
Consider the Happy Cruise Lines Sailor file shown below. It lists all of the sailors on the company’s cruise ships by their unique sailor identification number, their name, the unique identification number of the ship they currently work on, their home country, and their job title. Sailor Number Sailor Name Ship Number Home Country Job Title 1 00536 John Smith 009 USA Purser 2 00732 Ling Chang 012 China Engineer 3 06988 Maria Gonzalez 020 Mexico Purser 4 16490 Prashant...
MATLAB ONLY please. Please put the entire code below. 1. you will read a list of...
MATLAB ONLY please. Please put the entire code below. 1. you will read a list of internet logs from a notepad. 2. then you will extract the following. - a host (e.g., '146.204.224.152') - a user_name (e.g., 'feest6811' note: sometimes the username is missing! In this case, use '-' as the value for the username.) - the timme a request was made (e.g., '21/Jun/2019:15:45:24-0700') - the post request type (e.g., 'POST /incentivize HTTP/1.1' note: not everthing is a POST!) Your...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT