Hi Guys,
Please find below a copy of my code for the issue that I'm having.
So, before you have a look at the code and inevitably laugh at something silly that I've done (because I have a feeling that it would be), I should explain what my problem is.
Okay, So i have an array of HSSFCells, and an array of plans, and an individual plan instance. Once I've read through my HSSFCells array, and then go to add the individual plan to the array, it's only assigning it to the plan array at index 0.
I've checked the out put of the counter so that's not an issue, so why is it that it is only being set to array index 0... i wish i knew.
SO, here's my code, try only to laugh on the inside if you can ;)
Also, thank you in advance
package Model;
import java.io.*;
import java.sql.*;
import org.apache.poi.hssf.usermodel.*;
public class plan {
private static int planNum;
private static int planLength;
private static String planCode;
private static String planName;
private static String contractType;
private static double commission;
private static double simRebate;
private static double baseHandsetSubsidy;
private static plan onePlan;
private static plan allPlans[];
public void setPlanNum(int pn){ this.planNum = pn; }
public void setPlanLen(int pl){ this.planLength = pl; }
public void setPlanCode(String pc){ this.planCode = pc; }
public void setPlanName(String pn){ this.planName = pn; }
public void setContractType(String ct){ this.contractType = ct; }
public void setCommission(double com){ this.commission = com; }
public void setSimRebate(double srb){ this.simRebate = srb; }
public void setBaseHandset(double bhs){ this.baseHandsetSubsidy = bhs; }
public int getPlanNum(){ return this.planNum; }
public int getPlanLen(){ return this.planLength; }
public String getPlanCode(){ return this.planCode; }
public String getPlanName(){ return this.planName; }
public String getContractType(){ return this.contractType; }
public double getCommission(){ return this.commission; }
public double getSimRebate(){ return this.simRebate; }
public double getBaseHandset(){ return this.baseHandsetSubsidy; }
public static plan[] importPlansFromExcel(){
//public void importPlansFromExcel(String filePath){
boolean plansChecked = false;
String xlsPath = "C:\\Users\\******\\Documents\\********\\**********.xls";
//String xlsPath = filePath;
//plan plan = new plan();
try{
// - Create a new inputstream and construct an instance of HSSFWorkbook.
InputStream myxls = new FileInputStream(xlsPath); // - Initialize new FileInputStream with filepath 'xlspath'
HSSFWorkbook wb = new HSSFWorkbook(myxls); // - Initialize new HSSFWorkbook with inputstream
int numberOfSheets = wb.getNumberOfSheets();
for(int s = 0; s < 1; s++){ // - For each sheet (s) in HSSFWorkbook wb
//System.out.println(s + "<>");
HSSFSheet tempSheet = wb.getSheetAt(s); // - Create a temp sheet for sheet name
int numRows = tempSheet.getLastRowNum(); // - Get the number of rows in sheet
allPlans = new plan[numRows-1]; // - Initialize allPlans with length numRows-1
// - allPlans[] holds all plan items contained in 1 row.
int count = 0;
for(int r = 2; r < numRows+1; r++){ // - For each row (r) in HSSFSheet tempSheet
onePlan = new plan();
HSSFRow row = null;
HSSFCell oneCell = null;
HSSFCell allCells[] = null;
allCells = new HSSFCell[8];
for(int c = 0; c < 8; c++){ // - For each cell (c) in HSSF
row = tempSheet.getRow(r); // - Get row (r) from sheet 'tempSheet'
allCells[c] = row.getCell((short)c+1); // - Get cell value for cell c+1
}
allPlans[count] = setOnePlan( allCells, tempSheet, row );
System.out.print(count + " -- ");
System.out.print(allPlans[count].getPlanCode());
System.out.println();
count++;
}
}
}catch(Exception e){}
return allPlans;
}
private static plan setOnePlan(HSSFCell[] allCells, HSSFSheet tempSheet, HSSFRow row) {
// Sets the attributes associated with the plans class
// Checks the cell data type and converts to relevant data-type through casting to match member class
onePlan.setContractType( tempSheet.getSheetName() );
if ( allCells[0].getCellType() == HSSFCell.CELL_TYPE_STRING ) { onePlan.setPlanCode( row.getCell((short) 1).getStringCellValue() ); }
else if( allCells[0].getCellType() == HSSFCell.CELL_TYPE_NUMERIC ){ onePlan.setPlanCode( Integer.toString((int)(allCells[0].getNumericCellValue() ))); }
else{ onePlan.setPlanCode("Null"); }
if ( allCells[0].getCellType() == HSSFCell.CELL_TYPE_STRING ) { onePlan.setPlanCode( allCells[0].getStringCellValue() ); }
else if( allCells[0].getCellType() == HSSFCell.CELL_TYPE_NUMERIC ){ onePlan.setPlanCode( Integer.toString((int)(allCells[0].getNumericCellValue() ))); }
else{ onePlan.setPlanCode("Null"); }
if ( allCells[1].getCellType() == HSSFCell.CELL_TYPE_STRING ) { onePlan.setPlanName( allCells[1].getStringCellValue() ); }
else if( allCells[1].getCellType() == HSSFCell.CELL_TYPE_NUMERIC ){ onePlan.setPlanName( Integer.toString((int)(allCells[1].getNumericCellValue() ))); }
else{ onePlan.setPlanName("Null"); }
if ( allCells[2].getCellType() == HSSFCell.CELL_TYPE_STRING ) { onePlan.setPlanLen( Integer.parseInt( allCells[2].getStringCellValue() )); }
else if( allCells[2].getCellType() == HSSFCell.CELL_TYPE_NUMERIC ){ onePlan.setPlanLen( (int)( allCells[2].getNumericCellValue() )); }
else{ onePlan.setPlanLen(0); }
// Checks the cell data type and converts to relevant data-type through casting to match member class
if ( allCells[3].getCellType() == HSSFCell.CELL_TYPE_STRING ) { onePlan.setCommission( Double.parseDouble( allCells[3].getStringCellValue() )); }
else if( allCells[3].getCellType() == HSSFCell.CELL_TYPE_NUMERIC ){ onePlan.setCommission( ( allCells[3].getNumericCellValue() )); }
else{ onePlan.setCommission(0.0); }
// Checks the cell data type and converts to relevant data-type through casting to match member class
if ( allCells[4].getCellType() == HSSFCell.CELL_TYPE_STRING ) { onePlan.setBaseHandset( Double.parseDouble( allCells[4].getStringCellValue() )); }
else if( allCells[4].getCellType() == HSSFCell.CELL_TYPE_NUMERIC ){ onePlan.setBaseHandset( ( allCells[4].getNumericCellValue() )); }
else{ onePlan.setSimRebate(0.0); }
// Checks the cell data type and converts to relevant data-type through casting to match member class
if ( allCells[5].getCellType() == HSSFCell.CELL_TYPE_STRING ) { onePlan.setSimRebate( Double.parseDouble( allCells[5].getStringCellValue() )); }
else if( allCells[5].getCellType() == HSSFCell.CELL_TYPE_NUMERIC ){ onePlan.setSimRebate( ( allCells[5].getNumericCellValue() )); }
else{ onePlan.setSimRebate(0.0); }
return onePlan;
}
public static void main (String[] args) {
plan[] p = importPlansFromExcel();
//System.out.println(p[2].getPlanCode());
if(p[2] != null){
//boolean b = addPlans(p);
//System.out.println(p[0].getPlanCode());
} else{ System.out.println("failed"); }
}
}