dポイントプレゼントキャンペーン実施中!

現在、poiを用いてexcelファイルを読み込み二次元配列に格納するプログラムを作っているのですが、
仕組みを理解していないせいかエラーが出て困っています。

class ReadFileArray2{

public static class DataTable{
String getCellValue;
double getCellValue;

DataTable(double getCellValue, String getCellValue){
this.getCellValue = getCellValue;
}
}


static final String ID = "/Users/home/java/20160501.xlsx";

public static void main(final String[] args) throws Exception{

InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);

System.out.println("読み込むデータの日付を入力してください*(例5.1)");
String Date = br.readLine();
System.out.println(Date + "のデータの読み込みを開始します");

try {
FileInputStream fis = new FileInputStream(ID);
Workbook wb = WorkbookFactory.create(fis);
Sheet sheet = wb.getSheet(Date);
DataTable[][] table = new DataTable[86][146];
for(int i = 1; i <= sheet.getLastRowNum(); i++){
Row row = sheet.getRow(i);
for(int j = 1; j <= row.getLastCellNum(); j++){
Cell cell = row.getCell(j);
System.out.print(getCellValue(cell)+" ");
table[i][j] = new DataTable();

}
}
System.out.println("");

}catch(Exception e){
e.printStackTrace();
}
}

@SuppressWarnings(value={"deprecation"})
private static Object getCellValue(Cell cell){
if(cell == null){
return "";
}
switch (cell.getCellTypeEnum()) {
case STRING:
return cell.getRichStringCellValue().getString();

case NUMERIC:
if(org.apache.poi.ss.usermodel.DateUtil.isCellDateFormatted(cell)) {
java.util.Date date = cell.getDateCellValue();
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("h:mm");
LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
String formatted = dateTimeFormatter.format(localDateTime);
return formatted;
}else{
return cell.getNumericCellValue();
}
case FORMULA:
return cell.getCellFormula();

default:

return "" ;
}
}
}

DataTableクラスを作り、数値と文字列をまとめ、配列を作り、switch文で取得したセルのタイプ振り分けているのですが、実行すると

ReadFileArray2.java:19: エラー: 変数 getCellValueはすでにクラス DataTableで定義されています
double getCellValue;
^
ReadFileArray2.java:21: エラー: 変数 getCellValueはすでにコンストラクタ DataTableで定義されています
DataTable(double getCellValue, String getCellValue){
^
ReadFileArray2.java:48: エラー: クラス DataTableのコンストラクタ DataTableは指定された型に適用できません。
table[i][j] = new DataTable();
^
期待値: double,String
検出値: 引数がありません
理由: 実引数リストと仮引数リストの長さが異なります

とエラーが出ます。
配列の文がそもそもおかしいのでしょうか?

数日悩んでます。
以前はDataTableクラスを作らずにString型、Double型に分けて配列を作っていたのですが
Object型をString,Doubleに変換できませんとエラーがでました。
そこで、クラスでまとめて代入したのですがこれもダメでした。

どこをどのように変えればうまくいくでしょうか?
教えてください。

A 回答 (2件)

おそらく



public static class DataTable {
_ private Object value;
_ public DataTable(Object value) {
_ _ this.value = value;
_ }
_ public String toString() {
_ _ if (this.value == null) return "";
_ _ if (this.value instanceof String) return this.value;
_ _ if (this.value instanceof Double) return this.value.toString();
_ _ return "?";
_ }
}
...
table[i][j] = new DataTable(getCellValue(cell));
...
なんかの処理(table[i][j].toString());
    • good
    • 0
この回答へのお礼

丁寧な回答ありがとうございます。
一度試してみます!!

お礼日時:2016/12/04 17:56

同じクラスの中で複数のフィールドに同じ名前を使っちゃったら, どれがどれだかわかんなくなっちゃうじゃん.



「配列」がどうとか「仕組み」がどうとかいう前に, そも「Java のクラス」を理解していないように見えちゃうなぁ.
    • good
    • 0
この回答へのお礼

ご指摘ありがとうございます。

同じ名前を使うのはおかしいことは理解しておりますが、
switch文内で取得したCellのタイプをgetCellValueに返すようにしていますので、
仮に
String getCellStringValue;
double getCellNumericValue;

にしますと、正しい値が格納されなくなりませんか?

お礼日時:2016/12/01 02:07

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!