【问题标题】:Compare input to a specific line saved in a text file JAVA将输入与保存在文本文件 JAVA 中的特定行进行比较
【发布时间】:2017-05-05 13:50:02
【问题描述】:

为了验证用户输入的国家/地区,我尝试将该国家/地区输入与存储在文本文件中的国家/地区列表进行比较。如果输入与存储在文本文件中的国家/地区匹配,validCountry 将设置为“true”,程序将能够继续。这是我目前所得到的:

    Scanner sc = new Scanner (System.in);
    String country = "";
    boolean validCountry = false;

    while (!validCountry)
    {
        System.out.print("Country: ");
        String countryIn = sc.next();
        try{
            Scanner scan = new Scanner(new File("countries.txt"));
            while (scan.hasNext()) {
                String line = scan.nextLine().toString();
                if(line.contains(countryIn))
                {
                    country = line; 
                    validCountry = true;

                }
            }
        }catch(Exception e)
        {
            System.out.print(e);
        }
    }      

上面只是循环让我重新输入国家(暗示它是无效的)。

这就是 countries.txt 文件的样子(显然包含世界上所有的国家,而不仅仅是以 'A' 开头的前几个国家:

Afghanistan
Albania
Algeria
American Samoa  
Andorra 
Angola  
Anguilla
...

我确定这是一个非常简单的小错误,我似乎找不到;但我一直试图检测它一段时间但无济于事。我已经检查了多个其他 stackoverflow 答案,但它们似乎也不起作用。我真的很感激任何形式的帮助:)

如果我的问题需要进一步澄清,请告诉我。

【问题讨论】:

  • 你得到什么样的错误?文本文件是否正确加载?
  • 我没有收到任何具体错误,它只是循环返回让我输入国家/地区。我假设文本文件已正确加载 - 否则控制台会给我一个“找不到文件”错误;它没有..所以我认为它已正确加载?
  • 是的,如果您没有收到错误,那么它可能已正确加载。但是,也许您检查 scan.hasNext() 是否曾经返回 true (通过调试或通过 System.out.println()

标签: java validation loops compare text-files


【解决方案1】:

我测试了代码,它对我有用。 我像这样初始化了你的变量sc

Scanner sc = new Scanner(System.in);

请注意,最好在 while 循环之外加载文件(以获得更好的性能)

【讨论】:

  • 我没有编译错误或超出范围的错误或任何类似的错误......该程序似乎只是循环返回让我输入一个国家!它对你有什么作用?!它返回什么?
  • 我添加了一个"System.out.println("ok:" + country);"在您的代码末尾,如果我输入“安哥拉”,它会写给我“好的:安哥拉”。
【解决方案2】:

假设在 String countryIn = sc.next();sc 是一个使用 System.in 的扫描器,更改 i>.next() 变成 nextLine():

String countryIn = sc.nextLine();

然后,您还应该更改 if(line.contains(countryIn)),因为即使给定的行是国家/地区的子字符串,它也会返回 true (afg将在 afghanistan 中找到,即使 afg 不在国家/地区列表中。请改用 equalsIgnoreCase

if (line.equalsIgnoreCase(countryIn)) {
...
}

试试这个类是否有效:

import java.util.Scanner;
import java.io.File;

public class Country {

    public static void main(String[] args) {
        Scanner sc = new Scanner (System.in);
        String country = "";
        boolean validCountry = false;

        while (!validCountry)
        {
            System.out.print("Country: ");
            String countryIn = sc.nextLine();
            try{
                Scanner scan = new Scanner(new File("countries.txt"));
                while (scan.hasNext()) {
                    String line = scan.nextLine();
                    if(line.equalsIgnoreCase(countryIn))
                    {
                        country = line; 
                        validCountry = true;
                        break;
                    }
                }
            }catch(Exception e)
            {
                e.printStackTrace();
            }
        }   
    }
}

【讨论】:

  • 是的,sc 是一个使用 System.in 的扫描仪。我已将 .next() 更改为 .nextLine() 并将 .contains 更改为 .equalsIgnoreCase() ......但它仍然不起作用! System.out.print("国家:");重新循环让我输入一个国家 - 无论我是从文本文件中输入完整的垃圾还是实际的国家
  • 更正您在示例中提供的 countries.txt:县名在行尾包含多余的空格。尝试从文件中删除它们
  • 我尝试替换 if 语句中的代码: from country = line;有效国家=真;到一个简单的 System.out.print(line);...但它仍然重新循环让我输入一个国家而不是打印出所有内容
  • 好吧,如果 if 中的条件不成立,则不会执行里面的代码。您是否尝试更正国家/地区文件?
  • 问题解决了! country.txt 文件以 UNICODE 编码,我所要做的就是将其更改为 ANSI,现在它可以正常工作了!我惊呆了
【解决方案3】:

问题解决了,我拥有的 countries.txt 文件是用 UNICODE 编码的。我所要做的就是将其更改为 ANSI。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-07
    • 1970-01-01
    • 2010-12-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多