The program should ask the user for a last name, a unit of measurement and a veg
ID: 3870243 • Letter: T
Question
The program should ask the user for a last name, a unit of measurement and a vegetable and then assemble these into a sentence. Use the Scanner to read in user input.
Once you have the happy path implemented then consider various errors that may occur. For example, if spaces are entered at the beginning or ending of an answer, then remove them.
For the last name, the first character should be upper case and the rest lower case, now matter which case was used when entering the last name. Also, the user may enter multiple word last names, but for our assignment only the first character of the first word should be converted to upper case. The unit of measurement or the vegetable may be multiple words.
Write and call the following properCase method:
Suggestion: Methods in the Scanner class (next() and nextLine()), String class (trim(), substring(), toUpperCase(), charAt(), etc.) and Character class may be helpful.
IMPORTANT: Input: Solomon Fett
foot
pumpkin
Expceterd output: Enter your last name: Enter a unit of measurement: Enter the name of a vegetable: A tongue twister: Peter Solomon fett picked a foot of pickled pumpkins.
Explanation / Answer
import java.util.Scanner;
public class Tongue_Twister {
public static void main(String[] args) {
String lastName,unit,vegitable;
Scanner scan = new Scanner(System.in);
System.out.println("Enter your last name:");
lastName=scan.nextLine();
System.out.println("Enter a unit of measurement:");
unit =scan.nextLine();
System.out.println("Enter the name of a vegetable:");
vegitable =scan.nextLine();
String output =properCase(lastName);
System.out.println("A tongue twister: Peter "+output+" picked a "+unit+" of pickled "+vegitable+"s.");
scan.close();
}
public static String properCase(String name)
{
String str=name.trim();
return str.substring(0, 1).toUpperCase()+str.substring(1).toLowerCase();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.