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

The objective is to read the last five lines of a file (text file) in Perl....
The objective is to read the last five lines of a file (text file) in Perl. I wrote down what I have written so far in Perl but I get an error message saying, "Can't open sample.txt: No such file or directory." The sample.txt file is located in the same directory that I am executing the perl scripts on the command line. sample.txt ============================================================= My name is Jennifer and I like to eat tacos de birria . =========================================================== #!/usr/bin/perl $filename=...
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...
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...
Write a C program to run on unix to read a text file and print it...
Write a C program to run on unix to read a text file and print it to the display. It should count of the number of words in the file, find the number of occurrences of a substring, and take all the words in the string and sort them (ASCII order). You must use getopt to parse the command line. There is no user input while this program is running. Usage: mywords [-cs] [-f substring] filename • The -c flag...
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...
Make a program to read a file, filled with words separated by new lines, from command...
Make a program to read a file, filled with words separated by new lines, from command line arguments (args[0]), print the file, and then add each word into an arraylist. After making the arraylist, iterate through it and print its contents.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT