Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

I was wondering if somebody can correct my java script?

+13 votes
asked Jun 24, 2024 by Sindel Thomas (220 points)
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.*;
import java.util.Collections;
import java.util.List;

public class ExcelOrderParser {
    public static void main(String[] args) {
        try {
            FileInputStream file = new FileInputStream(new File("order_report.xlsx"));
            Workbook workbook = WorkbookFactory.create(file);
            Sheet sheet = workbook.getSheetAt(0);

            // Iterate over rows and process order details
            for (Row row : sheet) {
                Cell nameCell = row.getCell(0);
                Cell materialCell = row.getCell(1);
                Cell destinationCell = row.getCell(2);

                // Extract and process order details
                String name = nameCell.getStringCellValue();
                String material = materialCell.getStringCellValue();
                String destination = destinationCell.getStringCellValue();

                // Split name into parts if possible
                String[] nameParts = name.split(" ");

                // Create a new row in the output document
                Row newRow = sheet.createRow(sheet.getLastRowNum() + 1);
                newRow.createCell(0).setCellValue(nameParts[0]);
                newRow.createCell(1).setCellValue(nameParts.length > 1 ? nameParts[1] : "");
                newRow.createCell(2).setCellValue(material);
                newRow.createCell(3).setCellValue(destination);
            }

            // Write the modified workbook to a new file
            FileOutputStream out = new FileOutputStream(new File("processed_orders.xlsx"));
            workbook.write(out);
            out.close();
            workbook.close();
            file.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

public class OrderSorter {
    public static void sortOrdersByMaterial(List<Order> orders) {
        Collections.sort(orders, (o1, o2) -> o1.getMaterial().compareTo(o2.getMaterial()));
    }

    public static void sortOrdersByDestination(List<Order> orders) {
        Collections.sort(orders, (o1, o2) -> o1.getDestination().compareTo(o2.getDestination()));
    }
}
public class Order {
    private String name;
    private String material;
    private String destination;

    public Order(String name, String material, String destination) {
        this.name = name;
        this.material = material;
        this.destination = destination;
    }

    // Getters and Setters
    public String getName() {
        return name;
    }

    public String getMaterial() {
        return material;
    }

    public String getDestination() {
        return destination;
    }
}

1 Answer

+1 vote
answered Jul 2, 2024 by Peter Minarik (101,360 points)

Your code will not work on Online GDB as it needs the org.apache.poi.ss.usermodel package. Read this how to get it fixed on your own machine: https://stackoverflow.com/questions/46294033/how-to-import-import-org-apache-poi-ss-usermodel-row-error

Also, just to clear, this is not JavaScript, this is a Java code, in case anyone got confused. :)

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and receive answers from other members of the community.
...