Android读取文本文件并分割

发布于 2024-11-26 19:46:24 字数 829 浏览 0 评论 0原文

我正在尝试读取包含整数的文本文件并将它们存储到二维数组中。 问题是分裂。 我可以读:

0 0 0
0 1 0
0 0 0

很好,任何数字 0-9,但我有超过 9 的数字(10、100、1000)。 我的方法:

int[][] mapArray = new int[11][8];
    AndroidFileIO file = new AndroidFileIO(context.getAssets());
    InputStream is = null;
    try {
        is = file.readAsset("maps/map.txt");
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        Log.d("android", "could not load file");
    }
    Scanner scanner = new Scanner(is);
    scanner.useDelimiter(",");
    for (int i = 0; i < 11; i++) {
        for (int j = 0; j < 8; j++) {
            mapArray[i][j] = scanner.nextInt();
            Log.d("android", "" + mapArray[i][j]);
        }
    }

所以我尝试使用分隔符,但它没有挂起并告诉我类型不匹配? 读取文件时分割整数有什么解决方案吗?

I'm trying to read a text file that contains integers and store them into a 2d array.
The problem is splitting.
I can read:

0 0 0
0 1 0
0 0 0

just fine and any numbers 0-9 but I have numbers exceeding 9 (10, 100, 1000).
My method:

int[][] mapArray = new int[11][8];
    AndroidFileIO file = new AndroidFileIO(context.getAssets());
    InputStream is = null;
    try {
        is = file.readAsset("maps/map.txt");
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        Log.d("android", "could not load file");
    }
    Scanner scanner = new Scanner(is);
    scanner.useDelimiter(",");
    for (int i = 0; i < 11; i++) {
        for (int j = 0; j < 8; j++) {
            mapArray[i][j] = scanner.nextInt();
            Log.d("android", "" + mapArray[i][j]);
        }
    }

So I tried using a delimiter and not it hangs and tells me i'm getting a type mismatch?
Any solutions on splitting integers when reading files?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

魂牵梦绕锁你心扉 2024-12-03 19:46:24

您可以使用正则表达式来检索数字:

final Pattern PATTERN = Pattern.compile("(\\d+)");
final Matcher matcher = PATTERN.matcher(content);
while (matcher.find()) {
  final String numberAsString = matcher.group(0);
  final Integer number = Integer.valueOf(numberAsString);
  //do something with number
}

You can use regular expression to retrieve the numbers:

final Pattern PATTERN = Pattern.compile("(\\d+)");
final Matcher matcher = PATTERN.matcher(content);
while (matcher.find()) {
  final String numberAsString = matcher.group(0);
  final Integer number = Integer.valueOf(numberAsString);
  //do something with number
}
海螺姑娘 2024-12-03 19:46:24

看起来您在这里使用“,”作为分隔符,但是在您在问题中提到的输入中,数字之间有空格。因此 scanner.nextInt() 尝试将空格字符转换为 int,因此类型不匹配。

[编辑]

然后你的新行字符将被 scanner.nextInt() 读取。添加对 '\n' 的检查
当你阅读字符时忽略它。

Looks like here you are using ',' as delimiter, But in the input that you have mentioned in the questions there are spaces between the numbers. So scanner.nextInt() is trying to convert space character into int, and hence the type mismatch.

[EDIT]

Then your new line character is being read by the scanner.nextInt(). Add a check for '\n'
and ignore it when you are reading the characters.

心奴独伤 2024-12-03 19:46:24

根据您的目的,您可以将文件中的每一行作为字符串读取,
然后将其分开。

String line = bufferedReader.readLine();   // line is something like "1 2 3"
String[] row = line.split("\\s+");         // got array ["1", "2", "3"]

然后更容易将每个数组元素映射到目标数组。
并且您需要 Integer.parseInt() 来获取整数值。

depending on your purpose, you could read each line in the file as a string,
and then split it.

String line = bufferedReader.readLine();   // line is something like "1 2 3"
String[] row = line.split("\\s+");         // got array ["1", "2", "3"]

Then it is easier to map each Array element to the destination array.
And and you need Integer.parseInt() to get the integer value.

微凉 2024-12-03 19:46:24

想出用它作为我的分隔符:

    scanner.useDelimiter("[\\s,\r\n]+");

Figured out to use this as my delimiter:

    scanner.useDelimiter("[\\s,\r\n]+");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文