Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The answer must use the indexof , substring , and stringbuilder methods. The use

ID: 3799098 • Letter: T

Question

The answer must use the indexof, substring, and stringbuilder methods. The use of tokenizer utility is not allowed.

NOTE THERE IS ALREADY A SIMILAR QUESTION ON CHEGG AND THE MAIN CLASS AND ANSWER WILL DIFFER. PLEASE DONT COPY AND PASE THE SOLUTION ALREADY PROVIDED ON CHEGG.

Exercise 9-3 Parse a string of product data In this exercise, you’ll parse a string that contains data for a product and store that data in a Product object. Review the application Open the project named ch08_ex3_ProductParser.

Open the Main and Product classes and review the code. Note that the Main class defines a Product object and a String object that stores the data for a product with each field delimited by a colon (:). However, this code doesn’t parse this string and store the data in the Product object.

Run the application. At this point, it shouldn’t print any product data to the console.

Add the code that parses the string

In the Main class, add code to the parseString method that parses the string and gets the three fields that are stored in the string. Then, store this data in the Product object that is returned by the method.

Run the application and make sure it works correctly. After a successful run, the console should look something like this:

CODE PROVIDED:

main.java

package murach.ui;

import murach.business.Product;

public class Main {

public static void main(String[] args) {
String productString1 = "java:Murach's Java Programming:57.50";
String productString2 = "mysql:Murach's MySQL:54.50";

Product product1 = new Product();
Product product2 = new Product();
  
//TODO: process the productString variables and populate fields of
//product objects in the parseString method
product1 = parseString(productString1, product1);
product2 = parseString(productString2, product2);
  
System.out.println("Code: " + product1.getCode());
System.out.println("Description: " + product1.getDescription());
System.out.println("Price: " + product1.getPriceFormatted());
System.out.println("");
  
System.out.println("Code: " + product2.getCode());
System.out.println("Description: " + product2.getDescription());
System.out.println("Price: " + product2.getPriceFormatted());
System.out.println("");
}
  
public static Product parseString (String inputString, Product inputProduct){
// use String class 'index' amd substring methods to parse the input string parameter
// and then set the properties of the inputProduct object
  
  
return inputProduct;
}
}

product.java

package murach.business;

import java.text.NumberFormat;

public class Product {

private String code;
private String description;
private double price;

public Product() {
code = "";
description = "";
price = 0;
}

public Product(String code, String description, double price) {
this.code = code;
this.description = description;
this.price = price;
}

public void setCode(String code) {
this.code = code;
}

public String getCode() {
return code;
}

public void setDescription(String description) {
this.description = description;
}

public String getDescription() {
return description;
}

public void setPrice(double price) {
this.price = price;
}

public double getPrice() {
return price;
}

public String getPriceFormatted() {
NumberFormat currency = NumberFormat.getCurrencyInstance();
return currency.format(price);
}
}

Explanation / Answer

Let me know if this is not what you want.

public class Main {
   public static void main(String[] args) {
       String productString1 = "java:Murach's Java Programming:57.50";
       String productString2 = "mysql:Murach's MySQL:54.50";

       Product product1 = new Product();
       Product product2 = new Product();

       // TODO: process the productString variables and populate fields of
       // product objects in the parseString method
       product1 = parseString(productString1, product1);
       product2 = parseString(productString2, product2);

       System.out.println("Code: " + product1.getCode());
       System.out.println("Description: " + product1.getDescription());
       System.out.println("Price: " + product1.getPriceFormatted());
       System.out.println("");

       System.out.println("Code: " + product2.getCode());
       System.out.println("Description: " + product2.getDescription());
       System.out.println("Price: " + product2.getPriceFormatted());
       System.out.println("");
   }

   public static Product parseString(String inputString, Product inputProduct) {
      
       inputProduct.setCode(inputString.split(":")[0]);
       inputProduct.setDescription(inputString.split(":")[1]);
       inputProduct.setPrice(Double.parseDouble(inputString.split(":")[2]));
       return inputProduct;
   }
}

===O/p====

Code: java
Description: Murach's Java Programming
Price: $57.50

Code: mysql
Description: Murach's MySQL
Price: $54.50

===============OR================

public class Main {
   public static void main(String[] args) {
       String productString1 = "java:Murach's Java Programming:57.50";
       String productString2 = "mysql:Murach's MySQL:54.50";

       Product product1 = new Product();
       Product product2 = new Product();

       // TODO: process the productString variables and populate fields of
       // product objects in the parseString method
       product1 = parseString(productString1, product1);
       product2 = parseString(productString2, product2);

       System.out.println("Code: " + product1.getCode());
       System.out.println("Description: " + product1.getDescription());
       System.out.println("Price: " + product1.getPriceFormatted());
       System.out.println("");

       System.out.println("Code: " + product2.getCode());
       System.out.println("Description: " + product2.getDescription());
       System.out.println("Price: " + product2.getPriceFormatted());
       System.out.println("");
   }

   public static Product parseString(String inputString, Product inputProduct) {
      
       inputProduct.setCode(inputString.substring(0, inputString.indexOf(":")));
       inputProduct.setDescription(inputString.substring(inputString.indexOf(":")+1,inputString.lastIndexOf(":")));
       inputProduct.setPrice(Double.parseDouble(inputString.substring(inputString.lastIndexOf(":")+1,inputString.length())));
       return inputProduct;
   }
}

this is with substring and indexof.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote