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...
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...
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 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.
JAVA: Write a program that prompts the user to input a type of medication and the...
JAVA: Write a program that prompts the user to input a type of medication and the output will be a list of side effects that can occur from that medication.
Write a complete Java program to print out the name bob
Write a complete Java program to print out the name bob
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT