Project is to make a data entry program, with FirstName, MiddleInitial, LastName, and SSN. Then it comes down to the entry validation part.
I think the codes i wrote are correct, tested on single entry worked great. But when i put 4 validations together(FirstName, MiddleInitial, LastName, and SSN), there is always some problems.
I only know if, else if and switch(I tried swtich but it totally didn't work for me), this code was my last try on else if, and this way of coding always ends up poping the alpha - numeric entry error msg, for the FirstName entry. And I'm totally out of ideas of how to write the validation code. Please help!
part of the code
//Validating First Name Filed
if(TOne.getText().length() != 0 && Determinator == BufferOne.length())
{
for(k = 0; k < BufferOne.length(); k++)
{
for(m = 0; m < 26; m++)
{
if(BufferOne.charAt(k) == Alpha.charAt(m))
{
Determinator++;
}
}
}
}
else if(TOne.getText().length() == 0)
{
JOptionPane.showMessageDialog(null, "First Name Field REQUIRED!");
TOne.requestFocus();
}
else if(Determinator != BufferOne.length())
{
JOptionPane.showMessageDialog(null, "INVALID CHARACTER at First Name Field!" +
"\nPlease Enter A Valid FIRST NAME!"+
"\nEntry Should be Alpha Character A-Z!");
TOne.setText("");
TOne.requestFocus();
}
Code with The Action()
private class ClickWrite implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String MeUp, BufferOne, BufferTwo, BufferThree, BufferFour;
String Alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String Numeric = "0123456789";
int k,m,Determinator;
try
{
FileWriter WriteStream = new FileWriter("c:/random/IOFile.txt",true);
PrintWriter Write = new PrintWriter(WriteStream);
BufferOne = TOne.getText().toUpperCase();
BufferTwo = TTwo.getText().toUpperCase();
BufferThree = TThree.getText().toUpperCase();
BufferFour = TFour.getText();
Determinator = 0;
//Validating First Name Filed
if(TOne.getText().length() != 0 && Determinator == BufferOne.length())
{
for(k = 0; k < BufferOne.length(); k++)
{
for(m = 0; m < 26; m++)
{
if(BufferOne.charAt(k) == Alpha.charAt(m))
{
Determinator++;
}
}
}
}
else if(TOne.getText().length() == 0)
{
JOptionPane.showMessageDialog(null, "First Name Field REQUIRED!");
TOne.requestFocus();
}
else if(Determinator != BufferOne.length())
{
JOptionPane.showMessageDialog(null, "INVALID CHARACTER at First Name Field!" +
"\nPlease Enter A Valid FIRST NAME!"+
"\nEntry Should be Alpha Character A-Z!");
TOne.setText("");
TOne.requestFocus();
}
//Validating Middle Initial Field
else if(TTwo.getText().length() != 0 && Determinator == BufferTwo.length())
{
for(k = 0; k < BufferTwo.length(); k++)
{
for(m = 0; m < 26; m++)
{
if(BufferTwo.charAt(k) == Alpha.charAt(m))
{
Determinator++;
}
}
}
}
else if(TTwo.getText().length() == 0)
{
JOptionPane.showMessageDialog(null, "Middle Initial Field REQUIRED!");
TTwo.requestFocus();
}
else if(Determinator != BufferTwo.length() || Determinator > 1)
{
JOptionPane.showMessageDialog(null, "INVALID CHARACTER at Middle Initial Field!" +
"\nPlease Enter A Valid MIDDLE INITIAL!"+
"\nEntry Should be Alpha Character A-Z!");
TTwo.setText("");
TTwo.requestFocus();
}
//Validating Last Name Field
else if(TThree.getText().length() != 0 && Determinator == BufferThree.length())
{
for(k = 0; k < BufferThree.length(); k++)
{
for(m = 0; m < 26; m++)
{
if(BufferThree.charAt(k) == Alpha.charAt(m))
{
Determinator++;
}
}
}
}
else if(TThree.getText().length() == 0)
{
JOptionPane.showMessageDialog(null, "Last Name Field REQUIRED!");
TThree.requestFocus();
}
else if(Determinator != BufferThree.length())
{
JOptionPane.showMessageDialog(null, "INVALID CHARACTER at Last Name Field!" +
"\nPlease Enter A Valid LAST NAME!"+
"\nEntry Should be Alpha Character A-Z!");
TThree.setText("");
TThree.requestFocus();
}
//Validating SSN Field
else if(TFour.getText().length() != 0 && Determinator == BufferFour.length())
{
for(k = 0; k < BufferFour.length(); k++)
{
for(m = 0; m < 10; m++)
{
if(BufferFour.charAt(k) == Numeric.charAt(m))
{
Determinator++;
}
}
}
}
else if(TFour.getText().length() == 0)
{
JOptionPane.showMessageDialog(null, "SSN Field REQUIRED!");
TFour.requestFocus();
}
else if(Determinator != BufferFour.length())
{
JOptionPane.showMessageDialog(null, "INVALID CHARACTER at SSN Field!" +
"\nPlease Enter A Valid SSN!"+
"\nEntry Should be Numeric Numbers 0-9!");
TFour.setText("");
TFour.requestFocus();
}
else
{
BufferThree = "";
BufferThree = TThree.getText().toLowerCase() + " ";
BufferThree = BufferThree.substring(0,20);
BufferThree = BufferThree.substring(0,1).toUpperCase() + BufferThree.substring(1,20);
MeUp = BufferThree;
BufferOne = "";
BufferOne = TOne.getText().toLowerCase() + " ";
BufferOne = BufferOne.substring(0,15);
BufferOne = BufferOne.substring(0,1).toUpperCase() + BufferOne.substring(1,15);
MeUp = MeUp + BufferOne;
BufferTwo = "";
BufferTwo = TTwo.getText().toUpperCase() + " ";
BufferTwo = BufferTwo.substring(0,1);
MeUp = MeUp + BufferTwo;
BufferFour = "";
BufferFour = TFour.getText() + " ";
BufferFour = BufferFour.substring(0,9);
MeUp = MeUp + BufferFour;
Write.println(MeUp);
JOptionPane.showMessageDialog(null, "Record is written, c:/random/IOFile.txt");
WriteRecord.setEnabled(true);
AddRecord.setEnabled(true);
}
Write.close();
}
catch(IOException WhatIsTheMatter)
{
System.out.println("IO Error = " + WhatIsTheMatter.getMessage());
}
}
}
If i create different Buttons that would work, like FirstName Button LastName Button i.e.
But i'm wondering how do i do it in one button? and how can i write all of them under one action(), Thanks!!