You use a when the noun you are referring to begins with a consonant (b, c, d, f, g, h, j, k, l, m, n, p, q, r, s, t, v, w, x, y or z), for example, "a city", "a factory", and "a hotel".
You use an when the noun you are referring to begins with a vowel (a, e, i, o, u)
Pronunciation changes this rule. It's the sound that matters, not the spelling.
NOTE !
If the next word begins with a consonant sound when we say it, for example, "university" then we use a. If the next word begins with a vowel sound when we say it, for example "hour" then we use an.
We say "university" with a "y" sound at the beginning as though it were spelt "youniversity".
So, "a university" IS correct.
We say "hour" with a silent h as though it were spelt "our".
So, "an hour" IS correct.
(Lots of people get this wrong - including native speakers.)