JAVA..The program should ask the user for a last name, a unit of measurement and
ID: 3869891 • Letter: J
Question
JAVA..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.
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.
Explanation / Answer
ProperCaseTest.java
import java.util.Scanner;
public class ProperCaseTest {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter your last name: ");
String lastName = scan.next();
System.out.print("Enter a unit of measure: ");
String measure = scan.next();
System.out.print("Enter a name of vegetable: ");
String veg = scan.next();
System.out.println("A tonque twister: Peter "+lastName+" picked a "+measure+" of pickle "+veg);
scan.nextLine();
System.out.println("Enter a name: ");
String name = scan.nextLine();
System.out.println("Proper Case: "+properCase(name));
}
public static String properCase(String name) {
name = name.trim();
char ch = name.charAt(0);
name = name.substring(1);
return String.valueOf(ch).toUpperCase()+name;
}
}
Output:
Enter your last name: Zhang
Enter a unit of measure: meter
Enter a name of vegetable: pumpkin
A tonque twister: Peter Zhang picked a meter of pickle pumpkin
Enter a name:
hello How are you
Proper Case: Hello How are you
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.