对于一个赋值,我应该读取一个包含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
你在循环结果并在每个航点上调用toString()吗?另外java类通常以大写开头 – markg