• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Issue on putting in order the following code

 
Marcelo Bordolli
Ranch Hand
Posts: 46
Android Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have an api that have 3 textfields:

name: (gere goes textfiel1)

Last Name: (Second textfiel)

Id: (number 3 textfield)

then i have a button named SAVE , wen i hit SAVE all 3 textfields info goes to a txt.
Now the thing is that i want to control what people may put in the field i mean i dont want to save in the name textfield a number you know so i got the following code and i think i need to order this properly so this could work:

String line="";
String errorMsg = "";
boolean error = false;
int ci = 0;

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
if (jTextField1.getText().length() == 0 || jTextField1.getText().trim().length() == 0){
error = true;
errorMsg = "Must put a name ";

}else if (jTextField2.getText().length() == 0 || jTextField2.getText().trim().length() == 0){
error = true;
errorMsg = "Must put a last name ";

}else if (jTextField3.getText().length() == 0 || jTextField3.getText().trim().length() == 0){
error = true;
errorMsg = "Must put an ID ";

}else{
try{
ci = Integer.parseInt(jTextField3.getText());
}catch(NumberFormatException ne){
error = true;
errorMsg = "The ID must be numeric.";
}
}if(error){
JOptionPane.showMessageDialog(this, errorMsg, "Error ", JOptionPane.ERROR_MESSAGE);
}

String line= jTextField1.getText()+" "+jTextField2.getText()+" "+ jTextField3.getText()+"\n" ;

try{ //This part works fine
FileWriter fw= new FileWriter("C:\\Grupo.txt", rootPaneCheckingEnabled);
fw.write(line);
fw.close();

}catch(IOException e){};
}

Thank you ranchers !!
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please put the code inside code tags.

The code you provided is not doing what you wanted in the first place, because even if the user does not provide valid input, you are just showing a message and then you proceed with saving the input into file. What is the point of cheking if you can save invalid data?
Good practice is to do validation before performing such an action (e.g. saving to a file or database), and if the input is not valid disable the action invocation (in this case disable JButton).

Also, you're declaring line twice in your code.
Another improvement would be to implement separate methods for validating, to avoid code repetition - one method for validating text input, another for integers, etc.
 
K. Tsang
Bartender
Posts: 3457
14
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi please use code tags next time.

As for your question, check out DocumentListener interface.



The ValidTextChecker is an inner class




Hope this helps.
 
Marcelo Bordolli
Ranch Hand
Posts: 46
Android Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you kemal i fixed the thing whit your hint i just put a jButton2.setenable(false) and another changes that i spotted and all work fine.
Thank you k man!
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any time!
I would recommend you post your code snippet after you accomplish your task with help of anyone here, so other users may benefit if they experience the same issue. There might be some further suggestions to improve your code.

Also, I noticed from your posts that you are using NetBeans GUI builder extensively, which might not be the best idea until you get comfortable with Swing.

Cheers!
 
K. Tsang
Bartender
Posts: 3457
14
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kemal Sokolovic wrote:Also, I noticed from your posts that you are using NetBeans GUI builder extensively, which might not be the best idea until you get comfortable with Swing.


Yes I agree with Kemal about the NetBeans GUI builder. Playing with Swing components can be challenging
 
Marcelo Bordolli
Ranch Hand
Posts: 46
Android Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks i will post the code to help other that may have the same issues.

And i will take your advice on the netbeans for swing. the drag and drop save you a lot of time but on the other hand it gives you new pains in the ass.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic