0

I have currently created the code below to take a text file and convert it to ".ARFF". I have also added the Weka library to my IDE; however when trying to run the program, I get the following error:

https://i.stack.imgur.com/D1TCc.png

Below is my attempt at the code, I would appreciate any feedback or hints as to what could help me with my code.

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

/**
 *
 * @author Salim
 */

 public class ARFF {
    public static void main(String[] args) throws IOException {
        FileReader fileReader = null;
        BufferedReader br = null;

        FileWriter fileWriter = null;
        BufferedWriter bw = null;

        try {
            /*
             * Assigning the reference of file to variables for reading
             */
            fileReader = new FileReader("rawarffdata.txt");

            /*
             * Creating the reference for BufferedReader
             */
            br = new BufferedReader(fileReader);

            // Now reading the data for first 3 lines which are nothing but dataset name,
            // attribute name(column name) and their type
            String nameOfDataset = br.readLine();
            String[] columnArray = br.readLine().split(" ");
            /*
             * it will read first line and split it by delimiter space" " and returns the
             * array, same thing we do for datatype line
             */
            String[] typeArray = br.readLine().split(" ");

            /*
             * Now create an 2d array for holding the values and reading the data from file
             * one by one
             */
            String[][] data = new String[columnArray.length][];

            String line; // temporary variable which hold a line read from file
            int counter = 0; // it will count number of data line read
            while ((line = br.readLine()) != null) {
                // storing the values into data array, line.split(" ") will return an array of
                // separated values by space
                data[counter++] = line.split(" ");
            }

            // As of now all the data has been read successfully from the file
            // now from here it will write
            fileWriter = new FileWriter(nameOfDataset + ".txt");
            bw = new BufferedWriter(fileWriter);

            bw.write("@relation " + nameOfDataset + "\n");

            // Now write attributes according to their type
            for (int i = 0; i < columnArray.length; i++) {
                // check the datatype and write according to that
                if (typeArray[i].equals("n")) {
                    bw.write("@attribute " + columnArray[i] + " REAL\n");
                } else if (typeArray[i].equals("c")) {
                    // create set of attributes
                    Set<String> catSet = new HashSet<String>();
                    for (int j = 0; j < data.length; j++) {
                        // adding corresponding attribute to the set
                        catSet.add(data[j][i]);
                    }

                    // Now write the data into the file, it is required for formatting
                    // one can skip these if "[" is okay instead of "{"
                    bw.write("@attribute " + columnArray[i] + " {");
                    int count = 0;
                    for (String d : catSet) {
                        bw.write("'" + d + "'");
                        if (count < (catSet.size() - 1)) {
                            bw.write(", ");
                        }
                        count++;
                    }
                    bw.write("}\n");
                }
            }

            // Now writing the data
            bw.write("@data\n");
            for (String[] array : data) {
                for (String d : array) {
                    bw.write(d + ",");
                }
                bw.write("\n");
            }

            // closing the buffers
            br.close();
            bw.close();
        } catch (FileNotFoundException e) {
            System.out.println("rawarffdata.txt is not found!, please save the file at specified path");
        } finally {
            // closing the files
            fileReader.close();
            fileWriter.close();
        }
        // program executed successfully
        System.out.println("Program executed successfully!");
    }
}
    
    
fracpete
  • 1,931
  • 2
  • 11
  • 14
  • Insert `.replaceAll("[ ]+", " ")` before your `.split(...)` calls, to ensure that only a single blank is present. Otherwise you end up with a lot more cells. – fracpete Aug 18 '21 at 03:41

0 Answers0