0

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.

2nd
  • 9
  • 4

1 Answers1

1

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);
...
Hungry Blue Dev
  • 1,283
  • 15
  • 29
  • Thank you so much mate. I know it's a bit messy at the momment, I've cleaned it up :) – 2nd Mar 23 '14 at 06:13
  • Well, don't forget to accept the answer. :-) – Hungry Blue Dev Mar 23 '14 at 06:13
  • I know it might be a bit extra, but how do I make it so the capitalize part check that the "product" is the first string/word in a sentence and only capitalises it if it is? – 2nd Mar 23 '14 at 06:37
  • @2nd Take a look, tell me if you don't understand. – Hungry Blue Dev Mar 23 '14 at 06:43
  • Okay, I understand that code. However, I'm using UI.println to write the text to console. I cant seem to get it to check that. I've tried if (printmessage.startsWith(Product) but it doesnt seem to work. – 2nd Mar 23 '14 at 06:53
  • You have to _create_ a line. (Implement it correctly... Is there any chance that `text` is an empty `String`? I've assumed that and written my code accordingly.) – Hungry Blue Dev Mar 23 '14 at 06:57
  • i've tried it (edit up top), but I can't get it to print out the capitalised version :(. – 2nd Mar 23 '14 at 07:05