Hello -
I am trying to catch a NumberFormatException using the code below. However, even though it does perform the System.out.println() in the catch statement when a non-number is entered, it still "blows up" displaying the NumberFormatException with all of its accompanying messy messages in the console. I thought I had it catching the exception properly earlier, but something changed to make it no longer catch it properly. Generally, the code gets text from various specially formatted JTextFields using trim() and must convert them to numbers; but in the case where the user did not enter a number, the exception needs to be caught without displaying the NumberFormatException (of course).
Any help is greatly appreciated!
Thanks,
anthutton
if ( e.getSource() == EnterButton ){
try{
for(int i = 0; i <= 17; i++){
//get score in text & convert to int for player1
scoreInText[i] = hole[i].getText();
scoreInNumber[i] = Integer.parseInt(scoreInText[i].trim());
//get score in text & convert to int for player2
scoreInText2[i] = hole2[i].getText();
scoreInNumber2[i] = Integer.parseInt(scoreInText2[i].trim());
//determine lowest score between two players
String handicap1InText = handicap1.getText();
String handicap2InText = handicap2.getText();
handicap1InNumber = Integer.parseInt(handicap1InText.trim());
handicap2InNumber = Integer.parseInt(handicap2InText.trim());
int adjustment1 = 0;
int adjustment2 = 0;
int handicapDifference1 = 0;
int handicapDifference2 = 0;
//account for handicaps player1
//establish the handicapDifference #'s (must be positive)
//for calculation needed later
handicapDifference1 = (HOLEHANDICAPS[i] - handicap1InNumber) * -1;
handicapDifference2 = (HOLEHANDICAPS[i] - handicap2InNumber) * -1;
if(handicap1InNumber == 18){
adjustment1 = -1;}
else if(handicap1InNumber == 36){
adjustment1 = -2;}
else if(handicap1InNumber < 18 && HOLEHANDICAPS[i] - handicap1InNumber <= 0){
adjustment1 = -1;}
else if(handicap1InNumber > 18 && handicap1InNumber < 36 &&
HOLEHANDICAPS[i] - handicap1InNumber < 0 && HOLEHANDICAPS[i] <=
(handicap1InNumber - 18)){adjustment1 = -2;}
else if(handicap1InNumber > 18 && handicap1InNumber < 36 && HOLEHANDICAPS[i]
- handicap1InNumber < 0 && HOLEHANDICAPS[i] > (handicap1InNumber - 18)){
adjustment1 = -1;}
//account for handicaps player2
if(handicap2InNumber == 18){
adjustment2 = -1;}
if(handicap2InNumber == 36){
adjustment2 = -2;}
else if(handicap2InNumber < 18 && HOLEHANDICAPS[i] -
handicap2InNumber <= 0){adjustment2 = -1;}
else if(handicap2InNumber > 18 && handicap2InNumber < 36
&& HOLEHANDICAPS[i] - handicap2InNumber < 0 && HOLEHANDICAPS[i] <=
(handicap2InNumber - 18)){adjustment2 = -2;}
else if(handicap2InNumber > 18 && handicap2InNumber < 36 &&
HOLEHANDICAPS[i] - handicap2InNumber < 0 && HOLEHANDICAPS[i] >
(handicap2InNumber - 18)){adjustment2 = -1;}
scoreInNumber[i] = scoreInNumber[i] + adjustment1;
scoreInNumber2[i] = scoreInNumber2[i] + adjustment2;
if(scoreInNumber[i] < scoreInNumber2[i]){
bestBallScore[i] = scoreInNumber[i];}
else{
bestBallScore[i] = scoreInNumber2[i];}
bestBallScoreNumber[i] = Integer.toString(bestBallScore[i]);
hole3[i].setText(bestBallScoreNumber[i]);
}
}
catch (NumberFormatException nfe) {
System.out.println("Scores are not Complete - Please Verify Scores");
}