Looking at a map of James Cook's third voyage on Wikipedia, he makes a fairly straigtforward path to the Hawaiian Islands. Did he have some foreknowledge that the islands existed and their rough location? Was he just lucky?
I suppose you could say he was just lucky... though he did die indirectly because of it.
In the case of his third voyage, he was heading up to look for the Northwest passage after stopping off in Tahiti. On his way up to begin his survey, he encounter clues that there must be land nearby-- flotsam, birds, cloud formation-- can all give you a pretty good indication that you are near land well before you see it and the ladder two can often tell you exactly which direction to head. Reading natural signs was one of the ways that Oceanic peoples found the islands in the first place and is still used by many ocean voyaging peoples.
So Cook realized land was near and then caught sight of the island of Kauai (and eventually the neighboring Niihau and Lehua). He stopped there only briefly for some quick exchanges before heading off to complete his mission. After fruitlessly searching for the Northwest Passage, Cook sailed back down to investigate the islands he had encountered, this time arriving at Hawaii Island (or the Big Island in common parlance). He circled the island His men and him wintered there for about a month, before departing again. Of course, they then ran into troublesome weather and the foremast of the Resolution was snapped, forcing the ships to make for land to effect repairs. And shortly thereafter Cook was dead.
So did he know about Hawaii? Like I said, there is no substantial proof he was intentionally heading for the islands, they just happen to lay in the direction he was heading after leaving Tahiti. On his first voyage he had sailed with a Tahitian navigator, Tupaia, before his untimely death in Batavia at the end of the voyage. Tupaia drew Cook a map of all of the islands he was aware of-- the map itself is not quite a map in the since we might imagine it, but Tupaia's attempt to render his knowledge in a fashion more like the maps Cook had. Rather than physical distance Tupaia's map likely marks voyaging time between islands. And its possible that if Tupaia knew of Hawaii, he mentioned it to Cook-- yet this seems unlikely since Cook didn't encounter Hawaii till his third voyage and otherwise doesn't seem to have been looking for the islands. Its also possible he heard of the islands during one of his stops in Tahiti from someone else, there are oral traditions that connect the islands and by Cook's third visit to Tahiti he had at least some ability to communicate in the language-- yet he wasn't really hanging out on the right part of the island or with the right people to necessarily have picked up on that information, nor is it clear his command of the language would have been sufficient to actually glean the navigational details from an oral tradition if it had been shared with him. And its not really clear that this would have been the case.