将文本文件中的数据与 Java 中的用户输入进行匹配
我需要从记事本文件(如下)中检索 PIN 码,并使用用户输入的 PIN 码进行检查。我已经尝试了好几天了,但到目前为止,我提出的解决方案仅在我输入整行(即 1598 01-10-102203-0 95000)时才给我正确的输出。它还为每条记录显示“无效 PIN”。
PIN AccountNo Balance 1598 01-10-102203-0 95000 4895 01-10-102248-0 45000 9512 01-10-102215-0 125000 6125 01-10-102248 85000 Output - You have login! Invalid PIN Invalid PIN Invalid PIN
BufferedReader getIt = new BufferedReader(new InputStreamReader(System.in));
String userPIN = "";
try {
// Open the file that is the first command line parameter
FileInputStream fstream = new FileInputStream(
"D:\\Studies\\BCAS\\HND\\Semester 1\\Programming "
+ "Concepts\\Assignment\\AccountInfo.txt");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
//Read File Line By Line
System.out.println("Enter PIN");
userPIN = getIt.readLine();
while ((strLine = br.readLine()) != null) {
// Print the content on the console#
if (userPIN.equals(strLine)) {
System.out.println("You have login!");
} else {
System.out.println("Invalid PIN!");
}
}
//Close the input stream
in.close();
} catch (Exception e) {//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
I need to retrieve the PIN from a notepad file (below) and check it with the PIN which the user has typed. I have tried this for days, but so far the solution that I have come up with gives me the correct output only when I type the full row (i.e. 1598 01-10-102203-0 95000). Also it displays the "Invalid PIN" for each and every record.
PIN AccountNo Balance 1598 01-10-102203-0 95000 4895 01-10-102248-0 45000 9512 01-10-102215-0 125000 6125 01-10-102248 85000 Output - You have login! Invalid PIN Invalid PIN Invalid PIN
BufferedReader getIt = new BufferedReader(new InputStreamReader(System.in));
String userPIN = "";
try {
// Open the file that is the first command line parameter
FileInputStream fstream = new FileInputStream(
"D:\\Studies\\BCAS\\HND\\Semester 1\\Programming "
+ "Concepts\\Assignment\\AccountInfo.txt");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
//Read File Line By Line
System.out.println("Enter PIN");
userPIN = getIt.readLine();
while ((strLine = br.readLine()) != null) {
// Print the content on the console#
if (userPIN.equals(strLine)) {
System.out.println("You have login!");
} else {
System.out.println("Invalid PIN!");
}
}
//Close the input stream
in.close();
} catch (Exception e) {//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Jon Freedman 出色的答案,您应该考虑接受,是您必须将传入的文本行分解为其组成部分,以便将它们与键入的内容进行比较。这是一种方法:
这会产生以下输出:
附录:
An essential element of Jon Freedman's excellent answer, which you should consider accepting, is that you must break up the incoming line of text into its component parts in order to compare them to what is typed. Here's one approach:
This produces the following output:
Addendum: