gpt4 book ai didi

java - 包含什么不可告人的方法?

转载 作者:行者123 更新时间:2023-12-02 09:07:51 25 4
gpt4 key购买 nike

我正在尝试查看我的文件是否包含超过 1 个字段。当我在扫描仪中输入agentName 和agent_id 时,它会检查文件中是否有输入的值。它仅适用于 if 语句中的一个条件。当我将第二个语句放在 boolean 值 && 之后的 if 中时,它不会返回任何内容。

有人知道我能做什么吗?我也尝试过使用嵌套 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) {

}

}

}
}


最佳答案

除非两个值在同一行,否则它将返回 false。您需要使用 || 而不是 &&。请尝试以下操作:

            if (line.toLowerCase().contains(agentName.toLowerCase())
|| line.toLowerCase().contains(agent_id.toLowerCase())) {
// rest of code here

关于java - 包含什么不可告人的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59669020/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com