-1

THE MAIN PROBLEM is the method substring() is not found

I'm coding a shop where a <list> of products has already been initialized & all the user has to do is type in the name of the product they want to buy and the number. One of the specifications is that the user types the name of the product correctly e.g. Blue Pen but it should also work if they type in the correct product but with incorrect capitalization e.g. BluE PeN.

I'm trying to use the substring() method to sepearate and capitalize the first letter of the user input (with the toUpperCase() method) and the toLowerCase() for the rest.

private void sell() {
    Product product = getProductByName(readName());
    product = product.substring(0,1).toUpperCase() + product.substring(1).toLowerCase();
Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • 4
    `product` is not a `String` object? – Fullstack Guy Aug 31 '19 at 08:54
  • You cannot apply substring on another class Product. Provide Product also, so that we can provide alternative way – kaushik Aug 31 '19 at 08:55
  • 1
    You are attempting to call `substring` method which belongs to `String` class on type which has nothing to do with String. You probably need to use something like `String productName = product.getName();` (or `.getProductName()` or ... well look it up in your `Product` class) and then you can call `productName.substring(...)`. Regarding comparing strings without case sensitivity you can always use `oneString.equalsIgnoreCase(otherString)`. – Pshemo Aug 31 '19 at 08:56
  • I'm super new to programming so I figured that was the problem. But I'm not sure how to apply this same idea to a list of products opposed to strings. – FriendlyPepega Aug 31 '19 at 09:05

1 Answers1

0

The object you called substring on is not a string but a Product.

private void sell() {
    Product product = getProductByName(readName());
    String str_product = product.getvalueString(); // the product name you want to capitalize the first letter
    str_product = product.substring(0,1).toUpperCase() + product.substring(1).toLowerCase
    product.setValueString(str_product);  // set the name back to the capitalized one.

}
Mustahsan
  • 3,852
  • 1
  • 18
  • 34
Joshegwuatu
  • 43
  • 1
  • 8