java - How to append a string after a declared string? -


a beginner asking question might sound basic!....i still learning how walk....1 day running :)

i want amend following code can insert word "great" after inputted name. names length varies therefore know have change line z.insert(5, " great ");.

can please instruct correct method?

regards,

    scanner id = new scanner(system.in);      string name = " ";     string surname = " ";      system.out.println("please enter name: ");     name = id.nextline();      system.out.println();      system.out.println("please enter surname: ");     surname = id.nextline();      stringbuffer z = new stringbuffer();     z.append(name + " " + surname + " king of kings");     z.insert(5, " great ");      system.out.println(z);     system.out.println(); 

you need calculate length of name, ...

int len = name.length() + surname.length() + 1; 

should give index point after can insert additional string

personally, better append elements in single step if can, example

z.append(name).     append(" ").     append(surname).     append(" great ").     apoend("is king of kings"); 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -