Untitled

mail@pastecode.io avatar
unknown
plain_text
13 days ago
1.9 kB
2
Indexable
Never
import java.io.FileWriter;   // Import the FileWriter class
import java.io.IOException;  // Import the IOException class to handle errors
import java.util.Scanner;   // Import the Scanner class to read input

class Dataset {
    public static void main(String[] args) {
        int noOfInstances, noOfAttributes, i;
        String tempStr, classAttributes, data;
        System.out.print("Enter number of attributes (columns): ");
        Scanner sc = new Scanner(System.in);
        noOfAttributes = sc.nextInt();
        System.out.print("Enter number of instances (rows): ");
        noOfInstances = sc.nextInt();
        sc.nextLine();  // Consume the newline character after the integer input

        try {
            FileWriter myWriter = new FileWriter("exp8.arff");
            myWriter.write("@RELATION DWDM\n");

            for (i = 0; i < noOfAttributes; i++) {
                System.out.println("Enter attribute name " + (i + 1) + ": ");
                tempStr = sc.nextLine();
                myWriter.write("@ATTRIBUTE " + tempStr + " REAL\n");
            }

            System.out.println("Enter class labels with comma separated values:");
            classAttributes = sc.nextLine();
            myWriter.write("@ATTRIBUTE class {" + classAttributes + "}\n");
            myWriter.write("@DATA\n");

            System.out.println("Enter data (rows) line by line with comma separated values:");
            for (i = 1; i <= noOfInstances; i++) {
                System.out.println("Enter row " + i + ": ");
                data = sc.nextLine();
                myWriter.write(data + "\n");
            }

            myWriter.close();
            System.out.println("Successfully wrote to the file.");
        } catch (IOException e) {
            System.out.println("An error occurred.");
            e.printStackTrace();
        }
    }
}
Leave a Comment