[英]if & else statements
我正在寻找一些小问题的帮助。 基本上我的应用程序中有一个“ if&else”语句,但是我想添加另一个“ if”语句,该语句检查文件,然后检查该文件中的某些文本行。 但是我不确定该怎么做。
这是我有的
if(file.exists()) {
do this
} else {
do this
}
听起来您要么需要:
if (file.exists() && readFileAndCheckForWhatever(file)) {
// File exists and contains the relevant word
} else {
// File doesn't exist, or doesn't contain the relevant word
}
要么
if (file.exists()) {
// Code elided: read the file...
if (contents.contains(...)) {
// File exists and contains the relevant word
} else {
// File exists but doesn't contain the relevant word
}
} else {
// File doesn't exist
}
或颠倒前一个逻辑以使其扁平化
if (!file.exists()) {
// File doesn't exist
} else if (readFileAndCheckForWhatever(file)) {
// File exists and contains the relevant word
} else {
// File exists but doesn't contain the relevant word
}
除非我缺少任何东西,否则您难道不可以使用else if
吗?
else if((file.exists())&&(!file.contains(Whatever))) { ... }
需要将File.contains
交换为实际上检查文件的功能,但是您明白了。
也许您的意思是这样的:
if(file.exists() && containsLine(file))
{
// do something
}
else
{
// do something else
}
public boolean containsLine(File f)
{
// do the checking here
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.