java 将用户输入字符串与从文本文件中读取的字符串进行比较
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16030173/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Comparing user input string to string read from text file
提问by user2285167
I'm currently writing this program that I require to read info from a text file and to then compare the info read to a user input and output a message saying if it was a match or not.
我目前正在编写这个程序,我需要从文本文件中读取信息,然后将读取的信息与用户输入进行比较,并输出一条消息,说明它是否匹配。
Currently have this. The program is sucessfully reading the data specified but I can't seem to compare the strings correctly at the end and print a result.
目前有这个。该程序正在成功读取指定的数据,但我似乎无法在最后正确比较字符串并打印结果。
Code is below any help would be greatly appreciated.
代码低于任何帮助将不胜感激。
import java.util.Scanner; // Required for the scanner
import java.io.File; // Needed for File and IOException
import java.io.FileNotFoundException; //Required for exception throw
// add more imports as needed
/**
* A starter to the country data problem.
*
* @author phi
* @version starter
*/
public class Capitals
{
public static void main(String[] args) throws FileNotFoundException // Throws Clause Added
{
// ask the user for the search string
Scanner keyboard = new Scanner(System.in);
System.out.print("Please enter part of the country name: ");
String searchString = keyboard.next().toLowerCase();
// open the data file
File file = new File("CountryData.csv");
// create a scanner from the file
Scanner inputFile = new Scanner (file);
// set up the scanner to use "," as the delimiter
inputFile.useDelimiter("[\r,]");
// While there is another line to read.
while(inputFile.hasNext())
{
// read the 3 parts of the line
String country = inputFile.next(); //Read country
String capital = inputFile.next(); //Read capital
String population = inputFile.next(); //Read Population
//Check if user input is a match and if true print out info.
if(searchString.equals(country))
{
System.out.println("Yay!");
}
else
{
System.out.println("Fail!");
}
}
// be polite and close the file
inputFile.close();
}
}
回答by Erik
You should try reading the input from a textField in an user interface(visible window) where the user puts the country and getting that as raw input shortens the code.(Only if you have a visible window on screen)
I don't have that good experience with scanners, because they tend to crash my applications when I use them. But my code for the same test does only include a scanner for the file which does not crash my application and looks like following:
您应该尝试从用户界面(可见窗口)中的 textField 读取输入,用户在其中放置国家/地区并将其作为原始输入获取会缩短代码。(仅当您在屏幕上有可见窗口时)
我没有使用扫描仪的良好体验,因为当我使用它们时,它们往往会使我的应用程序崩溃。但是我的同一个测试代码只包含一个不会使我的应用程序崩溃的文件扫描仪,如下所示:
Scanner inputFile = new Scanner(new File(file));
inputFile.useDelimiter("[\r,]");
while (inputFile.hasNext()) {
String unknown = inputFile.next();
if (search.equals(unknown)) {
System.out.println("Yay!");
}
}
inputFile.close();
I think the easiest way to compare string against a file is to add a visible window where the user types the country, and reading the input to a string with String str = textField.getText();
我认为将字符串与文件进行比较的最简单方法是添加一个可见窗口,用户可以在其中键入国家/地区,然后使用以下命令读取字符串的输入 String str = textField.getText();
回答by c0D3l0g1c
I am guessing that your comparison is failing due to case-sensitivity.
我猜您的比较由于区分大小写而失败。
Should your string comparison not be CASE-INSENSITIVE?
您的字符串比较不应该不区分大小写吗?
回答by NilsH
There are a few possible issues here. First, you're converting the searchString
to lower case. Are the data in the CSV also lower case? If not, try using equalsIgnoreCase
instead. Also, it seems to me like you should be able to match parts of the country name. In that case, equals
(or equalsIgnoreCase
) would only work if the user inputs the complete country name. If you want to be able to match only a part, use contains
instead.
这里有几个可能的问题。首先,您要将 转换searchString
为小写。CSV 中的数据也是小写的吗?如果没有,请尝试equalsIgnoreCase
改用。此外,在我看来,您应该能够匹配部分国家/地区名称。在这种情况下,equals
(或equalsIgnoreCase
) 仅在用户输入完整的国家/地区名称时才有效。如果您只想匹配一部分,请contains
改用。