Advertisement
Guest User

Untitled

a guest
Mar 18th, 2016
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.66 KB | None | 0 0
  1. import javax.swing.*;
  2. public class Program
  3. {
  4.    public static void main(String[] args)
  5.    {
  6.       String name;
  7.       int i;
  8.       name = JOptionPane.showInputDialog("Enter your name");
  9.       boolean valid = false;
  10.       while(!valid)
  11.       {
  12.          for(i = 0; i< name.length(); i++)
  13.          {
  14.             if (Character.isDigit(name.charAt(i))) //checks for digit
  15.             {
  16.                name=JOptionPane.showInputDialog ("Name must not contain numbers. Re-enter");
  17.                break; //why keep looking for more errors  
  18.             }
  19.            
  20.          }//end for
  21.          if(name.equals(""))
  22.          {
  23.             valid = false;
  24.             name=JOptionPane.showInputDialog ("You must have a name. Re-enter");
  25.          
  26.          }
  27.          else if(i >= name.length()) //went through the whole name and all letters
  28.             valid = true;
  29.       }//end while
  30.       JOptionPane.showMessageDialog(null, "Name is now correct",
  31.          "good", JOptionPane.PLAIN_MESSAGE);
  32.          
  33.       String social = JOptionPane
  34.                 .showInputDialog("Please enter a social security number DDD-DD-DDDD: ");
  35.       boolean valids = checkSSN(social);
  36.       if (valid) {
  37.          JOptionPane.showMessageDialog(null, "Valid SSN");
  38.       }
  39.       else
  40.          JOptionPane.showMessageDialog(null, "Invalid SSN");
  41.    
  42.    }
  43.  
  44.    public static boolean checkSSN(String social) {
  45.       boolean valids = false;
  46.    
  47.         // Check if it uses the proper length
  48.       if (social.length() == 11) {
  49.          valids = true;
  50.      
  51.             // Make sure characters use proper digits
  52.          char index0 = social.charAt(0);
  53.          char index1 = social.charAt(1);
  54.          char index2 = social.charAt(2);
  55.          char index4 = social.charAt(4);
  56.          char index5 = social.charAt(5);
  57.          char index7 = social.charAt(7);
  58.          char index8 = social.charAt(8);
  59.          char index9 = social.charAt(9);
  60.          char index10 = social.charAt(10);
  61.             // Valid as hyphen
  62.          char index3 = social.charAt(3);
  63.          char index6 = social.charAt(6);
  64.      
  65.          if (Character.isDigit(index0) && Character.isDigit(index1)
  66.                     && Character.isDigit(index2) && Character.isDigit(index4)
  67.                     && Character.isDigit(index5) && Character.isDigit(index7)
  68.                     && Character.isDigit(index8) && Character.isDigit(index9)
  69.                     && Character.isDigit(index10) && index3 == '-'
  70.                     && index6 == '-') {
  71.             valids = true;
  72.          }
  73.       }
  74.       else
  75.          valids = false;
  76.    
  77.       return valids;
  78.      
  79.      
  80.          
  81.    }//end main
  82.    
  83. }//end class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement