電子書籍の厳選無料作品が豊富!

java勉強中のものです。
エラーが出て困っています。

プログラム内容としては、
エクセルファイルを読み込み、それを配列に入れるというプログラムです。
class ReadFileArray2{

public static class DataTable{
Object getCellValue2;

DataTable(Object getCellValue){
this.getCellValue2 = 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][20000];
for(int i = 0; i <= sheet.getLastRowNum(); i++){
Row row = sheet.getRow(i);
for(int j = 0; j <= row.getLastCellNum(); j++){
Cell cell = row.getCell(j);
// System.out.print(getCellValue(cell)+" ");
table[i][j] = getCellValue(cell);

}

}

for(int x = 0; x <= table.length; x++){
for(int t = 0; t <= table.length; t++ ){
System.out.print(table[x][t]);
}
}

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 "" ;
}
}
}

コメントアウトしているものは読み込めているかの確認用です。
コンパイルすると
ReadFileArray2.java:47: エラー: 不適合な型: ObjectをDataTableに変換できません:
table[i][j] = getCellValue(cell);
とエラーが出ます。

Switch文でObject型を用いているせいなのだと思われるのですが、
解決策が分かりません。

何かいい方法ありませんか?

A 回答 (4件)

>ここでの作業は、DataTable型に変換している感じでしょうか?



Fooはあなたの DataTable型にあたるものです。

>Q1の作業ですでに文字列を返していると思うのですが、
>ここでのreturnはどういうことなのでしょうか?

まず、修正

retrun new Foo(Foo.DataType.Numeric, "文字列");

retrun new Foo(Foo.DataType.String, "文字列");
ですね。

Foo[][] table = new Foo[86][20000];

で受け取るのですから、getCellValue が返すのは Foo 型が
よいだろうということです。Fooに文字列は代入できません。

==========
DataType getDataType() {
return this.dataType;
}
String getDataAsString() {
return (String)this.data;
}
==========

こいつらは、Foo型を使ってなにか処理する時に使います。
    • good
    • 0
この回答へのお礼

おかげさまでなんとなく分かってきました^^
ありがとうございます。
最後、配列に格納する際は
table[i][j] = new Foo();
でよろしいのでしょうか?

お礼日時:2016/12/03 03:00

>table[i][j] = new Foo();


これは出来ないです。コンパイルエラーになる筈。
>table[i][j] = getCellValue(cell);
を想定してます。
    • good
    • 0
この回答へのお礼

getCellValueですと、同じように不適合な型:object型をDataTable型に変換できませんとエラーが出ます。
個人的にはObject dataが原因なのではないかと思います。
public static class DataTable{
public enum DataType{
Double,String}
private DataType dataType;
private Object data;

public DataTable(DataType dataType, Object data){ ←
this.dataType = dataType;
this.data = data;
}
DataType getDataType(){
return this.dataType;
}
String getDataAsString(){
return (String)this.data;
}
double getDataAsDouble(){
return (double)this.data;
}

}

何度も質問して申し訳ないです。

お礼日時:2016/12/04 01:30

文字列型や数値型を一つの型で持ちたいなら、Cell型のような実装


が普通でしょう。

例えば

public class Foo {
public enum DataType {
Numeric,
String
}

private DataType dataType;

private Object data;

public Foo(DataType dataType, Object Data) {
this.dataType = dataType;
this.data = data;
}

DataType getDataType() {
return this.dataType;
}

String getDataAsString() {
return (String)this.data;
}
int getDataAsInt() {
return (int)this.data;
}
}

のような感じの型を作って

Foo[][] table = new Foo[86][20000];

とし、getCellValue は Foo を返すことにして、
getCellValueの中で文字列を返す場合は

retrun new Foo(Foo.DataType.Numeric, "文字列");

とすればよいでしょう。

Fooを使う側は getDataType() の戻りが Foo.DataType.String だったら
getDataAsString()メソッドで文字列を取り出せばよいでしょう。
    • good
    • 0
この回答へのお礼

回答ありがとうございます。

いくつか分からない点を質問しともよろしいでようか?

<Q1>
DataType getDataType() {
return this.dataType;
}

String getDataAsString() {
return (String)this.data;
}
int getDataAsInt() {
return (int)this.data;
}
}
ここでの作業は、DataTable型に変換している感じでしょうか?

<Q2>
>getCellValueの中で文字列を返す場合は

>retrun new Foo(Foo.DataType.Numeric, "文字列");

>とすればよいでしょう。

>Fooを使う側は getDataType() の戻りが Foo.DataType.String だったら
>getDataAsString()メソッドで文字列を取り出せばよいでしょう。

Q1の作業ですでに文字列を返していると思うのですが、ここでのreturnはどういうことなのでしょうか?

お礼日時:2016/12/01 18:44

getCellValue の戻りは全然 DataTable型でないのに


それをDataTble型の変数に格納できるわけがありません。

これなら Object型に格納した方がましですが、
getCellValueの中味も突っ込みどころが多すぎです。

もう少しゆっくり、基礎を学んだ方がよいのでは?
    • good
    • 0

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