android studio和netbeans中输入扫描程序和解析的不同行为 [英] different behaviour of input scanner and parse in android studio and netbeans

查看:114
本文介绍了android studio和netbeans中输入扫描程序和解析的不同行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用NetBeans(测试基本代码)和Android Studio(实际应用程序)读取相同的管道分隔文件并获得不同的结果。

I am reading the same pipe-delimited file using NetBeans (to test the basic code) and Android Studio (actual application) and getting different results.

以下是有问题的代码:

String URL = "http://.../CalendarUTF8(Dec2016).txt";

try {
  java.net.URL url = new java.net.URL(URL); 
  Scanner input = new Scanner(url.openStream());
  int i;
  for (i = 0; input.hasNext(); i++) {
    String temp = input.nextLine();
    eventCalendar[i] = temp.split("\\|",9);
  }
}
catch (java.io.IOException ex) {
  System.out.println("IO Errors");
}

代码在NetBeans中完美运行。在Android Studio中读取的同一文件随机中断输入行,然后将其读取为2行。我检查了文件,并且在它被拆分的地方没有隐藏的字符。如果我自己编写解析器,也可以正常工作。

Code works perfectly in NetBeans. Same file being read in Android Studio randomly breaks an input line in the middle, and then reads it as 2 lines. I checked the file and there is no hidden characters at the place where it gets split. If I write the parser myself, works fine as well.

推荐答案

看起来问题是在for循环的标题中使用hasNext()并在body体中使用nextLine() for循环。当我用hasNextLine()替换hasNext()时,我的记录计数匹配。不明白为什么,因为hasNext()应该看一些东西,而不是改变,但它有效。

Looks like the problem is in using hasNext() in the header of for loop and using nextLine() in the body of for loop. When I replaced hasNext() with hasNextLine(), my record counts matched. Don't understand why, since hasNext() is supposed to look at something, not change, but it worked.

这篇关于android studio和netbeans中输入扫描程序和解析的不同行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