hi all, i'm new to this forum and java
i'm working on my thesis with "market basket analysis" topic
and my lecturer told me to use java (while i'm new to it)
i have a set of item like this (example)
id|transaction| item
1 1 a
2 1 b
3 1 d
4 2 c
4 2 a
...
etc
where each transaction have many item to buy from market
from that itemset i need that item to be shown like this
[A]
item | count
c 4
f 4
a 3
m 3
p 3
b 3
[A] table is an item that is frequently bought from the store
and from [A] table i need to make a tree
in this case i don't know how to use tree in java so i use array
id pid count
f ~ 4
c f 3
a c 3
m a 2
p m 2
b a 1
m b 1
b f 1
c ~ 1
b c 1
p b 1
i've reached [A] and is almost
but i keep getting this array out of bounds...
any help is really appreciated
here is my code
package generatefptree;
import com.mysql.jdbc.Driver;
import java.sql.*;
import javax.sql.*;
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
/*String in_jumlah;
int min_support;
System.out.print("Masukkan jumlah minimal support : ");
try{
in_jumlah = br.readLine();
}catch(IOException e){
System.out.println("Input error!");
}
min_support = Integer.parseInt(in_jumlah);*/
//buat koneksi
Class.forName("com.mysql.jdbc.Driver");
String connectionUrl = "jdbc:mysql://localhost/tes?" +
"user=root&password=";
Connection con = DriverManager.getConnection(connectionUrl);
Statement stmt = null;
ResultSet rs = null;
//ambil data untuk header table
String SQL = "SELECT DISTINCT c.item, s.jumlah " +
"FROM contoh1 c INNER JOIN ( " +
"SELECT DISTINCT item, COUNT(*) AS jumlah " +
"FROM contoh1 " +
"GROUP BY item " +
") s ON s.item = c.item " +
"WHERE s.jumlah > 2 " +
"ORDER BY s.jumlah DESC";
stmt = con.createStatement();
rs = stmt.executeQuery(SQL);
int loop = 0;
String []header = new String[6];
int []count = new int[6];
int tes;
//inisialisasi header table jumlah awal = 0
for(tes = 0; tes < count.length; tes++){
count[tes] = 0;
}
while (rs.next()) {
header[loop] = rs.getString("item");
count[loop] = rs.getInt("Jumlah");
System.out.println(header[loop] + " : " + count[loop]);
loop++;
}
//ambil data untuk tree
String SQL2 = "SELECT DISTINCT id_transaksi, COUNT(*) AS jumlah " +
"FROM contoh1 " +
"GROUP BY id_transaksi " +
"ORDER BY Jumlah DESC ";
stmt = con.createStatement();
rs = stmt.executeQuery(SQL2);
int jumlah_data = 0;
while(rs.next()){
jumlah_data++;
}
String []fptree_id = new String[1500];
String []fptree_pid = new String[1500];
int []count2 = new int[1500];
int tes2;
//inisialisasi tree table jumlah awal = 0
for(tes2 = 0; tes2 < count2.length; tes2++){
/*count2[tes2] = 0;
fptree_id[tes2] = null;
fptree_pid[tes2] = null;*/
fptree_id[tes2] = new String();
fptree_pid[tes2] = new String();
count2[tes2] = 0;
}
int loop2, loop3;
String temp_id = null;
int a = 0;
int b = 0;
int c = 0;
String temp_pid = null;
//looping scan per transaksi
for(loop2 = 1; loop2 <= jumlah_data; loop2++){
int first = 0;
//looping scan per header table
for(loop3 = 0; loop3 < header.length; loop3++){
//System.out.println(header[loop3]);
String SQL3 = "SELECT * FROM contoh1 WHERE id_transaksi = " + loop2;
stmt = con.createStatement();
rs = stmt.executeQuery(SQL3);
int x = 0;
while((rs.next()) && (x < 1)){
String item = rs.getString("item");
if(header[loop3].equals(item)){
c = 0;
temp_id = item;
if(first == 0){
if(count2[0] == 0){
fptree_id[a] = item;
fptree_pid[a] = null;
count2[a]++;
a++;
}else{
for(b = 0; b <= fptree_id.length; b++){
if(fptree_id[b].equals(item)){
count2[b]++;
fptree_pid[b] = null;
b = fptree_id.length;
}else{
c++;
}
}
if(c >= fptree_id.length){
fptree_id[a] = item;
fptree_pid[a] = null;
count2[a]++;
a++;
}
}
temp_pid = item;
x = 1;
first = 1;
}else{
for(b = 0; b <= fptree_id.length; b++){
if((fptree_id[b].equals(item)) && (fptree_pid[b].equals(temp_pid))){
//fptree_id[b] = item;
//fptree_pid[b] = temp_pid;
count2[b]++;
b = fptree_id.length;
}else{
c++;
}
}
if(c >= fptree_id.length){
fptree_id[a] = item;
fptree_pid[a] = temp_pid;
count2[a]++;
a++;
}
temp_pid = item;
//break looping
x = 1;
}
}else{
//nothing else
}
}
}
}
System.out.println("ID | PID | Count");
for(c = 0; c < count2.length; c++){
System.out.println(" " + fptree_id[c] + " | " + fptree_pid[c] +
" | " + count2[c]);
}
} catch (SQLException e) {
System.out.println("SQL Exception: "+ e.toString());
} catch (ClassNotFoundException cE) {
System.out.println("Class Not Found Exception: "+ cE.toString());
}
}
}
and here is the result
run:
f : 4
c : 4
p : 3
b : 3
a : 3
m : 3
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1500
at generatefptree.Main.main(Main.java:140)
Java Result: 1