What's new

Anyone good with Java?

NO GOD

NO GOD

Only Me
Seasoned Veteran Grizzled Veteran
Messages
1,892
Reaction score
756
Points
365
Sin$
0
I'm looking for somebody good with Java, specifically, knowledge in the apache POI library would help the most.

Either way I'm still learning programming (especially Java) and have a few (which could turn into many) questions regarding the current code I've written.
 
WildeThing

WildeThing

Enthusiast
Messages
212
Reaction score
63
Points
130
Sin$
7
You should post your questions rather than inquire for those who probably have the answer for a very specific library.
I, and others here, will be able to answer any Java related questions you have although we may refer you to documents if it's Apache POI API specific since I personally don't have any experience of using that lib.
 
NO GOD

NO GOD

Only Me
Seasoned Veteran Grizzled Veteran
Messages
1,892
Reaction score
756
Points
365
Sin$
0
You should post your questions rather than inquire for those who probably have the answer for a very specific library.
I, and others here, will be able to answer any Java related questions you have although we may refer you to documents if it's Apache POI API specific since I personally don't have any experience of using that lib.

Yes I know that's how it usually works on a forum. I can read the documentation on my own, I was looking for somebody to discuss many things a little more in-depthly.

Specific questions I can usually answer myself through google or documentation, like the problem I had earlier trying to compare two strings.

Forget about the POI library for a moment, I would consider myself very mediocre at creating/knowing when to create classes, methods, objects and would like an opinion from somebody who has more experience at it, whether I'm doing it efficiently or not, without posting the entire program on here.
 
WildeThing

WildeThing

Enthusiast
Messages
212
Reaction score
63
Points
130
Sin$
7
Yes I know that's how it usually works on a forum. I can read the documentation on my own, I was looking for somebody to discuss many things a little more in-depthly.

Specific questions I can usually answer myself through google or documentation, like the problem I had earlier trying to compare two strings.

Forget about the POI library for a moment, I would consider myself very mediocre at creating/knowing when to create classes, methods, objects and would like an opinion from somebody who has more experience at it, whether I'm doing it efficiently or not, without posting the entire program on here.

Then post just a few classes and give some context behind them.
 
NO GOD

NO GOD

Only Me
Seasoned Veteran Grizzled Veteran
Messages
1,892
Reaction score
756
Points
365
Sin$
0
Then post just a few classes and give some context behind them.

My readsheet class. Reads a spreadsheet using the POI library. I guess my first question would be why can't I open a public file, not contained in a method?

Code:
FileInputStream file = new FileInputStream(new File("/Users/xxx/TimesheetProgram/src/Timesheet.xlsx"));
outside of a method just throw an error.

If I could make it public I wouldn't have to pass it into each method individually, correct?
Code:
public class Readsheet {

    static String jobNum = null;

    double totalManSTHours;
    double totalManETHours;

    double totalEquipmentHours;

    String materialType;
    double materialCount;

    public void readsheet() throws IOException {
        FileInputStream file = new FileInputStream(new File("/Users/xxx/TimesheetProgram/src/Timesheet.xlsx"));

        //Create Workbook instance holding reference to .xlsx file
        XSSFWorkbook workbook = new XSSFWorkbook(file);

        //Get first/desired sheet from the workbook
        XSSFSheet sheet = workbook.getSheetAt(0);

        //FileOutputStream out = new FileOutputStream(new File("test.xlsx"));
        //workbook.write(out);
        //out.close();

        setJobNum(sheet);

        getManSTHour(sheet);

        getManETHour(sheet);

        getEquipmentHours(sheet);

        getMaterialType(sheet);

        getMaterialCount(sheet);


        file.close();
    }

