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 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,...
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;...
Ask the user to input a series of numbers, write a C# program to output the...
Ask the user to input a series of numbers, write a C# program to output the sum, max, and min. Be sure to do error checking if the user input is not a number.
Write a java program that will take a line of input and go through and print...
Write a java program that will take a line of input and go through and print out that line again with all the word numbers swapped with their corresponding numeric representations (only deal with numbers from one to nine). Sample runs might look like this: Please enter a line of input to process: My four Grandparents had five grandchildren My 4 grandparents had 5 grandchildren without array and methods.
write a program i java that ask the user to enter salary, user ID and username...
write a program i java that ask the user to enter salary, user ID and username and out put them
Write in javaScript: User input 5 words in one input, and print out the longest word.
Write in javaScript: User input 5 words in one input, and print out the longest word.
Python Program Write a program that will ask a user on how many input colored balls...
Python Program Write a program that will ask a user on how many input colored balls of the following codes: R-red, B-blue, W-white, G-green and O-orange -will he or she would like to enter in the program and print the total number of Red balls were encountered. Assume an uppercase and lower case letter will be accepted.
Write a complete Java program to print out the name bob
Write a complete Java program to print out the name bob
Write a Python program which prompts the user to input a string. Then, print the string...
Write a Python program which prompts the user to input a string. Then, print the string in reverse to the terminal Sample output Please enter a word: "zeus" The reverse of zeus is suez Hint: There are several ways to accomplish this. Recall that a string is an itterable object and therefore can be used with a for loop
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT