Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- public class Program
- {
- public static void main(String[] args)
- {
- String name;
- int i;
- name = JOptionPane.showInputDialog("Enter your name");
- boolean valid = false;
- while(!valid)
- {
- for(i = 0; i< name.length(); i++)
- {
- if (Character.isDigit(name.charAt(i))) //checks for digit
- {
- name=JOptionPane.showInputDialog ("Name must not contain numbers. Re-enter");
- break; //why keep looking for more errors
- }
- }//end for
- if(name.equals(""))
- {
- valid = false;
- name=JOptionPane.showInputDialog ("You must have a name. Re-enter");
- }
- else if(i >= name.length()) //went through the whole name and all letters
- valid = true;
- }//end while
- JOptionPane.showMessageDialog(null, "Name is now correct",
- "good", JOptionPane.PLAIN_MESSAGE);
- String social = JOptionPane
- .showInputDialog("Please enter a social security number DDD-DD-DDDD: ");
- boolean valids = checkSSN(social);
- if (valid) {
- JOptionPane.showMessageDialog(null, "Valid SSN");
- }
- else
- JOptionPane.showMessageDialog(null, "Invalid SSN");
- }
- public static boolean checkSSN(String social) {
- boolean valids = false;
- // Check if it uses the proper length
- if (social.length() == 11) {
- valids = true;
- // Make sure characters use proper digits
- char index0 = social.charAt(0);
- char index1 = social.charAt(1);
- char index2 = social.charAt(2);
- char index4 = social.charAt(4);
- char index5 = social.charAt(5);
- char index7 = social.charAt(7);
- char index8 = social.charAt(8);
- char index9 = social.charAt(9);
- char index10 = social.charAt(10);
- // Valid as hyphen
- char index3 = social.charAt(3);
- char index6 = social.charAt(6);
- if (Character.isDigit(index0) && Character.isDigit(index1)
- && Character.isDigit(index2) && Character.isDigit(index4)
- && Character.isDigit(index5) && Character.isDigit(index7)
- && Character.isDigit(index8) && Character.isDigit(index9)
- && Character.isDigit(index10) && index3 == '-'
- && index6 == '-') {
- valids = true;
- }
- }
- else
- valids = false;
- return valids;
- }//end main
- }//end class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement