Android String.split() 和/或 StringTokenizer() 在 While 循环中不起作用

发布于 2025-01-06 06:02:24 字数 979 浏览 0 评论 0原文

这很奇怪......

BufferedReader br = new BufferedReader(new InputStreamReader(openFileInput("file")));
String s;
while ((s = br.readLine()) != null) {
    String[] sa = s.split("a");
    String a = sa[0];
    String b = sa[1];
    String c = a + "the letter a was here" + b;
    // Do something with String c...
}

我在“String b = sa[1];”行处受到力接近。 java.lang.NullPointerException。 我的循环中的“String s”有多个“a”字符,这不是问题。 “字符串 a”始终是正确的。 但“String b”总是抛出异常。 我在循环之外声明变量(sa,a,b,c),没有帮助。

奇怪的是,StringTokenizer 也有同样的问题。 它只能读取

StringTokenizer st = new StringTokenizer(s, "a");
int x = st.countTokens();
String a = st.nextToken();
String b = st.nextToken();
String c = a + "the letter a was here" + b;
// Do something with String c...

同一个 While 循环内的第一个标记/拆分... ...。 同样的问题! “String a”是准确的,当系统到达“String b”行时,它会抛出相同的异常/错误/强制关闭。 我什至检查“int x”的值只是为了确保有多个标记,并且有,这又不是问题。

为什么在这个 While 循环中我只能访问/读取/分配/使用我已分割/标记化的字符串的第一个分割/标记???

This is very odd...

BufferedReader br = new BufferedReader(new InputStreamReader(openFileInput("file")));
String s;
while ((s = br.readLine()) != null) {
    String[] sa = s.split("a");
    String a = sa[0];
    String b = sa[1];
    String c = a + "the letter a was here" + b;
    // Do something with String c...
}

I get a force close at the line "String b = sa[1];". java.lang.NullPointerException.
My 'String s' in the loop has multiple 'a' characters, that is not the issue.
'String a' is ALWAYS correct.
But 'String b' always throws the exception.
I declare the variables (sa, a, b, c) outside of the loop, no help.

THE ODD THING, StringTokenizer has the SAME EXACT PROBLEM.
It can only read the first token/split...

StringTokenizer st = new StringTokenizer(s, "a");
int x = st.countTokens();
String a = st.nextToken();
String b = st.nextToken();
String c = a + "the letter a was here" + b;
// Do something with String c...

...inside the same While loop.
SAME EXACT PROBLEM! 'String a' is accurate, when the system gets to the 'String b' line, it throws the same exception/error/force close.
I even check the value of 'int x' just to make sure there are multiple tokens, and there are, again this is not the issue.

WHY in this While loop can I only access/read/assign/use the first split/token of my string that I have split/tokenized???

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

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

发布评论

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

评论(1

夜血缘 2025-01-13 06:02:24

无论出于何种原因,当“BufferedReader”出现问题时,我只需从测试手机上卸载该应用程序,然后从头开始运行,问题就解决了。我不确定“文件”可能已损坏。我已经遇到过这个问题两次,卸载应用程序并重新运行两次都修复了它。

For what ever reason, when having problems with "BufferedReader", I just uninstall the app from the test phone, and run from scratch, and the problem is fixed. Possible the 'file' gets corrupted, I'm not sure. I've run into this problem twice and uninstalling the app and re-running fixed it both times.

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