Monday, April 23, 2012

J2ME ComboBox

import java.util.Hashtable;

import com.sun.lwuit.ComboBox;

public class J2MEComboBox extends ComboBox {

int index = 0;

Hashtable hashTable = new Hashtable();

public void addItem(Object key, Object value) {

hashTable.put(key, value);
super.addItem(key);
}

public Object getSelectedItemValue() {

Object selectedItem = super.getSelectedItem();
return hashTable.get(selectedItem);
}
}

Read More than one CSV files from a directory and Validate Number of Patterns in a row


import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class CSVRead {

private static File[] getBulkUploadPointsCSVFiles(File csvFolder) {

if (csvFolder == null) {
throw new IllegalStateException("Can't access input folder");
}

File[] directoryFiles = csvFolder.listFiles();
File[] csvFiles = new File[1];
int csvSize = 0;
for (int index = 0; index < directoryFiles.length; index++) {

String csvs = directoryFiles[index].toString();
if(csvs.endsWith(".csv")) {
if(csvSize == 0) {
csvFiles[0] = directoryFiles[index];
} else {
csvFiles = incrementFileArrayCapacity(csvFiles);
int capacity = csvFiles.length - 1;
csvFiles[capacity] = directoryFiles[index];
}
csvSize++;
}
}

if (csvSize == 0) {
throw new IllegalStateException("No CSV files inside the input folder");
}
return csvFiles;
}

public static File[] incrementFileArrayCapacity(File[] array) {

File[] newFile = new File[array.length + 1];

for (int j = 0; j < array.length; j++ ) {

newFile[j] = array[j];
}
return newFile;
}

private static boolean validatebulkUploadPointsFiles(File[] refFiles) {
long totalRecords = 0;
try {
for (File bulkPointsFile : refFiles) {
FileReader fileReader = new FileReader(bulkPointsFile);
BufferedReader buffReader = new BufferedReader(fileReader);
String currentLine;
String checkAnyLine = null;

while ((currentLine = buffReader.readLine()) != null) {
++totalRecords;
checkAnyLine = currentLine;
int noOfPattern = 0;
char[] chars = currentLine.toCharArray();
for(char c:chars)
if(c==',')
++noOfPattern;

if(noOfPattern!=3)
return false;
}
if(checkAnyLine == null)
return false;
}
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
public static void main(String[] arg) throws Exception {

File directory = new File("F:/WORKSPACE/Test/src/");
File[] bulkUploadPointsFiles = getBulkUploadPointsCSVFiles(directory);

// Validate the contents of the bulk upload balance file
validatebulkUploadPointsFiles(bulkUploadPointsFiles);

for (int index = 0; index < bulkUploadPointsFiles.length; index++) {

String s = bulkUploadPointsFiles[index].toString();

System.out.println(s);
BufferedReader CSVFile = new BufferedReader(new FileReader(s));

String dataRow = CSVFile.readLine();

while (dataRow != null) {

String[] dataArray = dataRow.split(",");
for (String item:dataArray) {
System.out.print(item + "\t");
}
System.out.println();
dataRow = CSVFile.readLine();
}
CSVFile.close();
System.out.println();
}
}
}

Increment Array Size by 1

int[] myArray = new int[1];
myArray[0] = 1;
myArray = incrementArrayCapacity(myArray);
myArray[1] = 2;
public static int[] incrementArrayCapacity(int[] array) {

int[] newArray = new int[array.length + 1];

for (int j = 0; j < array.length; j++ ) {

newArray[j] = array[j];
}
return newArray;
}