Java 一行数数
我在看我的游戏地图Java 一行数数,java,Java,我在看我的游戏地图 FileHandle file = Gdx.files.internal("data/" + level + ".txt"); StringTokenizer tokens = new StringTokenizer(file.readString()); while(tokens.hasMoreTokens()){ String type = tokens.nextToken(); if(typ
FileHandle file = Gdx.files.internal("data/" + level + ".txt");
StringTokenizer tokens = new StringTokenizer(file.readString());
while(tokens.hasMoreTokens()){
String type = tokens.nextToken();
if(type.equals("Car")){
carLiist.add(new Car(Integer.parseInt(tokens.nextToken()), Integer.parseInt(tokens.nextToken()), Float.parseFloat(tokens.nextToken()), Float.parseFloat(tokens.nextToken())));
}
这是我的文本文件
Block 0 0
Block 64 0
Car 1 5 9 5
Car 1 5 2
Block 1
Car 7
在java中是否可以计算每行中的数字?
编辑:
我需要如何使用stringtokenizer获取整条线?这里是我想做的,但我只得到每行的第一个单词
while(tokens.hasMoreTokens()){
String type = tokens.nextToken();
System.out.println(type);
if(type.equals("Block")){
//System.out.println(type);
list.add(new Brick(Integer.parseInt(tokens.nextToken()), Integer.parseInt(tokens.nextToken())));
有。Java提供了一种检查字符是否为数字的方法。检查字符,如果其数字如下所示,则增加计数:
int digitCount = 0;
if(isDigit(characterToCheck)) {
digitCount++;
}
方法:
booleanisdigit(char-ch)
。请看可能没有预先存在的方法,但您可以自己编写一个。一次只读一行。然后将行字符串拆分为字符串数组列表。循环数组列表,检查每个数组是否为数字。我会按照@CyberneticWerkGuruorc所说的做。您可以使用Apache来查看数字是否为数字,但我需要如何使用StringTokenizer从行中获取字符?这需要额外的代码来处理多个数字的数字,如示例中的64。好的,但是我需要如何使用StringTokenizer获取整行。我只知道第一个字,但那行没有数字