Rob Bamforth's Blog
Java, SQL,T-SQL, MySQL, Networking, SQL Server, PHP, COM, DBA, ODBC

Posts Tagged ‘Java’

JAVA – How To Calculate An Age From A Date Of Birth (DOB)

June 15, 2009

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 [...]

Java – Random Number Generator / How To Generate A Random Number

June 9, 2009

This 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 – Set The Position Of The ScrollBar In JScrollPane()

June 4, 2009

The following codes assumes that you have created a JScrollPane() called scrollPane

int x = 0;
int y = 10;
scrollPane.getViewport().setViewPosition(new java.awt.Point(x, y));

In this example x sets the horizontal position of a horizontal scroll bar.
y sets the vertical position of a vertical scroll bar – in this case the vertical scroll bar will appear 10 pixels down from [...]

JAVA – Remove Row from A JTable

March 30, 2009

How 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, 2009

How 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, 2009

Converting 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”.

Launch Web Browser From Java Application

February 12, 2009

Opening a web browser from a Java application.
Bare Bones Browser is a great tool for opening a web browser on a given URL from a Java / Swing application.
Step 1 – Firstly create an empty class called BareBonesBrowserLaunch.java.
Copy and paste the entire section of code below into the class:
/////////////////////////////////////////////////////////
//  Bare Bones Browser Launch                          //
//  Version [...]

JAVA Check To See If A File Already Exists

February 5, 2009

How 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, 2009

JAVA – 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, 2009

Appending 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) {
}