validating user input

April 7, 2009 | No Comments
Share |

import javax.swing.JOptionPane;

public class banditWithValidation {

	public static void main(String[] args) {

		final double PRIZE_1 = 1;
		final double PRIZE_2 = 2;
		final double PRIZE_3 = 3;
		final double PRIZE_4 = 4;
		final double PRIZE_5 = 5;

		double userNum;
		String userName;
		String inputString;

		userName = JOptionPane.showInputDialog("Hello there, whats your name?");

		inputString = JOptionPane.showInputDialog(userName + " please pick a number [1-5]");
		userNum = Integer.parseInt(inputString);

		while(!(userNum >= PRIZE_1 && userNum <= PRIZE_5)){

			inputString = JOptionPane.showInputDialog(userName + " you typed a number not in range please pick a number [1-5]");
			userNum = Integer.parseInt(inputString);

		}

		if (userNum == PRIZE_1) {

			System.out.println(userName + ", you have won the 1st prize of $100,000,000.00");
		}

		else if (userNum == PRIZE_2) {

			System.out.println(userName + ", you have won the 2nd prize of $750,000.00");
		}

		else if (userNum == PRIZE_3) {

			System.out.println(userName + ", you have won the 3rd prize of $90,000.00");
		}

		else if (userNum == PRIZE_4) {

			System.out.println(userName + ", you have won the 4th prize of $1,000.00");
		}

		else if (userNum == PRIZE_5) {

			System.out.println(userName + ", you have won the 5th prize of $101.01");
		}

		else {

			System.out.println(userName + ", you have won nothing, sorry bye bye :)");
		}
		System.exit(0);

	}

}
April 7, 2009 | No Comments
Share |

Leave a Reply