The following code illustrates how to derive an age from a given DOB.
This example uses the DOB format YYYY-MM-DD.
This can be altered to any format by changing the substrings taken from the original DOB input.
String dob = “1984-09-20″;
//TAKE SUBSTRINGS OF THE DOB SO SPLIT OUT YEAR, MONTH AND DAY
//INTO SEPERATE VARIABLES
int yearDOB = Integer.parseInt(dob.substring(0, 4));
int [...]
Posts Tagged ‘Java’
JAVA – How To Calculate An Age From A Date Of Birth (DOB)
June 15, 2009Java – Random Number Generator / How To Generate A Random Number
June 9, 2009This is a simple method for generating a random number between zero (0) and a given number.
The following code example with generate a random number between 0 and 49.
Although the value of maxNumber is set to 50, Java will start the count at 0.
If you want a number between 0 and 50, set the value [...]
JAVA – Remove Row from A JTable
March 30, 2009How to remeve a row from a JTable.
This example illustrates how to remove a row from an existing JTable called table.
The row to be deleted will be the row that has been selected by clicking the mouse on it.
//CREATE MODEL INSTANCE FROM EXISTING TABLE
DefaultTableModel model = new DefaultTableModel();
model = (DefaultTableModel) table.getModel();
//DELETE THE SELECTED ROW
model.removeRow(table.getSelectedRow());
//INSERT A [...]
JAVA Check For A Valid Email Address String
February 12, 2009How to check for a valid email address string.
Example Code:
String email = “test@test.com”;
Pattern p = Pattern.compile(“.+@.+\\.[a-z]+”);
Matcher m = p.matcher(email);
boolean matchFound = m.matches();
if(matchFound){
System.out.println(“EMAIL OK”);
}else{
System.out.println(“EMAIL ERROR”);
}
Convert A String To Proper Case In Java
February 12, 2009Converting a string to proper case in Java.
e.g. convert “roBErT” to “Robert”.
Example code:
String forename = “roBErT”;
String forenameforenameInitial = “”;
forename = forename.toLowerCase();
forenameInitial = forename.substring(0, 1).toUpperCase();
forename = forenameInitial + forename.substring(1,forename.length());
System.out.println(forename);
The string vaiable forename now has a value of “Robert”.
JAVA Check To See If A File Already Exists
February 5, 2009How to check if a file already exists.
String file = “c:/file.csv”;
File f = new File(file);
if(f.exists()){
// code if file exists
}else{
// code if file doesn’t esist
}
The following example checks to see if the file exists, then asks the user if they want to overwrite it.
If the users selects yes, then the original file is deleted.
String file [...]
How To Convert A Float Value to An Integer
February 5, 2009JAVA – how to convert a float value to an interger.
Example 1:
float a = 110.25;
int a = (int)(a + 0.5f);
int a will be rounded down version of float a. The value will be 110.
Example 2:
float b = 110.75;
int b = (int)(b + 0.5f);
int b will be rounded up version of float b. The value will [...]
JAVA – How To APPEND To A Flat File (CSV, TEXT etc.)
February 4, 2009Appending to a file.
The following example will APPEND the text “More Sample Text” to c:/test.csv.
If test.csv doesn’t already exist then it will be created
String fileName = “c:/test.csv”;
try {
BufferedWriter out = new BufferedWriter(new FileWriter(fileName , true));
out.write(“More Sample Text”);
out.close();
} catch (IOException e) {
}