简体   繁体   中英

How can I check if the value returned by .split() is empty?

I'm reading from a text file using scanner and I have the data in each line stored in a string variable. Note: The actual code is really, really long, this is just a shortened version

String line;
String splitResult[];
Scanner a= new Scanner (new File ("methods.txt"));
while (a.hasNextLine()){
  line = a.nextLine();
  splitResult = line.Split(" ");
}
a.close();

}

My text file looks like this

Micheal Smith James Matt
John Peter Donald Sophia

How can I check if James or Donald or any of the contents of the file is not there.

I tried this if statement but it does't work

if (splitResult.length <= 4 && !(words[words.length - 1].isEmpty())

Use this Below Code Snip

String line;
String splitResult[];
Scanner a= new Scanner (new File ("../yourProjectName/src/methods.txt"));
while (a.hasNextLine()){
  line = a.nextLine();
  splitResult =line.split(" ");
  for (String string : splitResult) {
      if("James".equals(string)|| "Donald".equals(string))
      System.out.println(string);
}

}
a.close();

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