2011-11-17 28 views
3

私はJavaを使い慣れていますが、プログラミングには新しくないので、私の最初のプロジェクトでは、職場の誰かのために.txt-.csvパーサーを作成することにしました。 .txtファイルの各行を読み込み、セクション、サブセクション、サブセクション、およびサブセクションのコンテンツ用に別々のマップに分けます。各マップは、それよりも上のマップに割り当てられます(詳細は後述)。私はそれをすべてうまく印刷しますが、私はそれを読み込もうとすると次のエラーが発生します:"java.lang.String cannot be cast to java.util.Map"。このエラーはコードの実行後にのみ表示され、コンパイル中ではなく、NetBeans IDEでは表示されません。java.lang.Stringはjava.util.Mapにキャストできません

マイマップは、各オブジェクトは、その下の地図であることを次の形式である:(なぜJavaは、この簡単-_-連想配列は私が望むすべてあることはできません)

(Map)array=<string,Object> 
(Map)subarray=<String,Object> 
(Map)subsubarray=<String,Object> 
(Map)subsubcontents=<String,String> 

ができない場合がありこれを読む最も効率的な方法は、これを後で再帰関数に変換する計画ですが、私のコードは私のプロジェクトからコピー貼り付けされています。私はエラーが見つかった場所にコメントを付けました。

public static Map<String,Object> array=new HashMap<String,Object>(); 

/* Code for populating the following Maps and pushing them into array 
<String,Object>subarray 
<String,Object>subsubarray 
<String,String>subsubcontents 
*/ 

Set section=array.entrySet(); 
Iterator sectionI=section.iterator(); 
while(sectionI.hasNext()) { 
    Map.Entry sectionInfo=(Map.Entry)sectionI.next(); 
    Map<String,Object> subMap=(Map)sectionInfo.getValue(); 
    Set subSet=subMap.entrySet(); 
    Iterator subI=subSet.iterator(); 
    while(subI.hasNext()) { 
      Map.Entry subInfo=(Map.Entry)subI.next(); 
      Map<String,Object> subsubMap=(Map)subInfo.getValue(); 
      Set subsubSet=subsubMap.entrySet(); 
      Iterator subsubI=subsubSet.iterator(); 
      while(subsubI.hasNext()) { 
        System.out.println("test"); 
        Map.Entry subsubInfo=(Map.Entry)subsubI.next(); 
        Map<String,Object> subcontentsMap=(Map)subsubInfo.getValue(); 
/* 
The above line seems to be causing the issues. 
If you comment out the rest of this loop (below this comment) 
the error will still appear. If you comment out the rest of this loop 
(including the line above this comment) it disappears. 
Power of deduction my dear Watson. 
*/ 
        Set subcontentsSet=subcontentsMap.entrySet(); 
        Iterator keys=subcontentsSet.iterator(); 
        while(keys.hasNext()) { 
         Map.Entry keyMap=(Map.Entry)keys.next(); 
        } 
        Iterator values=subcontentsSet.iterator(); 
        while(values.hasNext()) { 
         Map.Entry valueMap=(Map.Entry)values.next(); 
        } 
      } 
    } 
} 

ご協力いただければ幸いです。私は数日間このことに苦しんできました。

+0

stacktrace全体を投稿できますか? –

+0

