
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型を用いているせいなのだと思われるのですが、
解決策が分かりません。
何かいい方法ありませんか?
No.3ベストアンサー
- 回答日時:
>ここでの作業は、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型を使ってなにか処理する時に使います。
おかげさまでなんとなく分かってきました^^
ありがとうございます。
最後、配列に格納する際は
table[i][j] = new Foo();
でよろしいのでしょうか?
No.4
- 回答日時:
>table[i][j] = new Foo();
これは出来ないです。コンパイルエラーになる筈。
>table[i][j] = getCellValue(cell);
を想定してます。
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;
}
}
何度も質問して申し訳ないです。
No.2
- 回答日時:
文字列型や数値型を一つの型で持ちたいなら、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()メソッドで文字列を取り出せばよいでしょう。
回答ありがとうございます。
いくつか分からない点を質問しともよろしいでようか?
<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はどういうことなのでしょうか?
No.1
- 回答日時:
getCellValue の戻りは全然 DataTable型でないのに
それをDataTble型の変数に格納できるわけがありません。
これなら Object型に格納した方がましですが、
getCellValueの中味も突っ込みどころが多すぎです。
もう少しゆっくり、基礎を学んだ方がよいのでは?
お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!
関連するカテゴリからQ&Aを探す
おすすめ情報
デイリーランキングこのカテゴリの人気デイリーQ&Aランキング
-
ループ処理の際、最後だけ","を...
-
Ctrl+Zが入力されると終了する...
-
javaのエラーが回収できない
-
JavaのWhile文で
-
Javaの問題について
-
2次元配列の並び替え
-
フィールド名やメソッド名に日...
-
エラー(互換性の無い型)
-
Randomメソッドの確率設定
-
Java 配列 勝敗、引き分け判定...
-
LinkedHashMapについて
-
点数をだす時に、maxとminがお...
-
java DAO 日付指定フォーマット...
-
オブジェクトの中のプロパティ...
-
動的配列が存在(要素が有る)か...
-
System.err. printlnとSystem.o...
-
<forEach> 内で供給された "ite...
-
ORA-01858: 数値を指定する箇所...
-
0dの意味を教えてください
-
C言語のポインターに関する警告
マンスリーランキングこのカテゴリの人気マンスリーQ&Aランキング
-
ループ処理の際、最後だけ","を...
-
java キーボード入力された値の...
-
論理演算子”||”またはの入力方法
-
数値⇒漢数字変換 java
-
テキストボックスに入力された...
-
続・ZZZ,ZZZ,ZZ9形式の金額形式...
-
Ctrl+Zが入力されると終了する...
-
JAVAのfor文で困っています。
-
countに実行した回数をいれたい...
-
Java 入力した整数値の合計を、...
-
配列を逆順させて表示させる方...
-
Randomメソッドの確率設定
-
【JAVA <identifier>がありま...
-
コマンドライン引数の例外処理...
-
繰り返しによる星印の出力
-
Java 配列<選挙>
-
7つ数字を表示したら改行すると...
-
要素数が10の配列で、乱数0~9...
-
javaのプログラミングについて...
-
javaです。 途中まで出来ている...
おすすめ情報