2014-02-28 36 views
0

對於一個賦值,我應該讀取一個包含545個整數集的文本文件,每個集包含8個要存儲的整數一個對象「wayPoint」,每個整數有8個變量。從文本文件中讀取整數並將它們放入對象以存儲到散列圖

我想輸出我的結果來驗證,我確實已經加載到對象的整數,但控制檯輸出什麼。

我的課很粗糙,對於缺乏散文感到遺憾。

這是航點類:

public class Test { 


    public static void main(String args[]) 
    { 
     System.out.println(); 
    } 


    public void readwayPoints() 
    { 
     Scanner readFile = null; 
     try 
     { 
      readFile = new Scanner(new FileInputStream(
      "insert text file here")); 
     } 
     catch(FileNotFoundException e) 
     { 
      System.out.println("File not found."); 
      System.exit(0); 
     } 

     int x = 0, y= 0, height = 0, cost = 0, gold = 0, mapX = 0, mapY = 0, neighbor = 0; 
     int count = 0; 

     List<Waypoint> list = new ArrayList<>(); 
     while (readFile.hasNextLine()) 
     { 

      x = readFile.nextInt(); 
      y = readFile.nextInt(); 
      height = readFile.nextInt(); 
      cost = readFile.nextInt(); 
      gold = readFile.nextInt(); 
      mapX = readFile.nextInt(); 
      mapY = readFile.nextInt(); 
      neighbor = readFile.nextInt(); 
      Waypoint wayP = new Waypoint(x, y, height, cost, gold, mapX, mapY, neighbor); 
      System.out.println(wayP); 
      list.add(wayP); 
     } 


    } 

這裏是航點類:

public class Waypoint 
    { 
     private int x, y, height, cost, gold, mapX, mapY, neighbor; 

     public Waypoint (int x, int y, int height, int cost, int gold, int mapX, int mapY, 
        int neighbor) 
     { 
      this.x = x; 
      this.y = y; 
      this. height = height; 
      this.cost = cost; 
      this.gold = gold; 
      this.mapX = mapX; 
      this.mapY = mapY; 
      this.neighbor = neighbor; 
     } 

     public String toString() 
     { 
      return "x:" + this.x + "y:" + this.y + "height:" + this.height + "cost" + this.cost 
          + "gold:" + this.gold + "mapX:" + this.mapX + "mapY:" + this.mapY + "neighbor" 
          + this.neighbor; 
     } 

    } 

}

文本文件的例子:

20 120 102 84 0 0 0 0 
20 260 85 75 0 0 0 0 
20 360 91 74 0 0 0 0 
40 220 111 73 0 0 0 0 
40 280 77 94 0 0 0 0 
40 300 68 67 0 0 0 0 
60 480 135 96 0 0 0 0 
80 400 149 92 0 0 0 0 
100 160 122 74 0 0 0 0 
100 240 104 70 0 0 0 0 
100 460 120 54 0 0 0 0 
120 460 131 98 0 0 0 0 
140 160 117 80 0 0 0 0 
140 280 78 76 0 0 0 0 
140 420 135 76 0 0 0 0 
160 320 163 58 0 0 0 0 
180 240 134 92 0 0 0 0 
+0

你在循環結果並在每個航點上調用toString()嗎?另外java類通常以大寫開頭 – markg

回答

2

你不要聖毫無疑問,你甚至不會創建一個wayPoint實例。

這應該做的伎倆:

List<wayPoint> list = new ArrayList<>(); 
    while (readFile.hasNextLine()) {   
     x = readFile.nextInt(); 
     y = readFile.nextInt(); 
     height = readFile.nextInt(); 
     cost = readFile.nextInt(); 
     gold = readFile.nextInt(); 
     mapX = readFile.nextInt(); 
     mapY = readFile.nextInt(); 
     neighbor = readFile.nextInt(); 
     wayP = new wayPoint(x, y, height, cost, gold, mapX, mapY, neighbor); 
     System.out.println(wayP); 
     list.add(wayP); 
    } 

順便說一句,你的類航點重命名爲航點,這幾乎是在Java中的「法」。

+0

我已經調整了我的主帖中的代碼以反映線程中的反饋,並且仍然無法在控制檯中看到任何輸出。我很欣賞迄今爲止的幫助!謝謝! – user3299778

+0

歡迎您,請記住將此郵件標記爲已接受:)。如果你沒有看到任何輸出,問題可能是其他地方(它可能甚至不運行這部分代碼,或者你的輸出設置在其他地方) – libik

相關問題