    public void writeSheet() throws IOException {

        //Create Workbook instance holding reference to .xlsx file
        XSSFWorkbook workbook = new XSSFWorkbook();

        //Create new sheet
        XSSFSheet sheet = workbook.createSheet("sheet");

        sheet.setColumnWidth(0, 5000);
        sheet.setColumnWidth(1, 3000);
        sheet.setColumnWidth(2, 3000);

        //Output new Excel file with values
        FileOutputStream out = new FileOutputStream(new File("test.xlsx"));
        workbook.write(out);
        out.close();
    }

    //
    //
    //
    //JOB NUMBER
    public String setJobNum(XSSFSheet sheet) {
        Row row = sheet.getRow(1);
        Cell cell = row.getCell(5);
        String jobNum = cell.getStringCellValue();
        this.jobNum = jobNum;
        System.out.println(jobNum);
        return jobNum;
    }

    public String getJobNum() {
        return jobNum;
    }

    //
    //
    //
    //
    //
    //Man standard hours method
    public double getManSTHour(XSSFSheet sheet) {
        for (int a=5; a<21; a++) {

            Row row = sheet.getRow(a);
            Cell cell = row.getCell(1);

            totalManSTHours += cell.getNumericCellValue();

            System.out.println(totalManSTHours);
        }
        return totalManSTHours;
    }

    //Man overtime hours method
    public double getManETHour(XSSFSheet sheet) {
        for (int a = 5; a < 21; a++) {

            Row row = sheet.getRow(a);
            Cell cell = row.getCell(2);

            totalManETHours += cell.getNumericCellValue();

            System.out.println(totalManETHours);
        }
        return totalManETHours;
    }

    //equipment hours method
    public double getEquipmentHours(XSSFSheet sheet) {
        for (int a = 23; a < 30; a++) {

            Row row = sheet.getRow(a);
            Cell cell = row.getCell(4);

            totalEquipmentHours += cell.getNumericCellValue();

            System.out.println(totalEquipmentHours);
        }
        return totalEquipmentHours;
    }

    //materials type method
    public String getMaterialType(XSSFSheet sheet) {
        for (int a = 23; a < 30; a++) {

            Row row = sheet.getRow(a);
            Cell cell = row.getCell(0);

            materialType = cell.getStringCellValue();

            System.out.println(materialType);
        }
        return materialType;
    }

    //materials count method
    public double getMaterialCount(XSSFSheet sheet) {
        for (int a = 23; a < 30; a++) {

            Row row = sheet.getRow(a);
            Cell cell = row.getCell(1);

            materialCount = cell.getNumericCellValue();

            System.out.println(materialCount);
        }
        return materialCount;
    }
}
 
WildeThing

WildeThing

Enthusiast
Messages
212
Reaction score
63
Points
130
Sin$
7
My readsheet class. Reads a spreadsheet using the POI library. I guess my first question would be why can't I open a public file, not contained in a method?

Code:
FileInputStream file = new FileInputStream(new File("/Users/xxx/TimesheetProgram/src/Timesheet.xlsx"));
outside of a method just throw an error.

If I could make it public I wouldn't have to pass it into each method individually, correct?
Code:
public class Readsheet {

    static String jobNum = null;

    double totalManSTHours;
    double totalManETHours;

    double totalEquipmentHours;

    String materialType;
    double materialCount;

    public void readsheet() throws IOException {
        FileInputStream file = new FileInputStream(new File("/Users/xxx/TimesheetProgram/src/Timesheet.xlsx"));

        //Create Workbook instance holding reference to .xlsx file
        XSSFWorkbook workbook = new XSSFWorkbook(file);

        //Get first/desired sheet from the workbook
        XSSFSheet sheet = workbook.getSheetAt(0);

        //FileOutputStream out = new FileOutputStream(new File("test.xlsx"));
        //workbook.write(out);
        //out.close();

        setJobNum(sheet);

        getManSTHour(sheet);

        getManETHour(sheet);

        getEquipmentHours(sheet);

        getMaterialType(sheet);

        getMaterialCount(sheet);


        file.close();
    }

