answer.
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]
2 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]2 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
Define a) Principal Plane b) Principal Stress c) anelasticity d) yield point e) ultimate tensile stress f) hardness g) toughness
bagirrra123 [75]

Answer:

Principal Plane: It is that plane in a stressed body over which no shearing stresses act. As we know that in a stressed body on different planes 2 different kind of stresses act normal stresses acting normal to the plane ans shearing stresses acting in the plane. The special planes over which no shearing stresses act and only normal stresses are present are termed as principal planes.

Principal Stress: The stresses in the principal planes are termed as normal stresses.

Anelasticity: It is the behavior of a material in which no definite relation can found to exist between stress and strain at any point in the stressed body.

Yield Point: It is the point in the stress-strain curve of a body at which the stress in the body reaches it's yield value or the object is just about to undergo plastic deformation if we just increase value of stress above this value. It is often not well defined in high strength materials or in some materials such as mild steel 2 yield points are observed.

Ultimate tensile strength: It is the maximum value of stress that a body can develop prior to fracture.

Hardness: it is defined as the ability of the body to resist scratches or indentation or abrasion.

Toughness: It is the ability of the body to absorb energy and deform without fracture when it is loaded. The area under the stress strain curve is taken as a measure of toughness of the body.

Elastic limit: The stress limit upto  which the body regains it's original shape upon removal of the stresses is termed as elastic limit of the body.

4 0
2 years ago
if the mine winch drum diameter is 6 m, calculate how far the cage will drop for each single rotation of the drum
vesna_86 [32]

Answer:

The depth to which the cage will drop for each single rotation of the drum is approximately 18.85 m.

Explanation:

The mine winch drum consists of a drum around around which the haulage rope is attached

Therefore, given that the diameter of the winch drum = 6 m

The length of rope unwound by each rotation = How far the cage will drop for each single rotation of the drum

The length of the rope unwound by each rotation = The rope that goes round the circumference of the winch drum, once

∴ Since the rope that goes round the circumference of the winch drum, once = The circumference of the winch drum, we have;

The length of the rope unwound by each rotation = The circumference of the winch drum = π × The diameter of the winch drum

The length of the rope unwound by each rotation = π × 6 m = 6·π m

The length of rope unwound by each rotation = How far the cage will drop for each single rotation of the drum = 6·π m

How far the cage will drop for each single rotation of the drum = 6·π m ≈ 18.85 m

The depth to which the cage will drop for each single rotation of the drum ≈ 18.85 m.

3 0
2 years ago
The rigid bar CDE is attached to a pin support at E and rests on the 30 mm diameter brass cylinder BD. A 22 mm diameter steel ro
Leno4ka [110]

Answer:

stress = 38.84 MPa

Explanation:

S_{D} = \alpha _{brass} * (delta T) *(L_{BD} )\\= (18.8 * 10^(-6) )*(30)*(0.3)\\= 0.0001692 m\\\\E_{BD} = stress / strain\\stress = E_{BD} * S_{D}\\stress = (200 *10^9) * (0.0001692)\\stress = 33.84 MPa

3 0
2 years ago
Python Homework
DerKrebs [107]

Answer:

  1. TAX_RATE = 0.20
  2. STANDARD_DEDUCTION = 10000.0
  3. DEPENDENT_DEDUCTION = 3000.0
  4. # Request the inputs
  5. grossIncome = float(input("Enter the gross income: "))
  6. numDependents = int(input("Enter the number of dependents: "))
  7. # Compute the income tax
  8. taxableIncome = grossIncome - STANDARD_DEDUCTION - \
  9. DEPENDENT_DEDUCTION * numDependents
  10. incomeTax = taxableIncome * TAX_RATE
  11. # Display the income tax
  12. print("The income tax is $" + str(round(incomeTax,2)))

Explanation:

We can use round function to enable the program to output number with two digits of precision.

The round function will take two inputs, which is the value intended to be rounded and the number of digits of precision. If we set 2 as second input, the round function will round the incomeTax to two decimal places. The round function has to be enclosed within the str function so that the rounded value will be converted to a string and joined with the another string to display the complete a sentence of income tax info.

5 0
2 years ago
13–27. The conveyor belt is moving downward at 4 m&gt;s. If the coefficient of static friction between the conveyor and the 15-k
Feliz [49]

Answer:

See explanation for step by step procedure to get answer.

Explanation:

Given that:

The conveyor belt is moving downward at 4 m>s. If the coefficient of static friction between the conveyor and the 15-kg package B is ms = 0.8, determine the shortest time the belt can stop so that the package does not slide on the belt.

See the attachments for complete steps to get answer.

4 0
2 years ago
Other questions:
  • The pressure at any point in a static fluid depends only on the Select one a)- depth, surface pressure, and specific weight. b)-
    11·1 answer
  • 1. Create a class called Name that represents a person's name. The class should have fields named firstName representing the per
    8·2 answers
  • 3. (20 points) Suppose we wish to search a linked list of length n, where each element contains a key k along with a hash value
    7·1 answer
  • 5 kg of steam contained within a piston-cylinder assembly undergoes an expansion from state 1, where the specific internal energ
    7·1 answer
  • The driving force for fluid flow is the pressure difference, and a pump operates by raising the pressure of a fluid (by converti
    13·1 answer
  • The components of an electronic system dissipating 90 W are located in a 1-m-long circular horizontal duct of 15-cm diameter. Th
    11·1 answer
  • Benzene vapor at 480°C is cooled and converted to a liquid at 25°C in a continuous condenser. The condensate is drained into 1.7
    15·1 answer
  • Which requirement is an appropriate reason for a business to use information technology (IT) tools
    9·1 answer
  • Technician A says that weld-through primer can be removed from the immediate weld area to improve weld quality. Technician B say
    7·1 answer
  • Problema sobre programacion orientada a objetos!!
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!