class Blue_ManTest{
public static void main(String[] args){
String name = "I LOVE JAVAWORLD";
int index1 = name.indexOf(" ");
int index2 = name.lastIndexOf(" ");
String str1 = name.substring(0, index1);
String str2 = name.substring(index1 + 1, index1+ 5);
String str3 = name.substring(index2 + 5);
System.out.println(str3 + ", " + str1 + " " + str2 + ".");
}
}
I am having trouble figuring out what would be the output of this program I think I know it but I am not sure.
I did this I Love JavaWorld with 0 corresponding to j and 15 to D with 1 being the space between.
for str1
I get I
for str2
I get Love
but for str3
I get avaWorld
But str3
seems wrong to me as it would print out.
avaWorld, I Love.
Your str3
variable is taking a substring that starts at index2 + 5
where index2
is the index of the last space in your input string:
int index2 = name.lastIndexOf(" ");
That is, index2
is 6. And of course 6 + 5 is 11.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.