    public void writeSheet() throws IOException {

        //Create Workbook instance holding reference to .xlsx file
        XSSFWorkbook workbook = new XSSFWorkbook();

        //Create new sheet
        XSSFSheet sheet = workbook.createSheet("sheet");

        sheet.setColumnWidth(0, 5000);
        sheet.setColumnWidth(1, 3000);
        sheet.setColumnWidth(2, 3000);

        //Output new Excel file with values
        FileOutputStream out = new FileOutputStream(new File("test.xlsx"));
        workbook.write(out);
        out.close();
    }

    //
    //
    //
    //JOB NUMBER
    public String setJobNum(XSSFSheet sheet) {
        Row row = sheet.getRow(1);
        Cell cell = row.getCell(5);
        String jobNum = cell.getStringCellValue();
        this.jobNum = jobNum;
        System.out.println(jobNum);
        return jobNum;
    }

    public String getJobNum() {
        return jobNum;
    }

    //
    //
    //
    //
    //
    //Man standard hours method
    public double getManSTHour(XSSFSheet sheet) {
        for (int a=5; a<21; a++) {

            Row row = sheet.getRow(a);
            Cell cell = row.getCell(1);

            totalManSTHours += cell.getNumericCellValue();

            System.out.println(totalManSTHours);
        }
        return totalManSTHours;
    }

    //Man overtime hours method
    public double getManETHour(XSSFSheet sheet) {
        for (int a = 5; a < 21; a++) {

            Row row = sheet.getRow(a);
            Cell cell = row.getCell(2);

            totalManETHours += cell.getNumericCellValue();

            System.out.println(totalManETHours);
        }
        return totalManETHours;
    }

    //equipment hours method
    public double getEquipmentHours(XSSFSheet sheet) {
        for (int a = 23; a < 30; a++) {

            Row row = sheet.getRow(a);
            Cell cell = row.getCell(4);

            totalEquipmentHours += cell.getNumericCellValue();

            System.out.println(totalEquipmentHours);
        }
        return totalEquipmentHours;
    }

    //materials type method
    public String getMaterialType(XSSFSheet sheet) {
        for (int a = 23; a < 30; a++) {

            Row row = sheet.getRow(a);
            Cell cell = row.getCell(0);

            materialType = cell.getStringCellValue();

            System.out.println(materialType);
        }
        return materialType;
    }

    //materials count method
    public double getMaterialCount(XSSFSheet sheet) {
        for (int a = 23; a < 30; a++) {

            Row row = sheet.getRow(a);
            Cell cell = row.getCell(1);

            materialCount = cell.getNumericCellValue();

            System.out.println(materialCount);
        }
        return materialCount;
    }
}

You shouldn't really be passing the sheet into every method. You should initialize a private member called "sheet" and refer to "this.sheet" every time you want to receive information from it. You should open up the file, read it into a sheet object, close it in the constructor method. All your public methods shouldn't expose an API for injecting any other sheet.

There's a lot that you could potentially do in terms of SOLID OOP design (single responsibility principle, lisktov substitution; for example - you could create a sort of blueprint class - extends for automatic reading into a sheet object (calling super()) and then implement your own SheetReadable class for each case - assuming you want to have an OOP design for reading all kinds of sheet information).

Also, there's no really any time you'd want to throw an exception anywhere in a method, so surround your throwable clauses with a try catch block. The only time I've ever really thrown anywhere over a method is when you like have an inheritance functionality sort of thing where you can extend one functionality but find different issues per each extended class. Or, if you're making a library and want those using it to handle exceptions.

Technically speaking you're hardcoding a lot of values so you may as well making all the hardcoded offset information part of the class in private static final members. Or, you could create a whole enum class and create a constructor for the enum that takes the int offset. Although, then you'll end up with messy code. Shame Java can't do like: enum class SheetOffsets : int { BASE = 0, JOB_ROW = 4 }; etc. etc.

