True, but one tactic that the Germans didn't realize was we were reading their codes and thus able to better intercept U-Boots and wolf packs.
There's more (a lot more) to the story than just the soundbite "the Allies could read the German codes", but that's a different topic.
I'm not sure if Japan had developed sonar to the point it could detect submerged submarines; although radar could detect them while surfaced
The Japanese had decent enough sonars and useful radars - what they never really built was an effective ASW vessel. Their destroyers were focused on surface warfare, particularly large coordinated torpedo attacks and night fighting. Nor did they have any ASW advocates in the Imperial Navy, in a large part due the concentration on large scale fleet engagements - offense over defense. The result was they lacked the equipment, organization, doctrine, tactics, and training to conduct strategic ASW.
By the time they awakened to the size and severity of the threat in late 1943... it was too late. The limited military manpower and industrial capacity they could spare from other equally pressing threats was vastly overmatched by the increasing capability, lethality and raw numbers of Allied submarines they faced.
Finally, the Allies pretty much controlled the seas in the Atlantic and thus could conduct ASW without much concern that they would get into surface battles; Japan did not have that luxury
That's not quite the whole picture, Allied submarine warfare in the Pacific was largely conducted deep within what was colloquially known as "Empire waters" - where the Japanese unquestioningly ruled both the sea and the air. If they'd had the vessels, they'd have been able to fight with no fear of Allied interference. The luxury the Japanese didn't have was industrial capacity - we could produce a vast war fleet (for the Pacific, mostly) as well as a vast merchant and ASW fleet (for the Atlantic, mostly). They couldn't.
(Though on reflection, we're probably saying much the same thing, just from different points of view.)
It's industrial capacity that was the real "secret weapon" that the Allies had in WWII - in both theatres.