我有一个文本文件,我已读入数组列表。它看起来像这样:
[up,1,up,1,up,1,left,1,right,1,down,3]
如何以一种方式将ArrayList中的输入分开,方法是在方法向上(int n)之前分配数字,直到下一个方法离开(int n),依此类推?
答案 0 :(得分:1)
LoL只是把它放在一起检查它,看它是否有效。
BufferedReader br = new BufferedReader(new FileReader("myFile.txt"));
String line = br.readLine();
String [ ] words = line.split ( "," );
for(int i = 0; i < words.length; i++)
{
switch(words[i])
{
case " up":
case "up":
moveUP(Integer.parse(words[i + 1]);
i++;
break;
case " down":
case "down":
moveDown(Integer.parse(words[i + 1]);
i++;
break;
case " left":
case "left":
moveLeft(Integer.parse(words[i + 1]);
i++;
break;
case " right":
case "right":
moveRight(Integer.parse(words[i + 1]);
i++;
break;
}
}