1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Luda [366]
3 years ago
5

6.6 Parsing strings (Java) (1) Prompt the user for a string that contains two strings separated by a comma. (1 pt) Examples of s

trings that can be accepted: Jill, Allen Jill , Allen Jill,Allen Ex: Enter input string: Jill, Allen (2) Report an error if the input string does not contain a comma. Continue to prompt until a valid string is entered. Note: If the input contains a comma, then assume that the input also contain

Engineering
1 answer:
Vanyuwa [196]3 years ago
4 0

Answer:

Here is the JAVA code:

import java.util.Scanner;  // to get input from user

public class ParsingStrings //class ParsingStrings

{      public static void main(String[] args) { //start of main() function body

        //  creates object of Scanner class

Scanner scnr = new Scanner(System.in);  

       Scanner inputstream;  // to hold the inputline

       String inputline = ""; //contains the string entered by user

       String substring1 = ""; //First word of the input string

       String substring2 = ""; // next word of the input string after substring1

       System.out.println("Enter input string: ");

//prompts user to enter a string like Allen, Jill

       while (inputline.matches("q") == false) {

//while loop keeps reading the input strings until user enters q to exit

           inputline = scnr.nextLine(); //reads and scans the input string

           inputline = inputline.replaceAll(",",", "); // to replace all commas to " ,"

           inputstream = new Scanner(inputline); //scans input string

           int commaPos = inputline.indexOf(",");

// indexOf() returns the position of comma (",")

//if the string does not have comma and user does not enter q to exit

           if ((commaPos<=-1) && (inputline.matches("q") == false)) {

//display error message as comma is not found in input string

               System.out.println("Error: No comma in string");

               System.out.println("Enter input string: ");            }

//ask user to enter string again until he enters valid comma separated string

//if user enters no string, or one word or no comma seperated 2 words

          else if  ((commaPos <= -1) && (inputline.length() == 0 || inputline.split("\\s+").length < 2) && (inputline.matches("q") == false)) {

//ask user to enter two words

               System.out.println("Please enter two words");

               System.out.println("Enter input string: ");            }

// checks if user has entered q

           else if (inputline.matches("q") == false) {

               substring1 = inputstream.next(); //stores word 1

               substring2 = inputstream.nextLine(); //stores word 2

//prints the first word by replacing empty spaces with comma

               System.out.println("First word: " + substring1.replaceAll("\\s",""));

//prints second word by removing empty spaces

               System.out.println("Second word: " + substring2.replaceAll("\\s","").replaceAll("\\W",""));

               System.out.println("\n");

// asks user to enter string until user types q

               System.out.println("Enter input string: ");           }        }    } }

Explanation:

The program is briefly explained in the comments given in the code.

The program takes input from user in the form of strings.

The string contains two sub strings separated by comma.

commaPos stores the position of comma in the input string using indexOf() function.

If and else if conditions used here checks the input string for the following:

  • The strings should be separated by commas. commaPos <= -1
  • The user does not enter just one word. inputline.split("\\s+").length < 2. split() function here splits the input string by empty space between both the words and then checks the length that it should not be less than 2 which means two words should be entered.
  • The user should enter a string.
  • The length of the input string should not be 0. inputline.length() == 0
  • replaceAll() function is used to remove the spaces and extra commas and the other non word characters and replace them with comma so that the two words can easily be separated by a single comma and can be displayed in output.

The screenshot of output of the program is attached.

You might be interested in
Appliances that use force voltage analogy
Inga [223]
Answer: resistor

Explanation: Not quite sure. Need more research
5 0
3 years ago
Which distributions would you recommend be tested using Benford’s law? Select all choices that apply. (You may select more than
Leni [432]

Answer:

Please see the attached picture for the complete answer.

Explanation:

6 0
4 years ago
Engine oil flows through a 25‐mm‐diameter tube at a rate of 0.5 kg/s. The oil enters the tube at a temperature of 25°C, while th
Elodia [21]

Answer:

a) the log mean temperature difference (Approx. 64.5 deg C)

b) the rate of heat addition into the oil.

The above have been solved for in the below workings

Explanation:

3 0
4 years ago
2. Given that a quality-control inspection can ensure that a structural ceramic part will have no flaws greater than 100 µm (100
MA_775_DIABLO [31]

Answer:

SiC=169.26 Mpa

Partially stabilized zirconia=507.77 Mpa

Explanation:

<u>SiC </u>

Stress intensity, K is given by

K=\sigma Y\sqrt{\pi a} hence making \sigma the subject where \sigma is applied stress, Y is shape factor, a is crack length

\sigma=\frac {K}{Y\sqrt{\pi a}} substituting the given figures and assuming shape factor, Y of 1

\sigma=\frac {3}{1\sqrt{\pi 100*10^{-6}}}= 169.2569\approx 169.26 Mpa

<u>Stabilized zirconia </u>

Stress intensity, K is given by

K=\sigma Y\sqrt{\pi a} hence making \sigma the subject where \sigma is applied stress, Y is shape factor, a is crack length

\sigma=\frac {K}{Y\sqrt{\pi a}} substituting the given figures and assuming shape factor, Y of 1

\sigma=\frac {9}{1\sqrt{\pi 100*10^{-6}}}= 507.7706\approx 507.77 Mpa

7 0
3 years ago
I have to find the critical points of this function of two variables <img src="https://tex.z-dn.net/?f=%5C%5Cf%28x%2Cy%29%3Dx%5E
liraira [26]

Answer:

no i dont think there is

Explanation:

because theres not

4 0
4 years ago
Other questions:
  • An aluminum heat sink (k = 240 W/m-K) used to cool an array of electronic chips consists of a square channel of inner width w =
    6·2 answers
  • In a TDM communication example, 15 voice signals are badlimited to 5kHz and transmitted simultaneously using PAM. What is a prel
    14·1 answer
  • Liquid methyl ethyl ketone (MEK) is introduced into a vessel containing air. The system temperature is increased to 55°C, and th
    12·1 answer
  • g At this party, is the fact that a person is from South America independent of that person being majoring in biomedical enginee
    11·1 answer
  • If a pendulum takes 2 sec to swing in each direction, find the period and the frequency of the swing
    15·1 answer
  • A(n) ____________ is a side view that shows height.
    12·1 answer
  • A 3.7 g mass is released from rest at C which has a height of 1.1 m above the base of a loop-the-loop and a radius of 0.2 m . Th
    10·1 answer
  • To select the center of a line what option must be selected? A. Ortho B. Dynamic input C. Object snap
    7·1 answer
  • Aplicación al multivibrador monoestable
    5·1 answer
  • It’s busy at the bakery counter , and a loyal customer / member stops you to charge as he talking , you notice another customer
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!