import java.awt.*;
import javax.swing.*;
import java.sql.*;
import java.io.*;
import java.util.*;
import javax.swing.JScrollPane;
import java.awt.event.*;
import java.lang.*;
public class DataPrep
extends JFrame {
//Jlist to shows fields names from datasets.
//button to save,copy desired fields, submit and exit
//textfield to enter file name
int numberOfColumns;
int userlistsize;
String[] column_names;
String[] usercol_names;
//file chooser to provide save option.
Double[][] normalizeddata;
//connection var to connect to excel sheet
//Begin of Constructor
public DataPrep()
{
super("DataPreprocessing");
//Create panel to add title
//Create panel to add jlist component.
//Setting layout to boxlayout.
jlistpanel.
setBorder(BorderFactory.
createEmptyBorder(10,
60,
60,
60));
//Create panel to add sumit and exit button
buttonpanel.
setBorder(BorderFactory.
createEmptyBorder(0,
10,
40,
90));
buttonpanel.
add(Box.
createHorizontalGlue());
heading=
new JLabel("Data Preprocessing");
heading.
setFont(new Font("Arial",
Font.
ITALIC,
30));
heading.
setHorizontalAlignment(JLabel.
CENTER);
//Creat panel to add save buttona and textfield
filenamelabel=
new JLabel("Click the Button to Save Your Normalized Data File(eg.output.txt):");
filename.setEditable(false);
save.addActionListener(new savefile());
filepane.add(filenamelabel);
filepane.add(save);
filepane.add(filename);
title=
new JLabel("Select the Interger Type Attributes From the List Below:");
//Calling method in which coloumn names are read.
readcolnames();
available_cols=
new JList(v
);
//Create a scrollpane to which jlist panel is added.
jlistpanel.add(spane);
copy_button=
new JButton("Copy>>>");
//adding actionlistener to copy button
copy_button.addActionListener
(
{
{
userselected_cols.setListData(available_cols.getSelectedValues());
}
}
);
jlistpanel.add(copy_button);
userselected_cols=
new JList();
userselected_cols.setFixedCellWidth(100);
userselected_cols.setFixedCellHeight(15);
jlistpanel.add(spane2);
jlistpanel.setSize(50,20);
submit_button=
new JButton("Fuzzify");
//adding actionlisterner to submit button
submit_button.addActionListener
(
{
{
userlistsize=userselected_cols.getModel().getSize();
usercol_names=new String[userlistsize+1];
System.
out.
println("\nNo of attributs user selected is:"+userlistsize
);
System.
out.
println("\nUser selected Col names are:");
for(int i=1;i<(userlistsize+1);i++)
{
//get the user selected names to string array
usercol_names[i]=userselected_cols.getModel().getElementAt(i-1).toString();
System.
out.
println(usercol_names[i]
);
if(i==userlistsize)
selectedlist=selectedlist+usercol_names[i];
else
selectedlist=selectedlist+usercol_names[i]+",";
}
System.
out.
println("The selected string is:: "+selectedlist
);
if(outfilename==null)
{
}
else
{
readselectedcol(outfilename);
JOptionPane.
showMessageDialog(null,
"File with Processed data is created Successfully",
"alert",
JOptionPane.
INFORMATION_MESSAGE);
}
}
}
);
buttonpanel.add(submit_button);
//adding actionlistener to exit button
exit_button.addActionListener
(
{
//dispose the frame when user clicks exit button
dispose();
}
}
);
buttonpanel.add(exit_button);
// getContentPane().add(labelpanel,BorderLayout.NORTH);
}
//end of constructor
//Implementing actionlistener for save button
{
{
int val=jfc.showSaveDialog(DataPrep.this);
{
//get the file name from the jfilechooser component
savefile=jfc.getSelectedFile();
System.
out.
println("Saved file name is:"+savefile.
getName());
outfilename=savefile.getName();
//set text field with file name
filename.setText(savefile.getName());
}
}
}
void readcolnames()
{
try
{
//loading jbdc.odbc drivers
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//connection establishment
//Creating statement to execute query
//execute query and get values to result set
ResultSet rs=st.
executeQuery("Select * From [Sheet1$]");
//get the column count
numberOfColumns=rsmd.getColumnCount();
System.
out.
println("No of columns in excel sheet are:"+numberOfColumns
);
System.
out.
print("Column Names are:");
column_names=new String[numberOfColumns+1];
for(int i=1;i<=numberOfColumns;i++)
{
column_names[i]=rsmd.getColumnName(i);
v.add(column_names[i]);
System.
out.
print("\n"+column_names[i]
);
}
st.close();
con.close();
}
{
System.
err.
print("Exception in selecting from xls: ");
System.
err.
println(e.
getMessage());
}
}
String selectedcolumn_names[];
int selectedcolumncount;
void readselectedcol
(String outputfile
) {
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Formatter fmt = new Formatter();
Formatter smt = new Formatter();
Formatter tmt = new Formatter();
int noofrows=0;
ResultSet rs = st.
executeQuery("Select "+selectedlist+
" From [Sheet1$]");
ResultSet newrs=st.
executeQuery("Select "+selectedlist+
" From [Sheet1$]");
selectedcolumncount=rsmd.getColumnCount();
selectedcolumn_names=new String[selectedcolumncount+1];
for(int i=1;i<=selectedcolumncount;i++)
{
selectedcolumn_names[i]=rsmd.getColumnName(i);
v.add(column_names[i]);
System.
out.
print("\n"+selectedcolumn_names[i]
);
}
while(rs.next())
{
noofrows++;
}
System.
out.
println("No-of-Rows "+noofrows
);
numberOfColumns = rsmd.getColumnCount();
tempdata=new Double[numberOfColumns+1][noofrows+1];
System.
out.
println(rsmd.
getColumnCount());
double Data[][]=new double[noofrows+1][numberOfColumns+1];
double output[][]=new double[noofrows+1][(numberOfColumns*3)+1];
int temp11=1;
while(newrs.next())
{
for(int j=1;j<=numberOfColumns;j++)
{
try
{
Data[temp11][j]=
Double.
parseDouble(newrs.
getString(j
));
System.
out.
print(Data[temp11][j]+
" ");
}
{
System.
err.
print("Exception: ");
System.
err.
println(ex.
getMessage());
}
}
temp11++;
}
File normalized_file=
new File(outputfile
);
data=new Double[noofrows+1][(numberOfColumns*3)+1];
for(int k=1;k<=noofrows;k++)
{
try
{
for(int l=1;l<=numberOfColumns*3;l++)
{
fmt = new Formatter();
data[k][l]=Data[l][k];
System.
out.
printf(fmt.
format("%.2f",output[k][l]
).
toString()+
" ");
}
// System.out.println();
}
{
System.
out.
println("Exception internally "+eg
);
}
}
for(int i=1;i<=noofrows;i++)
{
for(int j=1;j<=numberOfColumns;j++)
{
//System.out.print(Data[i][j]+" ");
}
//System.out.println();
}
int mmm=3;
for(int col=1;col<=numberOfColumns;col++)
{
try
{
double x1[] =new double[Data.length+1];
double x2[] =new double[Data.length+1];
System.
out.
println("x1 length "+x1.
length);
for(int n=1; n<=noofrows; n++)
{
x1[n]=Data[n][col];
// System.out.print(x1[n]+" ");
}
double max=0;
for(int n=1;n<=noofrows;n++)
{
if(x1[n]>max)
{
max = x1[n];
}
}
double min = x1[1];
for(int n=1;n<=noofrows;n++){
if(x1[n]<min){
min = x1[n];
}
} double range=0;
range=(max-min)/3;
double a1=min + range;
double a2=(a1) + range;
double a3=(a2) + range;
double m=0,m1=0;
double c=0,c1=0; double y[]= new double[x1.length];
double y1[]= new double[x1.length];
double y2[]= new double[x1.length];
double x3=((a1-0.2)+(a2+0.2))/2;
// System.out.println("x3 value is " + x3);
for(int k=1;k<=noofrows;k++)
{
x2[k]=(x1[k]);
}
//int mmm=1;
for(int z=1;z<=noofrows;z++)
{
if((x2[z]>=min)&&(x2[z]<(a1-0.2)))
{
m=(1/(min-a1));
// System.out.println("m value is " + m);
c=-(m*(a1));
// System.out.println("C value is " + c);
y[z]= m*(x2[z]) + c;
y1[z]=0;
y2[z]=0;
fmt = new Formatter();
smt = new Formatter();
tmt = new Formatter();
}
if((x2[z]>a1)&& (x2[z]<=x3))
{
m=1/(x3-(a1-0.2));
c=-(m*(a1-0.2));
y1[z]=c+m*(x2[z]);
y[z]=0;
y2[z]=0;
fmt = new Formatter();
smt = new Formatter();
tmt = new Formatter();
}
if((x2[z]>=(a1-0.2)) && (x2[z]<=a1))
{
m=(1/(min-a1));
c=-(m*(a1));
y[z]= m*(x2[z]) + c;
m1=1/(x3-(a1-0.2));
c1=-(m1*(a1-0.2));
y1[z]=c1+(m1*(x2[z]));
y2[z]=0;
fmt = new Formatter();
smt = new Formatter();
tmt = new Formatter();
}
if((x2[z]> x3)&& (x2[z]<a2))
{
m=1/(x3-(a2+0.2));
c=-m*(a2+0.2);
y[z]=0;
y1[z]=m*(x2[z])+c;
y2[z]=0;
fmt = new Formatter();
smt = new Formatter();
tmt = new Formatter();
}
if(x2[z]>(a2+0.2) && (x2[z]<=a3))
{
m=1/(a3-a2);
c=-m*(a2);
y[z]=0;
y1[z]=0;
y2[z]=(m*(x2[z]))+c;
fmt = new Formatter();
smt = new Formatter();
tmt = new Formatter();
}
if ((x2[z]>=a2) && (x2[z]<=(a2+0.2)))
{
m=1/(x3-(a2+0.2));
c=-m*(a2+0.2);
m1=1/(a3-a2);
c1=-m1*(a2);
y[z]=0;
y1[z]=m*(x2[z])+c;
y2[z]=m1*(x2[z])+c1;
fmt = new Formatter();
smt = new Formatter();
tmt = new Formatter();
}
output[z][mmm-2]=y[z];
output[z][mmm-1]=y1[z];
output[z][mmm]=y2[z];
// System.out.println(mmm);
}//end of for in incrementing z
mmm=mmm+3;
}
{
System.
out.
println("Exception in fuzzifying part "+e1
);
}
}///
//int dd=1;
//output formatting
for(int k=1;k<=noofrows;k++)
{
for(int l=1;l<=numberOfColumns*3;l++)
{
if((l%3)>0)
{
fmt = new Formatter();
norm_fw.write(fmt.format("%.2f",output[k][l]).toString());
norm_fw.write(" ");
}
else
{
fmt = new Formatter();
norm_fw.write(fmt.format("%.2f",output[k][l]).toString());
norm_fw.write(" ");
}
}
// System.out.println();
// norm_fw.newLine();
//System.out.println();
}
st.close();
con.close();
norm_fw.close();
}///end of try
{
System.
out.
println("Exception in logic: "+e
);
}
}
}