I don't quite get your getMaterialCount method since you're changing the end return value like 7 times and ultimately doing nothing the previous 6 times. Try not to hardcode values that could be different for each sheet format or whatever, I think POI could throw some random exceptions if you don't look into checking if things exist within the sheet before trying to read them etc. etc.

EDIT: Although public by default, please add public identifiers to your public objects. It's Java standard.
 
NO GOD

NO GOD

Only Me
Seasoned Veteran Grizzled Veteran
Messages
1,892
Reaction score
756
Points
365
Sin$
0
You shouldn't really be passing the sheet into every method. You should initialize a private member called "sheet" and refer to "this.sheet" every time you want to receive information from it. You should open up the file, read it into a sheet object, close it in the constructor method. All your public methods shouldn't expose an API for injecting any other sheet.

The issue is I can't initialize the sheet until after I run fileInputStream to open the workbook. And that can't be ran unless it's in a method that throws IOException (to the best of my knowledge).

You're right I decided to hardcode a lot of values, mainly because the input sheet format will never change, and the output sheet layout will never change, the only thing that will change are the variables (all names+numbers) on the output sheet.

My writeSheet method isn't actually done I should have left it out for now, I only use println in each method for testing to make sure I'm getting all the values correctly. That's part of the reason why getMaterialCount changes the value each time. But I also consider that the best way to get the value in each row and print it to a new sheet instead of creating some like matCount1, matCount2, etc.. it will just pass the current value to the new sheet each time. The goal is to get String materialType, find cost of that specific material, multiply by materialCount, output to sheet.

So knowing that the sheet formats will never change, is it worth getting any deeper into a sheet class?
 
S7 Pro

S7 Pro

Seasoned Member
Modder Programmer
Messages
2,511
Reaction score
1,599
Points
560
Sin$
7
The issue is I can't initialize the sheet until after I run fileInputStream to open the workbook. And that can't be ran unless it's in a method that throws IOException (to the best of my knowledge).

You're right I decided to hardcode a lot of values, mainly because the input sheet format will never change, and the output sheet layout will never change, the only thing that will change are the variables (all names+numbers) on the output sheet.

My writeSheet method isn't actually done I should have left it out for now, I only use println in each method for testing to make sure I'm getting all the values correctly. That's part of the reason why getMaterialCount changes the value each time. But I also consider that the best way to get the value in each row and print it to a new sheet instead of creating some like matCount1, matCount2, etc.. it will just pass the current value to the new sheet each time. The goal is to get String materialType, find cost of that specific material, multiply by materialCount, output to sheet.

So knowing that the sheet formats will never change, is it worth getting any deeper into a sheet class?

This is how you can handle exceptions without adding "throws IOException" to the method header:
Code:
try {
    FileInputStream file = new FileInputStream(new File("XXXX"));
    ReadSheet sheet = new ReadSheet(file);
} catch (IOException ex) {    // if we receive an IOException, this will be executed
    System.out.println("An IO exception has occured!");    // or handle how ever you wish
}

Obviously you'd need to adapt your ReadSheet class to accept a File as a parameter, and from there you can perform the rest rather easily.
 
WildeThing

WildeThing

Enthusiast
Messages
212
Reaction score
63
Points
130
Sin$
7
This is how you can handle exceptions without adding "throws IOException" to the method header:
Code:
try {
    FileInputStream file = new FileInputStream(new File("XXXX"));
    ReadSheet sheet = new ReadSheet(file);
} catch (IOException ex) {    // if we receive an IOException, this will be executed
    System.out.println("An IO exception has occured!");    // or handle how ever you wish
}

Obviously you'd need to adapt your ReadSheet class to accept a File as a parameter, and from there you can perform the rest rather easily.
To add onto this, since I'm unaware of whether POI loads the sheet into memory or only indexes the structure then reads info as it goes, a try catch finally block may benefit OP to ensure no resource leakage.
 
Top Bottom
Login
Register