简体   繁体   English

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

[英]Java: Read a File & Split a line into separate string

I want to read a file, file.txt that contains word pairs like this...我想读取一个文件,file.txt,其中包含这样的单词对......

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:在阅读完这个 file.txt 之后,我想拆分这个文本并将每个单词放入变量中,并尝试像这样比较它们:

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

BUT I GET THIS ERROR??但我得到这个错误?? WHY??为什么?? WHats Wrong??怎么了??

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

Don't know if this will fix your problem, but instead of不知道这是否会解决您的问题,但不是

if(surname == "bobo") {

Use:利用:

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

surname == "bobo" will return false as its comparing the address of surname surname == "bobo" 将返回 false 作为比较 surname 的地址

Your code seems fine when I compiled and ran it using a mock file.当我使用模拟文件编译和运行它时,您的代码似乎很好。 Maybe the problem is in your file.也许问题出在您的文件中。 please provide your file for further inspection.请提供您的文件以供进一步检查。

Also note that your are comparing strings using == .另请注意,您正在使用==比较字符串。 Always compare strings using equals or equalsIgnoreCase method.始终使用equalsequalsIgnoreCase方法比较字符串。

like:喜欢:

if(surname.equals("bobo") {}

Check how your file ends.检查您的文件如何结束。 Scanner is clever enough to throw away a single line-break from the end. Scanner足够聪明,可以从最后扔掉一个换行符。 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.但是,在这种情况下,屏幕上应该会在异常之前出现line 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.请参阅此处的测试(使用字符串而不是文件): https://ideone.com/ixo0kd - 无换行符和单行换行符情况正常,换行符后的空格和双行符-break 案例抛出异常,但之前显示一个空line

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java将每一行读入单独的ArrayList(字符串) - Java Read Each Line Into Separate ArrayList (String) 如何读取大型csv文件并将每一行拆分为java中的字符串数组 - How to Read large size csv file and split each line to string array in java Bash逐行读取文件,按选项卡拆分,发送到Java应用程序 - Bash read file line by line, split by tab, send to java application 在Java中,我需要读取一个文本文件并将每一行放在单独的数组中。 但是每次阅读文本文件时,我都无法分开 - In Java I need to read a text file and put each line in a separate array. But every time I read the text file I cannot split the lines 一次读取一行文本文件,并将单词拆分为Array Java - Read in a text file 1 line at a time and split the words into an Array Java 从文件中读取,分割线并存储在数组JAVA中 - read from a file, split the line and store in an array JAVA Java:将字符串拆分为单独的字符串和整数变量 - Java: Split a string into separate string and integer variables 如何在Java中一行一行地读取文本文件,并分别分隔每一行的内容? - How do you read a text file, line by line, and separate contents of each line in Java? 使用Java String.split从文件动态读取 - Dynamic read from file using java String.split 按新行拆分 Java 字符串 - Split Java String by New Line
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM