别有用心的方法来包含?
Ulterior method to contains?
我正在尝试查看我的文件是否包含 1 个以上的字段。当我在扫描仪中输入 agentName 和 agent_id 时,它会检查文件中输入的值。它只适用于 if 语句中的一个条件。当我将第二个语句放在布尔值 && 之后的 if 中时,它没有 return 任何东西。
有人知道我能做什么吗?我也试过嵌套 if,但还是一样。
如有任何帮助,我们将不胜感激
提前致谢!!!
package p;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Login {
public static final Scanner input = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("Do you want to Login as a Club or an Agent? ");
System.out.println("Enter C for club or A for Agent or press e to exit !!! ");
char ch = input.next().charAt(0);
if (ch == 'A' || ch == 'a') {
String agentName = "";
String agent_id = "";
String line;
try {
FileReader fr = new FileReader("Agent.txt");
BufferedReader br = new BufferedReader(fr);
System.out.println("Please enter the name of the Agent \n");
agentName = input.next();
System.out.println("\n");
System.out.println("Please enter the Agents id ");
agent_id = input.next();
System.out.println("\n");
List<String> playerInfo = new ArrayList<String>();
while ((line = br.readLine()) != null) {
if (line.toLowerCase().contains(agentName.toLowerCase())
&& line.toLowerCase().contains(agent_id.toLowerCase())) {
int numLines = 2;//represents the number of lines in the file I want to show
playerInfo.add(line);
while ((line = br.readLine()) != null && numLines >= 0) {
playerInfo.add(line);
numLines--;
System.out.println("Agent found...... \n");
System.out.println("Loading.....");
System.out.println(playerInfo);
break;
} // inner while
} // if
} // while
} catch (IOException e) {
}
}
}
}
除非两个值都在同一行,否则它将 return 为假。您需要使用 ||
而不是 &&
。请尝试以下操作:
if (line.toLowerCase().contains(agentName.toLowerCase())
|| line.toLowerCase().contains(agent_id.toLowerCase())) {
// rest of code here
我正在尝试查看我的文件是否包含 1 个以上的字段。当我在扫描仪中输入 agentName 和 agent_id 时,它会检查文件中输入的值。它只适用于 if 语句中的一个条件。当我将第二个语句放在布尔值 && 之后的 if 中时,它没有 return 任何东西。
有人知道我能做什么吗?我也试过嵌套 if,但还是一样。
如有任何帮助,我们将不胜感激 提前致谢!!!
package p;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Login {
public static final Scanner input = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("Do you want to Login as a Club or an Agent? ");
System.out.println("Enter C for club or A for Agent or press e to exit !!! ");
char ch = input.next().charAt(0);
if (ch == 'A' || ch == 'a') {
String agentName = "";
String agent_id = "";
String line;
try {
FileReader fr = new FileReader("Agent.txt");
BufferedReader br = new BufferedReader(fr);
System.out.println("Please enter the name of the Agent \n");
agentName = input.next();
System.out.println("\n");
System.out.println("Please enter the Agents id ");
agent_id = input.next();
System.out.println("\n");
List<String> playerInfo = new ArrayList<String>();
while ((line = br.readLine()) != null) {
if (line.toLowerCase().contains(agentName.toLowerCase())
&& line.toLowerCase().contains(agent_id.toLowerCase())) {
int numLines = 2;//represents the number of lines in the file I want to show
playerInfo.add(line);
while ((line = br.readLine()) != null && numLines >= 0) {
playerInfo.add(line);
numLines--;
System.out.println("Agent found...... \n");
System.out.println("Loading.....");
System.out.println(playerInfo);
break;
} // inner while
} // if
} // while
} catch (IOException e) {
}
}
}
}
除非两个值都在同一行,否则它将 return 为假。您需要使用 ||
而不是 &&
。请尝试以下操作:
if (line.toLowerCase().contains(agentName.toLowerCase())
|| line.toLowerCase().contains(agent_id.toLowerCase())) {
// rest of code here