+ 1

Java get first 4 letters from the word

Hi! My code is done, but looking at the assignment I see that name in the sample output was shortened from William to Will. It's might be just a mistype(nothing about that in the assignment), but I don't want to take a chance. The question is: How can I turn William to Will? Any Simple way without creating a separate function? Here is the code: public static void main(String[] args) { String name = "William"; int age = 25; double payRate = 18.50; char office = 'B'; System.out.printf(name + " is " + age + " years old \n"); System.out.printf("He is in office " + office + "\n"); System.out.printf("In a 40-hour week, his pay is

quot; + (payRate * 40)); Any help much appreciated! Thanks!

28th May 2018, 1:37 AM
DIY Mods
DIY Mods - avatar
3 Answers
+ 5
And why can't you just do name.substring(0,4); ?
28th May 2018, 2:31 AM
Hatsy Rei
Hatsy Rei - avatar
+ 1
Thank you! Great material. But as you can see from my question, I need to get the first letters, not last, so beginIndex isn't suitable in this case
28th May 2018, 2:28 AM
DIY Mods
DIY Mods - avatar