Write a program which takes two strings as input from the us

Write a program which takes two strings as input from the user. Ask the user to enter names in the first string and foods in the second string. In both the strings the names and foods are separated by space. Number of items in both lists can be different. Example Input Sequence Alpha Bravo Charlie Delta Echo Pizza Burger Fries Tacos Pasta Write a function creates0 which takes the above2 strings and 2 integers as arguments.

Solution

solution

package com.gp.classes;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Testing {
//initialization
   static String generatedString;
   static Scanner scanner = new Scanner(System.in);
  
   public void create(String names, String food, int i, int j, String ans) {
       String finderName = null;
       String finderFood = null;
       String[] name = names.split(\" \");
//for spliting based on spaces
       String[] foods = food.split(\" \");

       for (int k = 0; k < name.length; k++) {
           if (i == k) {
               finderName = name[k];
           }
       }
       for (int k = 0; k < food.length(); k++) {
           if (j == k) {
               finderFood = foods[k];
           }
       }

       generatedString = finderName + \" \" + \"loves\" + \" \" + finderFood;
       System.out.println(\"the concat word is\" + generatedString);
   }

   public static void findAndReplace(String generatedString, String match,
           String replace) {
       String[] words = generatedString.trim().split(\"\\\\s+\");
       if (words[1].equals(match)) {
           for (int i = 0; i < words.length; i++) {
               if (match.equals(words[i])) {
                   words[i] = replace;
                   System.out.println(words[i]);
               }
           }
           System.out.println(words[0] + \" \" + words[1] + \" \" + words[2]);
       } else
           System.out.println(\"string not found\");

   }

   /**
   * @param args
   */
   public static void main(String[] args) {
       int count = 0;
       int count1 = 0;

       String ans = null;

       Testing test = new Testing();

       System.out.println(\"enter the names by giving the space sequently\");

       String names = scanner.nextLine();
       System.out
               .println(\"enter the food by giving the space sequently otherwise it won\'t work\");
       String food = scanner.nextLine();

       String[] name = names.split(\" \");

       String[] foods = food.split(\" \");

       for (String word : name) {
           count++;
       }

       int nameslength = count - 1;
       for (String word : foods) {
           count1++;
       }

       int foodlength = count1 - 1;

       System.out
               .println(\"enter your choice not exceed this number in names\\t\"
                       + nameslength);

       int i = scanner.nextInt();

       System.out.println(\".....\");
       System.out.println(\"enter your choice not exceed this number in food\\t\"
               + foodlength);
       int j = scanner.nextInt();

       test.create(names, food, i, j, ans);

       System.out.println(\"enter the word to test matching \");
       String word = scanner.next();
       System.out.println(\"enter the second string to replace if matching\");
       String word1 = scanner.next();

       findAndReplace(generatedString.trim(), word.trim(), word1.trim());

   }

}

output

enter the names by giving the space sequently
Alpha David Charlie
enter the food by giving the space sequently otherwise it won\'t work
break milk pasta
enter your choice not exceed this number in names   2
2
.....
enter your choice not exceed this number in food   2
2
the concat word isCharlie loves pasta
enter the word to test matching
loves
enter the second string to replace if matching
hates

Charlie hates pasta

 Write a program which takes two strings as input from the user. Ask the user to enter names in the first string and foods in the second string. In both the str
 Write a program which takes two strings as input from the user. Ask the user to enter names in the first string and foods in the second string. In both the str
 Write a program which takes two strings as input from the user. Ask the user to enter names in the first string and foods in the second string. In both the str

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site