2016-11-22 36 views
-1

我正在嘗試爲Java創建Tile Map Editor,並將其卡在文件打開中。打開文件本身並不是什麼大問題,但是隻要我在文本文件中輸入空格就會造成運行時錯誤。每個圖塊包含一個圖像(黑色或白色方形atm)以及它是否將變爲固定(1)或不變爲(0)。該tilemaps將目前被保存在一個格式是這樣的:將重複模式的文本文件讀入列表中

1:1 1:1 1:1 1:1 1:1 1:1 
1:1 0:0 0:0 0:0 0:0 1:1 
1:1 0:0 0:0 0:0 0:0 1:1 
1:1 1:1 1:1 1:1 1:1 1:1 

例如,這可能是一個簡單的房間黑壁,中實,將阻止球員。這將是一張6x4的瓷磚地圖。我怎樣才能定義格式x:x(這裏是空格)?

List<Integer> list = new ArrayList<Integer>(); 
File file = new File("file.txt"); 
BufferedReader reader = null; 
try 
{ 
    reader = new BufferedReader(new FileReader(file)); 
    String text = null; 
    while ((text = reader.readLine()) != null) 
    { 
    list.add(Integer.parseInt(text)); 
    } 
} 
catch (FileNotFoundException e) 
{ 
    e.printStackTrace(); 
} 
catch (IOException e) 
{ 
    e.printStackTrace(); 
} 
+0

你究竟想要在你的List的每個元素中存儲什麼?單一1:1還是全部?當您嘗試在充滿冒號的線上parseInt時發生異常。你爲什麼要在包含冒號的這一行上parseInt?如果你想解析你的int,那麼你想讓列表中的每個元素看起來像什麼? –

+0

那麼上面的代碼示例就是爲了讓它在更基本的層面上工作。在真實情況下,我有一個列表 TileMap。 Tile是一個由int ID和int Type組成的類,它們的getter()和setters()。所以我想將第一個作爲ID存儲,第二個作爲類型,並且像列表中的每個Tile元素那樣存儲。 編輯:我在找什麼就像c#scanf參數%d或%r等... –

回答

0

雖然猜測,這是你問的,這樣的事情然後...

import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.FileReader; 
import java.io.IOException; 
import java.util.ArrayList; 
import java.util.List; 

public class ReadTextFile { 

    public static void main(String[] args) { 
     List<Tile> list = new ArrayList<Tile>(); 
     String path = "C:/whatever/your/path/is/"; 
     File file = new File(path + "file.txt"); 
     try (BufferedReader reader = new BufferedReader(new FileReader(file))) { 
      String text = null; 
      while ((text = reader.readLine()) != null) { 
       String[] pairs = text.split(" "); 
       for(String pair : pairs) { 
        String[] chars = pair.split(":"); 
        int id = Integer.parseInt(chars[0]); 
        int type = Integer.parseInt(chars[1]); 
        list.add(new Tile(id, type)); 
       } 
      } 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

    } 
} 

class Tile { 
    int id; 
    int type; 

    Tile(int id, int type) { 
     this.id = id; 
     this.type = type; 
    } 

    @Override 
    public String toString() { 
     return "Tile [id=" + id + ", type=" + type + "]"; 
    } 


} 
+0

完美!幾乎可以實現一對一。我會花時間修改流和他們的可能性:)唯一的改變將是程序識別txt文件中一行的結尾。我想我可以用一個十六進制的編輯器來做到這一點,它會告訴我什麼樣的字符用於換行,然後在我的程序中檢查它。謝謝! –

+0

還有一件事:我不應該關閉BufferedReader嗎? –

+0

BufferedReader會自動關閉。注意它是如何使用try-with-resources實現實例化的(從JDK7開始可用)。另請注意,BufferedReader實現了AutoCloseable;任何實現AutoCloseable的東西都可以在try-with-resources語句中使用。是的,用Streams做這件事會很好。 –

0

如果你只是用空格困擾,更改代碼以

while ((text = reader.readLine()) != null) 
{ 
    list.add(Integer.parseInt(text.trim())); 
} 

但我認爲這將無法正常工作,因爲您無法將整行轉換爲整數,因此我會建議:

while ((text = reader.readLine()) != null) 
{ 
    String[] values = text.split(":") 
    for(String val : values) 
    { 
     list.add(Integer.parseInt(val.trim())); 
    } 
} 
+0

謝謝你絕對把我帶入了正確的軌道。就像我剛剛在上面的評論中所說的那樣,我只需要知道bufferedreader如何知道txt文件中的行何時完成。 –