首页 > 解决方案 > 名称搜索程序问题。在解析 } 错误消息时获取“到达文件末尾”

问题描述

我似乎无法编译这段代码,你能告诉我我做错了什么吗?我尝试在所有地方添加 },但似乎没有任何效果。太感谢了!我可以使用哪些修复程序来正确编译此代码?

import java.util.ArrayList;
import java.util.Scanner;
import java.io.*;

public class NameSearch {
    public static void main(String args[]) throws IOException {

        ArrayList<String> searchName = new ArrayList <String>();
        String searchName = getNames();
        String boysNames;
        String girlsNames;

        displaySearchResults(searchName, boysNames, girlsNames);
    } 


    public static String getNames() {
        Scanner keyboard = new Scanner(System.in);

        File boysNames = new File("BoysNames.txt");
        Scanner inputFileBoysNames = new Scanner (boysNames);

        File girlsNames = new File("GirlsNames.txt");
        Scanner inputFileGirlsNames = new Scanner (girlsNames);

        System.out.println("Top 200 Name Search");
        System.out.print("Enter the name of your choosing here: ");

        String name = Keyboard.nextLine();
        Keyboard.close();
        return name;
    }

    public static void displaySearchResultsPing 
(String searchName, List<String> boysNames, List<String> girlsNames); 

    {

        System.out.println("\nBOOM! here are the results of your search: \n");

        boolean popularBoyName = boysNames.stream().anyMatch (p ->  p.equalsIgnoreCase(searchName));
        boolean popularGirlName = girlsNames.stream().anyMatch (p -> p.equalsIgnoreCase(searchName));


        if (popularBoyName) {
            System.out.println(searchName + "is a super popular boy's name. Pick another.");
        }
        if (popularGirlName) {
            System.out.println(searchName + "is a super popular girl's name. Pick another.");
        }
        if (!popularBoyName && !popularGirlName) {
            System.out.println (searchName + "is not a popular name. You have chosen correctly!");
        }
    }

标签: javacompiler-errors

解决方案


一个问题是您的displaySearchResultsPing方法签名以;.

另一个是在main您调用的方法中,displaySearchResults()但它不存在。

然后您从文件中读取名称,然后丢弃这些变量。例如:

File boysNames = new File("BoysNames.txt");  // this is never kept or returned

并且方法中的boysNamesgirlsNames变量main永远不会设置为任何值 - 总是null


推荐阅读