I am assuming that you are 40 years old (birth year 1967?). I take the extra liberty of assuming that you are a smoker.
The evaluation of pain would require some details. What kind of activities provoke the pain? Does it seem to hurt more with certain body positions like lying on your back, standing up? after any vigorous work or exercise? any relation with breathing? With cough? The pain associated with breathing points to a lung problem in general – not necessarily cancer. If you are indeed a smoker – smoker related changes in the lung may make breathing difficult and painful, eventhough cancer is absent. Gastrointestinal related reasons, tend to occur with lying down, and tend to occur in the evenings (when you recline). Other symptoms that raise the suspicion of GI causes is temporal exacerbation and improvement with feeding/starvation.
The quality of the pain is also important. Pain in the lung tends to be dull, over a large area (not localized to a specific trigger area).. Those of gastro-related would be a burning sensation.
Look also for other things such as weight loss (though these are again not specific for either a lung or a stomach problem). Your age (if it is indeed 40) is still far off from the average age at diagnosis (which is approx 64), although men of 45 years have been included in trials of lung cancer prevention. Suspicion for cancer tends to be higher, the older you get, as risk increases with age to a certain extent.
Its best you have yourself examined by a physician, and make a definite approach to the evaluate and treat theses symptoms.