Im trying to print just the first 5 characters of a given string in one line. My problem is the whole string is not being printed. Part of the end is being cut off.
int l = 0;
int m = 5;
for(int i = 0; i < (string.length()/5) ; i++)
{
System.out.println(string.substring(j,k));
l = m;
m = m + 5;
}
Given string = "Hello world, my name is something which you want to"
The result would be something like:
Hello
worl
d, my
However, the last parts of the string is not being printed.

However, the last parts of the string is not being printed.
Yes, that's right - because of your loop condition. You're iterating (string.length()/5) times - which rounds down. So if the string has 12 characters, you'll only iterate twice... leaving out the last two letters.
I would suggest solving this slightly differently - get rid of the l and m variables (which I assume you meant to use in your substring call - you never declare j or k) and use the variable in the for loop instead. You need to make sure that you don't try to use substring past the end of the string though - Math.min is handy for that:
for (int sectionStart = 0; sectionStart < string.length(); sectionStart += 5) {
int sectionEnd = Math.min(string.length(), sectionStart + 5);
System.out.println(string.substring(sectionStart, sectionEnd);
}
See more on this question at Stackoverflow