我主要使用 C++ 编程,并且一直在努力将我的游戏移植到 Java 和 Android 上。 我的一些代码遇到了一个小问题。 我的文本文件采用这种格式:
0:1 0:0 1:1 2:2 3:3
我像这样用 fscanf 函数读取它:
for(int Y = 0;Y < MAP_HEIGHT;Y++) {
for(int X = 0;X < MAP_WIDTH;X++) {
Tile tempTile;
fscanf(FileHandle, "%d:%d ", &tempTile.TileID, &tempTile.TypeID);
TileList.push_back(tempTile);
}
我如何读取 Java 中显示的格式化数据?显然没有 fscanf lol afaik ...
最佳答案
使用下面的代码在java中格式化字符串
import java.util.StringTokenizer;
public class Test {
public static void main(String args[])
{
String str="0:1 0:0 1:1 2:2 3:3";
format(str);
}
public static void format(String str)
{
StringTokenizer tokens=new StringTokenizer(str, " "); // Use Space as a Token
while(tokens.hasMoreTokens())
{
String token=tokens.nextToken();
String[] splitWithColon=token.split(":");
System.out.println(splitWithColon[0] +" "+splitWithColon[1]);
}
}
}
代码输出:
0 1
0 0
1 1
2 2
3 3
关于java - 在java中读取格式化的文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18007839/