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

if((fptree_id[b].equals(item)) && (fptree_pid[b].equals(temp_pid))){

I assume your error happens here.
Check your logic to see what that the value of b is valid. Use some println() to show the value of b and the value of any other variable that could effect how the loop works.

The for loop allows b to be equal to the length of the array. The index of last item in an array is one less.

thanx NormR1
it seems that i tried to access an array with index more than it's array length
now i'm getting a new error "null pointer exception"
it still on the same line
i've tried to print some variable
at this point i added some code

for(b = 0; b < fptree_id.length; b++){
                                System.out.print(b);
                                System.out.print(fptree_id[b]);
                                System.out.println(fptree_pid[b]);
                                //System.out.println(temp_pid + item);
                                
                                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;
                                    System.out.println(b);
                                }else{
                                    c++;
                                }
                            }

and it's result is

0 f null
1 null null

where

fptree_id[1] = null
item = c
fptree_pid[1] = null
temp_pid = f

i wonder why it isn't break on the first loop where

fptree_id[0] = f
item = c
fptree_pid[0] = null
temp_pid = f

still trying to find how to handle this error though X_X

If the contents of the array is null at the index location, then you have a design or coding error.
Is it possible that some elements in the array can be empty? Then the code must test for it.
If there should NOT be any empty slots then check your logic to see why your leaving the empty slot.

yeah at first the array is empty (or null if u see mine)
and then the array is filled with some value (in this case is item from database and temp_id from fptree_id before)
and it break on the second loop where

(null == c) && (null == f)

when i googling i found many people find null pointer exception and but the case is different...
i'll check my logic again
and thanx for fast reply :)

fptree_pid[a] = null;

What does this line of code at line 115 do?

ah that one is for nothing
fptree_pid stands for parent id in fp-tree
parent id is a node before reaching the item
in my attachment example is
to reach node A it must traverse through C and F
so it's parent id is F while F's parent id is C

i've been searching how to use tree in java for these but i think my knowledge still not there yet
so i plan to make an array like in table


id pid count
c ~ 4
f c 3
a f 3
m a 2
p m 2
b a 1
m b 1
b c 1
p b 1
f ~ 1
b f 1

the code before this has been successful to print like this

[C]
id pid count
c ~ 1
f c 1
a f 1
m a 1
p m 1
c ~ 1
f c 1
a f 1
m a 1
p m 1
b a 1
m b 1
b f 1
c ~ 1
b c 1
p b 1
...
etc

i need [C] table to convert to table so i add some logic so i could mine easier for later...
but the logic when comparing item in database with array that has null value is error in

if((fptree_id.equals(item)) && (fptree_pid.equals(temp_pid))){

i've still got to print it into xml and parse it into html while using a large dataset
it's still a long way to go for my thesis to complete but it stuck here :(

the problem has been solved :)
my problem is still at this logic

if((fptree_id.equals(item)) && (fptree_pid.equals(temp_pid))){

we can't compare null but we can if it isn't the first object
in example

String a = "Thanx God";
String b = null;

if(b.equals(a)){ // return NullPointerException
// some logic here
}
if(a.equals(b)){ // return false or do logic in else
// some logic here
}

thanx to NormR1 as well for keeping up with my task :)
i like this forum and will post something again in the future :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.