[英]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.始终使用equals
或equalsIgnoreCase
方法比较字符串。
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.