If the US Navy knew it was under attack, how would it be a trap if the fleet knew to expect the Japanese fleet there?
Edit: it has been answered thank y’all!
The simple answer to this is that the Imperial Japanese Navy (IJN) did not know that the US Navy (USN) knew that the IJN planned to attack Midway.
But let's zoom out a bit first to discuss why the IJN was even going for Midway in the first place. By mid-1942, the Japanese had effectively accomplished most of the immediate objectives they sought when they went to war with the western Allies at the end of 1941. Singapore and the Phillippines had fallen, the Dutch East Indies and its vital oil resources had been seized, and Japanese naval forces had raided northern Australia and into the Indian Ocean. Allied sea power in the Western Pacific had been mostly destroyed, with the Royal Navy withdrawing into the western half of the Indian Ocean, while the main US battleline remain crippled. Tactically and operationally, the Japanese had achieved a stunning victory. Strategically however, they were on borrowed time. Although their operations had been highly succesful so far, they had no way to compel the Americans or British to make peace (a problem which had been conveniently ignored in most pre-war Japanese planning). This meant that by the spring of 1942, the Japanese faced a significant conundrum: what to do next?
The Army wanted to bring an end to further offensives, and focus on consolidating Japan's gains, fortifying its new holdings to enable them to hold out against the coming American counter offensives as American industrial output picked up. However the Navy, with Isoroku Yamamoto as Commander in Chief of the Combined Fleet pressed for continued offensive action. Yamamoto's belief was that the US would not come to the negotiating table while they maintained a fleet capable of responding, and--moreover--that the Americans were demoralized and would have to be baited into committing their remaining assets. His plan for the 1942 campaign was to launch an attack on a target the US could not afford to lose, and that would force the Americans into commiting what remained of their fleet to respond where it could be crushed by the concentrated power of the IJN. With the American fleet destroyed as a threat until its new constructions could come online, the Japanese would be free to strike south again, attacking Fiji and New Caledonia, potentially severing the lines of communication between the US and Australia. The Doolittle Raid helped to solidify this course of action, by providing a demonstration that the Japanese Home Islands would not be safe until the American carriers were sunk. With these as his basic assumptions, Yamamoto set out to craft a plan that would become Operation AF, the attack on Midway Island. Operation MI (and the concurrent attack on the Aleutians), would involve almost every major unit of the IJN, in multiple seperate task forces converging on Midway Island. The Kidō Butai, Japan's fast carrier fleet, would launch air raids on Midway, neutralizing most of the islands defenses in time for a seperate Landing Force to invade and occupy at this island. Assuming that the USN would respond to air attacks on Midway by sortieing their remaining forces from Pearl Harbor, these forces would be detected by a Japanese submaring cordon between Pearl Harbor and Midway, and Kidō Butai would manuver to engage them, all the while Yamamoto closed in with his Main Body to destroy the US fleet in the Decisive Battle.
If this all sounds very complicated, rest assured that it was. With the assumption that American morale was near breaking, Yamamoto assumed that if an overwhelming display of force were committed, the USN would remain in harbor. As such, his forces were dispersed to conceal their strength. This was the trap Japan hoped the Americans would walk into.
Yamamoto's base assumptions, alas, were wrong. Although the USN had undoubtedly taken a severe beating at Pearl Harbor, Java Sea, the various island raids, and even at the "draw" at Coral Sea, it was far from a demoralized force that would have to be drawn out of Pearl Harbor. Admiral Nimitz, the Commander in Chief of the Pacific Fleet was looking for a place to attack to try and reverse Japanese operations. The dispatch of two carriers to the Coral Sea had already succeeded in turning back a Japanese invasion attempt, albeit at the cost of the Lexington sunk and Yorktown badly damaged. Moreover, American signals intelligence had succeeded in breaking Japanese codes. While they could only read bits and pieces of Japanese transmissions, it was enough for American intelligence to believe that the Japanese were planning a major operation against Midway, involving up to five carriers. With this information in hand, Nimitz planned his own trap for the Japanese. The American carriers would not respond to the Japanese attack by sortieing from Pearl Harbor. They would be lying in wait for the Japanese off of Midway, ready to strike once the Japanese arrived. In addition, Midway was heavily reinforced and fortified, to such an extent that even if the US had lost the naval battle, it was highly unlikely the Japanese would succeed in taking the island itself.
To add on to this, Japanese strategic reconaissance had failed completely in the lead up to Midway. The Japanese planned two reconaissance missions on the strategic level: 1) a cordon of submarines between Midway and Pearl Harbor that would spot the American fleet as it came north, and 2) a reconaissance mission by flying boats to confirm the American carriers were in harbor. Because the American carriers sortied ahead of the schedule set for them by the Japanese, (and because the subs were late), the American carriers slipped past a Japanese submarins unnoticed. Similarly, an unexpected American seaplane tender in the small island chain where the Japanese hoped to refuel their flying boats resulted in that operation being scratched. Similarly, Japanese tactical reconaissance was very spotty. Japanese carrier doctrine placed very little emphasis on scouting, believing that a carrier's aircraft should be conserved for massed, coordinated strikes. This meant that the lacklustre reconaissance screen put out by the Japanese failed to locate the American carriers before it was too late.
And the rest as they say, is history.
So, to bring it all the way back around to your original question: the Japanese did not know that the Americans knew they were coming. As a bit of an add on to this, in the planning stages of the battle, Japanese wargames presented a scenario wherein the Americans were alerted to the Japanese attack, and so prepositioned their fleet, much as what happened in real life. However Yamamoto chose to overrule this, and instead insisted that the US only act as he presupposed they would act. Japanese reconaissance on the strategic and tactical levels was spotty at best, meaning that even as they sailed towards Midway, they had no idea that their trap had been uncovered and the Americans were preparing their own ambush.
Hope this helps answer your question. Please feel free to ask any follow ups.
Sources
No it answered it perfectly thank you!