I answered a similar question in another thread that I will repeat here.
I will tell you what the more advanced books say - which is correct - others may not be quite right. Its from Ballentine - QM - A Modern development:
https://www.amazon.com/dp/9814578584/?tag=pfamazon01-20
I suggest if you are interested in such things get a hold of the book and read the first three chapters.
First we will start with the axioms of QM:
Axiom 1
Associated with each measurement we can find a Hermitian operator O, called the observations observable, such that the possible outcomes of the observation are its eigenvalues.
Axiiom 2 - called the Born Rule
Associated with any system is a positive operator of unit trace, P, called the state of the system, such that expected value of of the outcomes of the observation is Trace (PO).
Note - the state of a system is not an element of a Hilbert space - it is an operator. How they come into it I will explain.
A state is called pure if its of the form |u><u|. A state is called mixed if its the convex sum of pure states ∑ pi |ui><ui|. It can be shown all states are either pure or mixed. Its the pure states that can be mapped to the Hilbert space - the u in |u><u| can be mapped to normalised vectors but there is an ambiguity in doing so because |cu><cu| = |u><u| if c is simply a phase factor. It can be extended further for all vectors in the Hilbert space by |u/||u||><u/||u||| - hence the states become rays in the Hilbert space - but the length is on no consequence. However always bear in mind the state is really an operator - this is simply a mapping.
Like any vector the basis you choose to write the vector in is entirely arbitrary - it purely depends on utility - and in QM that often depends on the observable you are interested in.
Given an Hermitian operator O by the spectral theorem it can be expanded in terms of its eigenvalues and eigenvectors O = ∑yi |yi><yi|. Since the |yi> form an orthonormal basis any vector |v> can be expanded in terms of that basis |v> = ∑vi |yi>. The vi is called the representation of the vector v in terms of the observable O. Without proving it, it turns out, from the Born rule, given a representation of an observable O, vi, then if you observe it with the observation associated with O the probability of outcome i is |vi|^2.
Now in the particle in the box you use Schroedingers equation which is written in terms of the wave function which is the expansion of the state in terms of the position eigenvalues. You get the eigenvalues and eigenvectors in terms of the energy operator. The only allowable states are superpositions of those eigenvectors. Let |u> = ∑ wi |ei>, where i labels the possible energies ie the eigenvalues of the energy operator, be on such supposition. The 'weight' wi in that superposition gives via |wi|^2 the probability of the system being in energy i if you were to observe it.
Thanks
Bill