Write an application that prompts the user for three first names and concatenate
ID: 674921 • Letter: W
Question
Write an application that prompts the user for three first names and concatenates them in every possible two-name combination so that new parents can easily compare them to find the most pleasing baby name. Save the file as BabyNameComparison.java. Write an application that allows a user to enter two Strings. Output the number of characters in the first String that also appear in the second String, and output those characters. Figure 7-20 shows two typical executions. Save the file as CountMatches.java.
Explanation / Answer
import java.util.Scanner;
public class BabyNameComparison {
public static void main(String[] args){
Scanner userInputScanner = new Scanner(System.in);
System.out.print("Enter 1st FirstName");
String name1 = userInputScanner.nextLine();
System.out.print("Enter 2nd First Name");
String name2 = userInputScanner.nextLine();
System.out.print("Enter 3rd First Name");
String name3 = userInputScanner.nextLine();
System.out.println(name1 + "" + name2);
System.out.println(name1 + "" + name3);
System.out.println(name2 + "" + name1);
System.out.println(name2 + "" + name3);
System.out.println(name2 + "" + name1);
System.out.println(name3 + "" + name2);
}
}
import java.util.Scanner;
public class CountMatches {
public static void main(String[] args) {
Scanner inp= new Scanner(System.in);
String str;
char ch;
int count=0;
System.out.println("Enter the string:");
str=inp.nextLine();
while(str.length()>0)
{
ch=str.charAt(0);
int i=0;
while (i < str.length() && str.charAt(i) == ch)
{
count++;
i++;
}
str = str.substring(count);
System.out.println(ch);
System.out.println(count);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.