Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele Challenge: Safe Parsing for User Input | Validating Input and Safe Parsing
C# Exceptions and Error Handling Practice

bookChallenge: Safe Parsing for User Input

Tehtävä

Swipe to start coding

Implement a method that safely parses string input for a product price and quantity. Your method should use TryParse to convert the price to a decimal and the quantity to an int, and return a message indicating the result.

  • Attempt to parse priceInput as a decimal.
  • Attempt to parse quantityInput as an int.
  • If both values are invalid, return "Invalid price and quantity input.".
  • If only priceInput is invalid, return "Invalid price input.".
  • If only quantityInput is invalid, return "Invalid quantity input.".
  • If both values are valid, return a message in the format: "Total cost for {quantity} items at {price:C} each is {total:C}.", where {price} is the parsed decimal, {quantity} is the parsed int, and {total} is the product of price and quantity.

Ratkaisu

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 3. Luku 4
single

single

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

Suggested prompts:

Can you explain this in simpler terms?

What are the main benefits or drawbacks?

Can you give me a real-world example?

close

bookChallenge: Safe Parsing for User Input

Pyyhkäise näyttääksesi valikon

Tehtävä

Swipe to start coding

Implement a method that safely parses string input for a product price and quantity. Your method should use TryParse to convert the price to a decimal and the quantity to an int, and return a message indicating the result.

  • Attempt to parse priceInput as a decimal.
  • Attempt to parse quantityInput as an int.
  • If both values are invalid, return "Invalid price and quantity input.".
  • If only priceInput is invalid, return "Invalid price input.".
  • If only quantityInput is invalid, return "Invalid quantity input.".
  • If both values are valid, return a message in the format: "Total cost for {quantity} items at {price:C} each is {total:C}.", where {price} is the parsed decimal, {quantity} is the parsed int, and {total} is the product of price and quantity.

Ratkaisu

Switch to desktopVaihda työpöytään todellista harjoitusta vartenJatka siitä, missä olet käyttämällä jotakin alla olevista vaihtoehdoista
Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 3. Luku 4
single

single

some-alt