public class betterIndexOf { public static void main( String[] args ) { String forest = "The forest is big and scary"; String meadow = "My meadow has bunnies"; int forestFirstSpace = forest.indexOf(' '); int meadowFirstSpace = meadow.indexOf(' '); int meadowSecondSpace = meadow.indexOf(' ', meadowFirstSpace + 1); System.out.println(forest.substring(0, forestFirstSpace) + " " + meadow.substring(meadowFirstSpace + 1, meadowSecondSpace ) ); } }