wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Any help would be appreciated Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Any help would be appreciated " Watch "Any help would be appreciated " New topic
Author

Any help would be appreciated

Nicholas Lefebvre
Greenhorn

Joined: Feb 05, 2013
Posts: 23
I finished a number to vaildate south african id numbers. I wounder if there where any mistakes I made and if there were any ways to make it shorter. Please can you look at my code and tell me any tips or criticism you might have which i will used to learn from. Thanks

import java.util.Calendar;

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/*
* NewJFrame.java
*
* Created on Mar 27, 2013, 11:14:51 AM
*/
/**
*
* @author Nicholas.Lefebvre
*/
public class NewJFrame extends javax.swing.JFrame {

/** Creates new form NewJFrame */
public NewJFrame() {
initComponents();
}

/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jInternalFrame1 = new javax.swing.JInternalFrame();
jLabel1 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
jTextField2 = new javax.swing.JTextField();
jLabel3 = new javax.swing.JLabel();
jTextField3 = new javax.swing.JTextField();
jTextField4 = new javax.swing.JTextField();
jLabel4 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
jTextField5 = new javax.swing.JTextField();
jLabel5 = new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jInternalFrame1.setVisible(true);

jLabel1.setText("Enter ID Number :");

jLabel2.setText("Lenght :");

jTextField2.setEditable(false);

jLabel3.setText("AGE :");

jTextField3.setEditable(false);
jTextField3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextField3ActionPerformed(evt);
}
});

jTextField4.setEditable(false);

jLabel4.setText("Status :");

jButton1.setText("SUBMIT");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});

jTextField5.setEditable(false);

jLabel5.setText("Gender");

javax.swing.GroupLayout jInternalFrame1Layout = new javax.swing.GroupLayout(jInternalFrame1.getContentPane());
jInternalFrame1.getContentPane().setLayout(jInternalFrame1Layout);
jInternalFrame1Layout.setHorizontalGroup(
jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jInternalFrame1Layout.createSequentialGroup()
.addGroup(jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jInternalFrame1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, 114, Short.MAX_VALUE)
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 114, Short.MAX_VALUE)
.addComponent(jLabel2)
.addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, 114, Short.MAX_VALUE)
.addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, 114, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 262, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, 262, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, 262, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 262, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, 262, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(jInternalFrame1Layout.createSequentialGroup()
.addGap(196, 196, 196)
.addComponent(jButton1)))
.addContainerGap(158, Short.MAX_VALUE))
);
jInternalFrame1Layout.setVerticalGroup(
jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jInternalFrame1Layout.createSequentialGroup()
.addGap(31, 31, 31)
.addGroup(jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel4))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel3))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel5))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 43, Short.MAX_VALUE)
.addComponent(jButton1)
.addGap(32, 32, 32))
);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jInternalFrame1, javax.swing.GroupLayout.Alignment.TRAILING)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jInternalFrame1)
);

pack();
}// </editor-fold>

private void jTextField3ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

//check lenght methode
String Idnumber = jTextField1.getText();
int lenght = Idnumber.length(); //Gets the lenght of the string and stores it in the variable lenght
System.out.println(lenght);// prints the lenght that is stored in lenght

if(lenght==13){
jTextField2.setText("Correct");
}else{
jTextField2.setText("Incorrect");
}

// end of check lenght

//method to validate the id number
int d1 = Integer.parseInt(""+Idnumber.charAt(0));
int d2 = Integer.parseInt(""+Idnumber.charAt(1));
int d3 = Integer.parseInt(""+Idnumber.charAt(2));
int d4 = Integer.parseInt(""+Idnumber.charAt(3));
int d5 = Integer.parseInt(""+Idnumber.charAt(4));
int d6 = Integer.parseInt(""+Idnumber.charAt(5));
int d7 = Integer.parseInt(""+Idnumber.charAt(6));
int d8 = Integer.parseInt(""+Idnumber.charAt(7));
int d9 = Integer.parseInt(""+Idnumber.charAt(8));
int d10 = Integer.parseInt(""+Idnumber.charAt(9));
int d11 = Integer.parseInt(""+Idnumber.charAt(10));
int d12 = Integer.parseInt(""+Idnumber.charAt(11));
int d13 = Integer.parseInt(""+Idnumber.charAt(12));


String dd1 = d1+"";
String dd2 = d2+"";
String dd3 = d3+"";
String dd4 = d4+"";
String dd5 = d5+"";
String dd6 = d6+"";
String dd7 = d7+"";
String dd8 = d8+"";
String dd9 = d9+"";
String dd10 = d10+"";
String dd11 = d11+"";
String dd12 = d12+"";

