对于赋值,我应该读取一个包含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
答案 0 :(得分: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);
}
顺便说一句,将你的class wayPoint重命名为WayPoint,它几乎是Java中的“法律”。