getStackTrace()が返す[Ljava.langStackTraceElement; @ c0a150 – mseancole

答えて

7

にあなたの宣言を変更した場合

コンパイラは、私はあなたがで開始するあなたのジェネリックをクリーンアップする必要があると思うこれについて警告を表示します。次に

Set<Map.Entry<String, Object>> section = array.entrySet(); 
Iterator<Map.Entry<String, Object>> sectionI = section.iterator(); 
while (sectionI.hasNext()) { 
    Map.Entry<String, Object> sectionInfo = sectionI.next(); 
    Map<String, Object> subMap = (Map<String, Object>) sectionInfo.getValue(); // is this actually a Map<String, Object>? 
    Set<Map.Entry<String, Object>> subSet = subMap.entrySet(); 
    Iterator<Map.Entry<String, Object>> subI = subSet.iterator(); 
    while (subI.hasNext()) { 
     Map.Entry<String, Object> subInfo = subI.next(); 
     Map<String, Object> subsubMap = (Map<String, Object>) subInfo.getValue(); // is this actually a Map<String, Object>? 
     Set<Map.Entry<String, Object>> subsubSet = subsubMap.entrySet(); 
     Iterator<Map.Entry<String, Object>> subsubI = subsubSet.iterator(); 
     while (subsubI.hasNext()) { 
      System.out.println("test"); 
      Map.Entry<String, Object> subsubInfo = subsubI.next(); 
      Map<String, Object> subcontentsMap = (Map<String, Object>) subsubInfo.getValue(); // somehow a String got in here? 
/* 
The above line seems to be causing the issues. 
If you comment out the rest of this loop (below this comment) 
the error will still appear. If you comment out the rest of this loop 
(including the line above this comment) it disappears. 
Power of deduction my dear Watson. 
*/ 
      Set<Map.Entry<String, Object>> subcontentsSet = subcontentsMap.entrySet(); 
      Iterator<Map.Entry<String, Object>> keys = subcontentsSet.iterator(); 
      while (keys.hasNext()) { 
       Map.Entry<String, Object> keyMap = keys.next(); 
      } 
      Iterator<Map.Entry<String, Object>> values = subcontentsSet.iterator(); 
      while (values.hasNext()) { 
       Map.Entry<String, Object> valueMap = values.next(); 
      } 
     } 
    } 
} 

、あなたがする必要がありますarrayのあなたの宣言で、より明示的な:

public static Map<String, Map<String, Map<String, Map<String, String>>>> array = new HashMap<String, Map<String, Map<String, Map<String, String>>>>(); 

これは、あなたがちびであることを確実にするでしょう正しいオブジェクトを各マップに追加します。 putStringの値は、コンパイルできないため、Map<>が必要です。これは、あなたが(キャストを必要とせずに)次のコードを書くことができます:

final Set<Map.Entry<String, Map<String, Map<String, Map<String, String>>>>> section = array.entrySet(); 
final Iterator<Map.Entry<String, Map<String, Map<String, Map<String, String>>>>> sectionI = section.iterator(); 
while (sectionI.hasNext()) { 
    final Entry<String, Map<String, Map<String, Map<String, String>>>> sectionInfo = sectionI.next(); 
    final Map<String, Map<String, Map<String, String>>> subMap = sectionInfo.getValue(); 
    final Set<Map.Entry<String, Map<String, Map<String, String>>>> subSet = subMap.entrySet(); 
    final Iterator<Map.Entry<String, Map<String, Map<String, String>>>> subI = subSet.iterator(); 
    while (subI.hasNext()) { 
     final Map.Entry<String, Map<String, Map<String, String>>> subInfo = subI.next(); 
     final Map<String, Map<String, String>> subsubMap = subInfo.getValue(); 
     final Set<Map.Entry<String, Map<String, String>>> subsubSet = subsubMap.entrySet(); 
     final Iterator<Map.Entry<String, Map<String, String>>> subsubI = subsubSet.iterator(); 
     while (subsubI.hasNext()) { 
      System.out.println("test"); 
      final Map.Entry<String, Map<String, String>> subsubInfo = subsubI.next(); 
      final Map<String, String> subcontentsMap = subsubInfo.getValue(); 
      final Set<Map.Entry<String, String>> subcontentsSet = subcontentsMap.entrySet(); 
      final Iterator<Map.Entry<String, String>> entries = subcontentsSet.iterator(); 
      while (entries.hasNext()) { 
       final Map.Entry<String, String> entry = entries.next(); 
      } 
     } 
    } 
} 

はすべてのことを言われて、それらのネストされたジェネリック医薬品のすべてが醜い見えます。私はあなたのデータを表すためにいくつかのオブジェクトを作成することをお勧めします。

+1

ありがとうございます。これで直接解決することはできませんでしたが、私は自分が持っていたものを掃除し始めました。そして、私はマップをスキップしたことに気付きました。提案していただきありがとうございます、皆さんありがとうございます。 – mseancole

1

例外はすべてあなたに伝えます。この電話subsubInfo.getValue();は実際にStringを返します。Mapではなく、マップを作成する際に論理エラーが発生します。あなたがMap<String, Map>代わりのMap<String, Object>

関連する問題