Question

In: Computer Science

This is a java coding String manipulation Question. Let us say I have a String which...

This is a java coding String manipulation Question.

Let us say I have a String which looks something likes this:

String x = "MATH,PHYSICS,CHEMISTRY,CODING"

And then I have another String which looks something like this:

String y = "XXXXMXXXAXXXTXXXXHXXXXXXCXXXXOXXXXDXXXIXXXXNXXXGXXXX"

and another String that looks like this:

String z = "XXXXHXXTXXXXAXXMXXXXXCXXXOXXXDXXXIXXXNXXXG"

I want to take String y and print the words that are found in String x as such:

Math

Coding

and for String z if I want to take it and print the words also found in x as such:

Math

Coding

So basically I want to the program to print the possible words from the letters I have in String y and z.

and only print possible words from my String x.

I want to know how I can do such thing please explain so that I understand what is going on. I have tried replacing the Xs with nothing and then I would have the letters and then I'll use it to check if it has something in String x. but that made me use arrays and also if I replaced Xs with nothing and I had to words in Z or Y then I'll end up with something I can not work with only letters.

I have also thought about making a char array from the resulting String after replacing Xs. and then split String x by ',' and after make a char array of each word and then check if the char array of each word contains the chars of the resulting String after replacing Xs with nothing.

but that also does not work.

I think this is a simple problem that I am over complicating.

Please help and thank you.

Solutions

Expert Solution

/******************************************************************************

Online Java Compiler.
Code, Compile, Run and Debug java program online.
Write your code in this editor and press "Run" button to execute it.

*******************************************************************************/

public class Main
{
   public static void main(String[] args) {
       String x = "MATH,PHYSICS,CHEMISTRY,CODING";
       String y = "XXXXMXXXAXXXTXXXXHXXXXXXCXXXXOXXXXDXXXIXXXXNXXXGXXXX";
       String z = "XXXXHXXTXXXXAXXMXXXXXCXXXOXXXDXXXIXXXNXXXG";
      
       String wordsInX[] = x.split(",");
       System.out.println("In String y the words that are found in String x as such:");
       printWords(wordsInX,y);
       System.out.println("\nIn String z the words that are found in String x as such:");
       printWords(wordsInX,z);
   }
   public static void printWords(String array[],String inputString)
   {
   //traversing to each word to check weather that word is present in that string or not
   for(String word:array)
       {
       boolean flag = false;
       //in that word checking each character is present in that input string or not
       for(int i=0;i<word.length();i++)
       {
       if(inputString.contains(word.charAt(i)+""))
       {
       flag = true;
       }
       else
       {
       flag = false;
       break;
       }
       }
       //if each character in that word is present in input string printing that word to console
       if(flag)
       {
       System.out.println(word);
       }
       }
   }
}


Related Solutions

Java Question I have a Queue containing String type taking in a text file with an...
Java Question I have a Queue containing String type taking in a text file with an iterator. I need to use ArrayList and HashMap for freqHowMany to get a frequency table in descending order by the highest frequency words in the text. Any help would be much appreciated and thanks! final Iterator input = new Scanner(System.in).useDelimiter("(?U)[^\\p{Alpha}0-9']+"); final Queue queueFinal = new CircularFifoQueue<>(wordsLast); while (input.hasNext()) { final String queueWord = input.next(); if (queueWord.length() > minimumLength) { queueFinal.add(queueWord); // the oldest item...
using java language "Data Structures" I have to write program which stores string entered by user...
using java language "Data Structures" I have to write program which stores string entered by user into cursor array implementation here is the code public static void main(String[] args) { CursorArray sorted =new CursorArray();//the strings must added here how can i store them                  String []inputs = new String[50];                   for (int i=0; i< 50; i++) {            System.out.println("Enter the words you want to sort and use exit to stop");...
JAVA coding language: Consider the method getAllCharAsString: public statis String getAllCharAsString(String inputStr, char target) { }...
JAVA coding language: Consider the method getAllCharAsString: public statis String getAllCharAsString(String inputStr, char target) { } The method accepts a String parameter, inputStr, and a char parameter, target. It returns the result string consisting of all the characters in inputStr which match the target character (matching is case sensitive, for example, 'c' does not match "C"). If the characters match, the character is appended to the result String. After all the characters in inputStr have been compared, the method returns...
2) Let us say that the protectionism of the coal industry is in the form of...
2) Let us say that the protectionism of the coal industry is in the form of a tariff. Talk on the benefits and costs of a tariff. Do the benefits exceed the costs or vise versa? Ascertain you include the terms of trade. Also, what is an optimal tariff?
Question for Java String[] tokens = expression.split(" "); for (String token : tokens) { if (token.equals("+")...
Question for Java String[] tokens = expression.split(" "); for (String token : tokens) { if (token.equals("+") || token.equals("-")) { while (!ops.isEmpty() && (ops.peek() == '+' || ops.peek() == '-' || ops.peek() == '*' || ops.peek() == '/')) {    applyOp(values, ops); } What does the for (String tokens : token) mean? What are the different ways to write this for loop?
Machine Learning - multivariate methods Let us say in two dimensions, we have two classes with...
Machine Learning - multivariate methods Let us say in two dimensions, we have two classes with exactly the same mean. What type of boundaries can be defined? show a picture of the options
JAVA JAVA JAVA . I need to convert a string input to int array, for example...
JAVA JAVA JAVA . I need to convert a string input to int array, for example if user enters 12 / 27 / 2020 , I want to store each value in a separate array and add them afterwards.
Researchers are interested in the mean age of a certain population. Let us say that they...
Researchers are interested in the mean age of a certain population. Let us say that they are asking the following questions: can we conclude that the mean age of this population is different from 30 years? If the sample mean of 10 individuals drawn from that population is 27and the population variance is 20. Make a confidence interval of the mean A. 1.96 , -1.96 B. 24.23 ,29.20 C. 24.23 , 29.77 D. 2.77 , -2.77
<Python coding question string practice> Can anyone please answer these python string questions?? 1. Write a...
<Python coding question string practice> Can anyone please answer these python string questions?? 1. Write a function called username that takes as parameters a person's first and last names and prints out his or her username formatted as the last name followed by an underscore and the first initial. For example, username("Martin", "freeman") should print the string "freeman_m". 2. Write a function called letters that takes as a parameter a string and prints out the characters of the string, one...
This is a question about coding in python. I think that the question is asking me...
This is a question about coding in python. I think that the question is asking me to code the following: Implement the scalar product of 2 vectors in 2-space, using two tuple parameters (the function scalarProduct2). Write a docstring; then write some test function calls (can I pick any data for this? - the question gave these examples data examples: (1., 1.) dot (2,3) = (1.,1.) dot (2,0) = (1., 1.) dot (0,2) = (1., 1.,) dot (4,5) = );...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT