Question

In: Computer Science

Write java program that will ask for the user for 2 input lines and print out...

Write java program that will ask for the user for 2 input lines and print out all words that occur 1 or
more times on both lines (case sensitive). Write this without arrays and method. Here is a sample run:
<Output>
Enter two lines to process.
The quick brown fox jumps over a lazy dog
The fox hound outruns the lazy dog
The words that occur on both lines are: The fox lazy dog

Solutions

Expert Solution

Java Code:

import java.io.*;
import java.util.Arrays;
import java.util.ArrayList; 

class WordsCommon
{
public static void main (String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader (System.in));
int i,j;

System.out.print("Enter the first sentence: ");
String str1 = br.readLine();
System.out.print("Enter the second sentence: ");
String str2 = br.readLine();


//Store all the words of the first sentence in a string array
String[] words1 = str1.split("\\s+");

//Store all the words of the second sentence in a string array
String[] words2 = str2.split("\\s+");

//Converting array into array list
ArrayList<String> array1= new ArrayList<String>(Arrays.asList(words1));
ArrayList<String> array2= new ArrayList<String>(Arrays.asList(words2));

//Find common elements
array1.retainAll(array2); 
  

String s = String.join(" ", array1);
System.out.println("The words that occurs on both lines are: "+ s);

}
}

Output:

Enter the first sentence: The quick brown fox jumps over a lazy dog
Enter the second sentence: The fox hound outruns the lazy dog
The words that occurs on both lines are: The fox lazy dog

How to run the code:

1) Copy paste the code in notepad and save the file as "WordsCommon.java".

2) Go to command prompt, go to the directory where the file is saved and compile the java code by typing "javac WordsCommon.java"

3) Then run the code by typing "java WordsCommon"

4) Then input the two sentences

5) Output will be generated


Related Solutions

Write a program to find the prime numbers IN JAVA Ask user to input the integer...
Write a program to find the prime numbers IN JAVA Ask user to input the integer number test the number whether it is a prime number or not Then, print “true” or “false” depending on whether the number is prime or isn’t. Hint: number is prime when is has exactly 2 factors: one and itself. By this definition, number 1 is a special case and is NOT a prime. Use idea of user input, cumulative sum, and loop to solve...
2. Write a program to do the following: • ask the user to input the size...
2. Write a program to do the following: • ask the user to input the size of an array of integers and allocate space for the array • ask the user to input the integer values for the array and store these values into the array • calculate and display the sum and the average of elements of the array
Write a program that will ask for the user to input a filename of a text...
Write a program that will ask for the user to input a filename of a text file that contains an unknown number of integers. And also an output filename to display results. You will read all of the integers from the input file, and store them in an array. (You may need to read all the values in the file once just to get the total count) Using this array you will find the max number, min number, average value,...
Write Java program that asks a user to input a letter, converts the user input to...
Write Java program that asks a user to input a letter, converts the user input to uppercase if the user types the letter in lowercase, and based on the letter the user the user enters, display a message showing the number that matches the letter entered. For letters A or B or C display 2 For letter D or E or F display 3 For letter G or H or I display 4 For letter J or K or L...
Create a python program that will ask the user for his job title and print out...
Create a python program that will ask the user for his job title and print out the first letter, the last letter and the total number of letters in his job
Write a Python program that will ask the user to input a word, will return the...
Write a Python program that will ask the user to input a word, will return the first letter of the input word, and ask the user to put another word, and so on, in the form of a loop. If the user chooses to stop, he or she should input the integer "0" for the loop to stop.
Write a java program. The program requirements are: the program will let the user input a...
Write a java program. The program requirements are: the program will let the user input a starting number, such as 2.  It will generate ten multiplication problems ranging from 2x1 to 2x10.  For each problem, the user will be prompted to enter the correct answer. The program should check the answer and should not let the user advance to the next question until the correct answer is given to the question being asked currently. After testing a total of 10 multiplication problems,...
In Java, I need a program that will ask a user to input how many tests...
In Java, I need a program that will ask a user to input how many tests they want the average of. For example, It needs to ask the user how many tests would you like the average of? When the user enters the amount of tests they want the average score of, the program needs to give them that many test scores to input. Then with the average, determine what their grade is, if 90-100=A if 80-90 = B etc....
The JAVA program should do the following: –Ask the user for how many lines of text...
The JAVA program should do the following: –Ask the user for how many lines of text they wish to enter –Declare and initialize an array of Strings to hold the user’s input –Use a while loop to prompt for and read the Strings (lines of text) from the user at the command line. Typically, this would be a 'for' loop since we know the number of times to execute the loop based upon the number supplied by the user, but...
Write a program in C, that uses standard input and output to ask the user to...
Write a program in C, that uses standard input and output to ask the user to enter a sentence of up to 50 characters, the ask the user for a number between 1 & 10. Count the number of characters in the sentence and multiple the number of characters by the input number and print out the answer. Code so far: char sentence[50]; int count = 0; int c; printf("\nEnter a sentence: "); fgets(sentence, 50, stdin); sscanf(sentence, %s;    for(c=0;...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT