Ask Question
5 March, 00:59

A multiplication problem is provided as a string using the format 'x times y'. Create a function called MultiplyString to extract the numbers that should be multiplied from a character vector called inputCharVect, and then assign the product to the variable multResult.

+4
Answers (1)
  1. 5 March, 01:06
    0
    import java. util.*;

    public class MyClass {

    public static void main (String args[]) {

    Scanner scan = new Scanner (System. in);

    System. out. println ("Enter your statement: ");

    String inputCharVect = scan. nextLine ();

    multiplyString (inputCharVect);

    }

    public static void multiplyString (String input) {

    int indexOfWordStart = input. indexOf ("t");

    int indexOfWordEnd = input. indexOf ("s");

    String firstString = input. substring (0, indexOfWordStart);

    String secondString = input. substring (indexOfWordEnd + 1);

    int firstNumber = Integer. parseInt (firstString. trim ());

    int secondNumber = Integer. parseInt (secondString. trim ());

    int multResult = firstNumber * secondNumber;

    System. out. println (multResult);

    }

    }

    Step-by-step explanation:

    The first line of the program is the import statement which import Scanner to allow user input. Then, the user input is assigned to inputCharVect, which is then supplied as arguments to the method multiplyString.

    The multiplyString method then extracts the index of 't' and 's' which are the start letter for times, then generate a substring, after which it was assigned to firstNumber and secondNumber. The product of firstNumber and secondNumber is assigned to multResult.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “A multiplication problem is provided as a string using the format 'x times y'. Create a function called MultiplyString to extract the ...” in 📗 Mathematics if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers