Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Got a little Problem  RSS feed

 
Ebunoluwa Adedoye
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello everyone,
i ve beeen trying to run a program but this method keeps given me error:


private void validateDate(){
if(lastTextField.getText().equals("")||firstTextField.getText().equals("")||addressTextField.getText.equals("")||
cityTextField.getText().equals("")||stateTextField.getText().equals("")||zipTextField.getText().equals("")||
phoneTextField.getText().equals(""));

JOptionPane.showMessageDialog(this,"please fill all fields");

else if (!firstTextField.getText().matches("[A-Z][a-zA-Z]*"))
JOptionPane.showMessageDialog(this,"Invalid first name");


else if (!lastTextField.getText().matches("[A-Z][a-zA-Z]*"))
JOptionPane.showMessageDialog(this,"Invalid last name");

else if (!addressTextField.getText().matches("\\d+\\s+([a-zA-Z]+|[a-zA-Z]+\\s[a-zA-Z]+)"))
JOptionPane.showMessageDialog(this,"Invalid address");

else if (!cityTextField.getText().matches("([a-zA-Z]+|[a-zA-Z]+\\s[a-zA-Z]+)"))
JOptionPane.showMessageDialog(this,"Invalid city");

else if (!stateTextField.getText().matches("([a-zA-Z]+|[a-zA-Z]+\\s[a-zA-Z]+)"))
JOptionPane.showMessageDialog(this,"Invalid state");

else if (!zipTextField.getText().matches("\\d{5}"))
JOptionPane.showMessageDialog(this,"Invalid zip");

else if (!phoneTextField.getText().matches("([1-9]\\d{2}-[1-9]\\d{2}-\\d{4}"))
JOptionPane.showMessageDialog(this,"Invalid phone number");

else
JOptionPane.showMessageDialog(this,"thank u");
}
}
the compiler is saying else without if.
thanks in advance.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's a ; at the end of your first if
 
aslam parveez
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


why is that you have placed a semicolon at the end of this statement !!!
 
Ebunoluwa Adedoye
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks i have seen it.
 
Vladimir Nesov
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of thisyou should have list of pattern to match as error codes corresponding to them like in(better yet create structure for pattern, but that would be overkill here). It's against code duplication.

Instead of this:simplier to create a collection and then check all its elements for this condition. This way you'd only specify in single place what you want of all these String things:(it'd be better to create structure to manage that information and check information correctness while initializing it (within it))


P.S. Whole point: write it like this and you'll never loose that ';' after 17th if statement.
[ June 29, 2006: Message edited by: Vladimir Nesov ]
 
Richard Green
Ranch Hand
Posts: 536
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I haved actually extended JTextField in my project (and called it GTextField).

GTextField surname = new GTextField("",true,true);
where arg0 => text
arg1=>boolean indiciating whether its mandatory or ont
arg1=>boolean indiciating whether its a text-only field (numbers are not allowed)

so, my validation code is lot cleaner and looks like this

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!