Question

what I'm trying to do is write a method that has one argument and returns a new string that capitalises that string and returns in its parameter.

this is my code so far:

 public void input(){

    this.printmessage("Dillon", "Francis", "chimes", "chimes from hudson mohawke", "2",             "$69.00", "$420.00", "$1337.00");
}


public void printmessage(String firstName, String lastname, String product, String company, String number, String retail, String price, String saving){

    UI.println("text " + firstName + ",");



    UI.println(text + " " + product + "s text, text text text text -");



    UI.println(" ");
}

What I want to do is capitalise the product parameter (chimes) and then return into into the printMessage capitalized if it is used at the beginning of a sentence.

Will something like this work?

 public String capitalise(String product){

    return Character.toUpperCase(product.charAt(0)) + product.substring(1);

}

I'm really stuck and would love some help.

Thanks.

I've tried this

 String pls = (product + " example");
    if ( pls.startsWith(product) ) {
        product = capitalise(product); 
   }
   UI.println(pls);

but it doesnt print out the capitalised version.

Was it helpful?

Solution

change this line :

UI.println(text + " " + product + "s text, text text text text -");

to:

UI.println(text + " " + capitalize(product) + "s text, text text text text -");

But your code needs a bit more structuring. Focus on even indentation. And if you need the capitalized product later on, you'd better save it before you use it, like

...
product = capitalize(product);
UI.println(text + " " + product + "s text, text text text text -");
...

EDIT:

For this I'm assuming the line is contained in a String called line.

First check if the line begins with product. Then capitalize it.

...
String line = text + " " + product + "s text, text text text text -";
line = line.trim(); // removes whitespaces.
if ( line.startsWith( product ) ) {
    product = capitalize ( product ); //or whatever.
}
UI.println(line);
...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top