gpt4 book ai didi

java - 搜索 ArrayList

转载 作者:行者123 更新时间:2023-12-01 14:05:53 25 4
gpt4 key购买 nike

自从编写了一些代码以来,我对在某个部分之后要做什么有点困惑。它应该通过 ArrayList 放入一个文本文件,然后用户应该能够键入团队名称,并且应该添加它们在文本文件中弹出的次数。但在我的一生中,我一直被困在如何在我拥有的东西之后继续前进。

 public static void main(String[] args) throws FileNotFoundException {
ArrayList<String> mlb = new ArrayList<String>();
Scanner fileReader = new Scanner(new File("WorldSeriesWinners.txt")); // Declares the scanner to read from the file.
int Count = 0;

Scanner keyboard = new Scanner(System.in);
String userInput;


while (fileReader.hasNext()){
// While loop to continue to read while there is another line.
mlb.add(fileReader.nextLine());
}
System.out.println("Enter a championship team you are looking up(1903-2009).");
userInput = keyboard.nextLine();

for(String team : mlb){ // Will run through the file and display the ArrayList.
if(team.contains(userInput))
System.out.println(team);

最佳答案

将 userInput 在 ArrayList 中出现的次数相加。

int count = 0;
for(String team : mlb){ // Will run through the file and display the ArrayList.
if(team.contains(userInput))
count++;
}
System.out.println(count);

关于java - 搜索 ArrayList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18908455/

25 4 0