int totalA = d1+d3+d5+d7+d9+d11;



String B = dd2+dd4+dd6+dd8+dd10+dd12;

int totalB= Integer.parseInt(B);
int b= 2;
int t;
t=b*totalB;
String totalBa = Integer.toString(t) ;


int totalB1 = Integer.parseInt(""+totalBa.charAt(0));
int totalB2 = Integer.parseInt(""+totalBa.charAt(1));
int totalB3 = Integer.parseInt(""+totalBa.charAt(2));
int totalB4 = Integer.parseInt(""+totalBa.charAt(3));
int totalB5 = Integer.parseInt(""+totalBa.charAt(4));
int totalB6 = Integer.parseInt(""+totalBa.charAt(5));
int totalB7 = Integer.parseInt(""+totalBa.charAt(6));

int totalC = totalB1 + totalB2 + totalB3 + totalB4 + totalB5 + totalB6 + totalB7;
System.out.println("totalc"+totalC);
int totalD = totalC + totalA;


int controldigit=totalD%10;
System.out.println("totalD =" + totalD);
System.out.println("control digit =" +controldigit);

if(d1==controldigit){
jTextField3.setText("VALID ID NUMBER");
}else{
jTextField3.setText("VALID ID NUMBER");
}

//end of validate ID number methode

// start of determine age methode
String dateOfBirth= dd1+dd2;
int yeara = 1900;

int yearb = 2000;




int dateofbirthint= Integer.parseInt(dateOfBirth);

int dateofBirthB;

if (dateofbirthint<20){

dateofBirthB = dateofbirthint + yearb;


}else{

dateofBirthB = dateofbirthint + yeara;
}

int year = Calendar.getInstance().get(Calendar.YEAR);

int age = year - dateofBirthB ;
String ages = Integer.toString(age);
jTextField4.setText(ages);
//end of determine age method

//start of determine gender

String genderdigits = dd7+dd8+dd9+dd10;
int genderintdigit = Integer.parseInt(genderdigits);

if(genderintdigit<5000){
jTextField5.setText("Female");
}else{
jTextField5.setText("Male");
}
//end of determine gender


}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>

/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {

@Override
public void run() {
new NewJFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JInternalFrame jInternalFrame1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
private javax.swing.JTextField jTextField3;
private javax.swing.JTextField jTextField4;
private javax.swing.JTextField jTextField5;
// End of variables declaration
}
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Nicholas Lefebvre wrote:I finished a number to vaildate south african id numbers. I wounder if there where any mistakes I made and if there were any ways to make it shorter. Please can you look at my code and tell me any tips or criticism you might have which i will used to learn from. Thanks


What do you mean when you say
I finished a number to vaildate south african id numbers.
? Can you explain what is it that you are attempting to achieve here? The problem statement.

Secondly, always use code tags while referring ti Java code:




~ Mansukh
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

You want to make code which validates South African ID numbers, and you want it shorter than that? Well, you've got a couple of hundred lines of GUI code. You don't need any of that to validate a number, so for a start you could remove all of the GUI stuff.

You may think I'm joking, but I'm only sort of joking. Seriously, if you want us to look at your validation code then don't bury it in a mountain of unrelated code. Just post the method which does the validation and ask about that. You do have it in a method, don't you?
Nicholas Lefebvre
Greenhorn

Joined: Feb 05, 2013
Posts: 23
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11153
    
  16

too much code and not enough comments. And comments that say "End of <whatever> method" are pointless and distracting.

Why in heaven would you loop through Idnumber character by character (lines 18-20) and then immediately afterwards explicitly look at each (lines 22-34)? loop through the string once and do everything you need. Your code will break if the length of an ID changes to something other than 13 characters.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Nicholas Lefebvre
Greenhorn

Joined: Feb 05, 2013
Posts: 23
So how would i break the code if the id number is not 13
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11153
    
  16

You tell me. I have no idea what you are trying to do, as I don't know anything about South African ID numbers.

When you code, you should spend 90% of your time thinking about what you need to do, and 10% of your time writing code to do it.

So, start off by explaining in English how you validate things. Then explain in English how you would do it. Say things like

Validate the string is exactly thirteen characters


do not say

call .length() to determine how long the string is.

The first is a what. The second is a how.

Until you can explain exactly what needs to be done, you shouldn't consider anything about the how.

 
Consider Paul's rocket mass heater.
 
subject: Any help would be appreciated
 
Similar Threads
Text from txtfield in txt file and from file to combobox
java programming
JTable Problem with netbeans
Pressing a button to open a new window
How to add JTable into a JPanel