2011-11-17 140 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

你可以發佈整個堆棧跟蹤嗎? –

+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>>>>(); 

這將確保你是丘比特將正確的對象分配到每個地圖中。您將永遠無法獲得put a String值,因爲它不會被編譯,因此需要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

謝謝。雖然這並沒有直接解決它,但它確實讓我開始清理我的東西,然後我意識到我跳過了一個Map。感謝您的建議和幫助,謝謝大家。 – mseancole

1

異常告訴你一切。這個電話號碼subsubInfo.getValue();實際上是返回String,而不是Map,所以在創建地圖時出現邏輯錯誤。

編譯器會發出警告,如果你改變你的聲明來Map<String, Map>,而不是Map<String, Object>

相關問題