【问题标题】:Reading integers from a text file and putting them into objects to store into a hashmap从文本文件中读取整数并将它们放入对象中以存储到哈希图中
【发布时间】:2014-02-28 01:11:02
【问题描述】:

对于一个作业,我应该读取一个包含 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);
        }


   }

这是 Waypoint 类:

   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

【问题讨论】:

  • 您是否循环遍历结果并在每个航点上调用 toString()?此外,java 类通常以大写开头

标签: java object integer


【解决方案1】:

你不存储它anyhwere,你甚至不创建一个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);
    }

顺便说一下,把你的类wayPoint重命名为WayPoint,这几乎是Java中的“定律”。

【讨论】:

  • 我已经调整了我的主要帖子中的代码以反映线程中的反馈,但仍然难以在控制台中看到任何输出。到目前为止,我感谢您的帮助!谢谢!
  • 不客气,记得将此消息标记为已接受:)。如果您没有看到任何输出,则问题可能出在其他地方(它甚至可能没有运行这部分代码,或者您的输出设置到其他地方)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-28
  • 1970-01-01
相关资源
最近更新 更多