简体   繁体   中英

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

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

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:

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

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.

like:

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM