Java:读取文件并将一行拆分为单独的字符串

huangapple go评论106阅读模式
英文:

Java: Read a File & Split a line into separate string

问题

I understand your request. Here's the translation of the code snippet you provided:

  1. 我想要读取一个名为 file.txt 的文件其中包含类似这样的单词对...

yaniv:bobo

  1. 读取了 file.txt 后,我想要分割这段文本,并将每个单词放入变量中,然后尝试进行比较,就像这样:
  2. ```java
  3. Scanner scan = new Scanner(new FileReader(file));
  4. while(scan.hasNextLine()) {
  5. String descritpion = scan.nextLine();
  6. System.out.println("line" + descritpion);
  7. String []temp = descritpion.split(":");
  8. String name = temp[0];
  9. String surname = temp[1];
  10. System.out.println("name : " + name);
  11. System.out.println("surname : " + surname);
  12. }
  1. if(surname.equals("bobo")) {
  2. System.out.println("date set from file");
  3. GUI_view.getDateChooser().setDate( new SimpleDateFormat("yyyy-MM-dd").parse(part1) );
  4. }

但是我得到了这个错误?为什么?出了什么问题?

  1. 在线程 "AWT-EventQueue-0" 中的异常
  2. java.lang.ArrayIndexOutOfBoundsException: 1
  3. at controller.TaskController.openFile(TaskController.java:213)
  4. at controller.TaskController.lambda$11(TaskController.java:110)
  5. at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
  6. at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
  7. at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
  8. at javax.swing.DefaultButtonModel.setPressed(Unknown Source)

请注意,这部分代码并不与您提供的原始论文写作程序的框架直接相关。如果您需要进一步的代码实现或指导,请随时提问。

英文:

I want to read a file, file.txt that contains word pairs like this...

  1. yaniv:bobo

After reading this file.txt , I want to split this text and put each words in variables and try to compare them like this :

  1. Scanner scan = new Scanner(new FileReader(file));
  2. while(scan.hasNextLine()) {
  3. String descritpion = scan.nextLine();
  4. System.out.println("line" +descritpion);
  5. String []temp = descritpion.split(":");
  6. String name = temp[0];
  7. String surname = temp[1];
  8. System.out.println("name : "+ name);
  9. System.out.println("surname : "+ surname);
  10. }
  1. if(surname == "bobo") {
  2. System.out.println("date set from file ");
  3. GUI_view.getDateChooser().setDate( new SimpleDateFormat("yyyy-MM-dd").parse(part1) );
  4. }

BUT I GET THIS ERROR ?? WHY ?? WHats Wrong ??

  1. Exception in thread "AWT-EventQueue-0"
  2. java.lang.ArrayIndexOutOfBoundsException: 1
  3. at controller.TaskController.openFile(TaskController.java:213)
  4. at controller.TaskController.lambda$11(TaskController.java:110)
  5. at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
  6. at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
  7. at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
  8. at javax.swing.DefaultButtonModel.setPressed(Unknown Source)

答案1

得分: 0

不知道这是否会解决您的问题,但是改用以下方式可能会有帮助:

  1. if(surname.contains("bobo")) {

surname == "bobo" 会返回false,因为它比较的是surname的地址。

英文:

Don't know if this will fix your problem, but instead of

  1. if(surname == "bobo") {

Use:

  1. if(surname.contains("bobo")) {

surname == "bobo" will return false as its comparing the address of surname

答案2

得分: 0

检查文件的结尾方式。Scanner 足够聪明,可以忽略末尾的单个换行符。但是,如果之后还有任何东西(比如空格或另一个换行符),那将成为要读取的新行。

在这种情况下

> String descritpion = scan.nextLine();

将读取一个近似为空的字符串,然后

> String []temp = descritpion.split(":");

将其拆分成一个包含单个项的数组,其中

> String name = temp[0];

包含整个字符串(可能为空或包含一个空格或其他内容),这就是它成功的原因,
但是

> String surname = temp[1];

不存在,这就是它抛出异常的原因。
然而,在这种情况下,异常之前应该出现一个line。请参见此处的测试(使用字符串而不是文件):https://ideone.com/ixo0kd - 无换行和单个换行情况正常工作,换行后有空格和双换行情况会引发异常,但在异常之前有一个空的line显示出来。

英文:

Check how your file ends. Scanner is clever enough to throw away a single line-break from the end. However if there is anything (like a space or another line break) afterwards, that's going to be a new line to be read.
In such cases

> String descritpion = scan.nextLine();

will read an empty-ish string, then

> String []temp = descritpion.split(":");

splits it into a single-item array, where

> String name = temp[0];

contains the entire string (being empty or containing a single space or something), that's how it passes
but

> String surname = temp[1];

does not exist, and that's why it throws an exception.
However in such cases a line should appear on screen prior to the exception. See test (with strings instead of files) here: https://ideone.com/ixo0kd - the no-line-break, and single-line-break cases work fine, the space-after-line-break and double-line-break cases throw the exception, but have an empty line displayed before.

huangapple
  • 本文由 发表于 2020年8月6日 18:38:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/63281792.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定