2012-03-02 36 views
1
Try 
     Dim sr As New IO.StreamReader(Mapfile & ".txt") 
     'Dim intValue As String = "" 
     Dim strLine As String = "" 
     Dim X As Integer = 0 
     Dim Y As Integer = 0 

     Do Until sr.EndOfStream 
      strLine = sr.ReadLine 
      strLine = strLine.Replace(strLine.LastIndexOf(","), "") 
      For Each item As String In Split(strLine, ",", -1) 
       'MsgBox("X:" & X & " Y:" & Y & "= " & item) 
       If item = "" Then 
        item = 0 
       End If 
       If X <= MapWidth Then 
        Map(X, Y, 0) = Int(item) 
       End If 

       X = X + 1 
      Next 

      X = 0 
      Y = Y + 1 
     Loop 

     sr.Close() 
     sr.Dispose() 
    Catch ex As Exception 
     MsgBox("Map: " & Mapfile & " could not be loaded." & vbCrLf & vbCrLf & ex.Message, MsgBoxStyle.Critical, "ERROR") 
     IsOn = False 
    End Try 

試圖將此代碼從Visual Basic移植到Java。我試過使用Buffered Reader,但似乎沒有做到這一點。上面的代碼是針對Visual Basic的,下面的代碼是我的java端口,看起來似乎沒有相同的工作。 http://pastebin.com/freXYTi3需要將Visual Basic .NET代碼移植到Java

public void readFile(Context c) { 

    BufferedReader br = null; 

    try { 


     br = new BufferedReader(new InputStreamReader(c.getAssets().open("map1.txt"))); 
     String line = null; 
     String newLine = ""; 
     int x = 0; 
     int y = 0; 

     while ((line = br.readLine()) != null) { 
     int length = line.length(); 
     String lastChar = line.substring(length-1); 

     if (lastChar.contains(",")) { 
      newLine = line.substring(0,length-1) + ""; 
     } 
     //line = line.substring(0, line.lastIndexOf(",")) + ""; 
     for (String str : line.split(",", -1)) { 
     System.out.println(str); 
      if(str == ""){ 
       str = "0"; 
      } 
      if(x <= mapwidth){ 

       System.out.println(x + " " + y); 
       int N = Integer.parseInt(str); 
       Map[x][y] = N; 

      } 

      x = x + 1; 
     } 
     x = 0; 
     y = y + 1; 
     } 
    } 
    catch (FileNotFoundException ex) { 
     ex.printStackTrace(); 
    } 
    catch (IOException ex) { 
     ex.printStackTrace(); 
    } 
    finally { 
     try { 
     if (br != null) 
      br.close(); 
     } 
     catch (IOException ex) { 
     ex.printStackTrace(); 
     } 
    } 
    } 
+1

」似乎沒有任何東西給我想要我想要的東西「幾乎不是一個什麼問題的詳細描述。請閱讀http://tinyurl.com/so-hints – 2012-03-02 23:40:33

+0

爲什麼不發佈您的Java轉換的粗略版本,我們可以幫助您糾正它。 – 2012-03-02 23:41:06

+0

我可以發表我寫的代碼。堅持在 – Xwire 2012-03-02 23:41:27

回答

2

不知道你是失敗的確切線(錯誤代碼給行號,但我不知道該關聯到您的文件),我注意到的唯一的事情是,這條線:

if(x <= mapwidth){ 

可能是一次性錯誤。我認爲VB是基於1的,Java是基於零的,但這只是一個猜測,你可能想要<而不是< =你能否告訴我們NPE在哪條線上。

而且這是錯誤的:

if(str == ""){ 

需要 「str.equals(」 「)」 或 「str.length()== 0」

,但我看不到任何東西這可能會導致循環內的NPE

此外,您在循環中分配換行符,並且您從不在該範圍內使用它,所以如果它退出並且您有另一個「換行符」,並且您希望它被設置 - 屏住呼吸。 「

+0

地圖[x] [y] = N;是在java中崩潰的地方! – Xwire 2012-03-03 01:09:48

+0

非常感謝你!你修好了! – Xwire 2012-03-03 01:19:03

0

如果您發佈的錯誤你得到,那麼你可能會得到一個更好的答案,但不知道你正在嘗試分析它看起來像你正在你身邊正在分析數據假設的內容。

+0

即將發佈錯誤。 – Xwire 2012-03-03 00:00:51

+0

錯誤代碼已啓動。 – Xwire 2012-03-03 00:07:29