我创建了一个Java程序,它读取每个名称都有两个字符串的.txt文件。
Rob Junior
鲍比凯利当它读取它并且找不到相关名称时,它将显示一个对话框错误消息。我遇到的问题是程序正在读取每一行,如果它不匹配,将显示每行的错误消息。这是我的代码片段:
Scanner fileScan = new Scanner (new File("1.txt"));
while (fileScan.hasNextLine()){
String input = fileScan.nextLine();
String Username = input.substring(0,input.indexOf(' '));
String Password = input.substring(input.indexOf(' '),input.length());
if (Username.equals(inputusername) || (Password.equals(inputpassword)))
{
getContentPane().removeAll();
getContentPane().add(panel3);//Adding to content pane, not to Frame
repaint();
printAll(getGraphics());
}
else {
JOptionPane.showMessageDialog(null,
"Invalid password. Try again.",
"Error Message",
JOptionPane.ERROR_MESSAGE);
}
}
inputusername
和inputpassword
是读取程序中文本字段的字符串。我知道我应该使用除了while循环之外的东西,但似乎无法将它拼凑在一起。
答案 0 :(得分:0)
您要做的是移动检查以查看密码是否不在while循环外的文件中。以下代码通过将临时变量设置为false,然后在找到用户名和密码组合时将其设置为true来执行此操作。否则,它仍为false,在检查文件后,我们显示对话框错误。
Scanner fileScan = new Scanner (new File("1.txt"));
boolean found = false; // added this variable
while (fileScan.hasNextLine()) {
String input = fileScan.nextLine();
String Username = input.substring(0,input.indexOf(' '));
String Password = input.substring(input.indexOf(' '),input.length());
if (Username.equals(inputusername) || (Password.equals(inputpassword))) {
found = true; // added this to set found
getContentPane().removeAll();
getContentPane().add(panel3);//Adding to content pane, not to Frame
repaint();
printAll(getGraphics());
} // removed the else statement
}
if(!found) { // added the contents of the previously existing else statement here, outside the while
JOptionPane.showMessageDialog(null,
"Invalid password. Try again.",
"Error Message",
JOptionPane.ERROR_MESSAGE);
}
答案 1 :(得分:0)
试试这个:
Scanner fileScan = new Scanner(new File("1.txt"));
boolean founduser = false;
while (fileScan.hasNextLine()) {
String input = fileScan.nextLine();
String Username = input.substring(0, input.indexOf(' '));
String Password = input.substring(input.indexOf(' '), input.length());
if (Username.equals(inputusername) && (Password.equals(inputpassword))) {
founduser = true;
break;
}
}
if (founduser) {
getContentPane().removeAll();
getContentPane().add(panel3);// Adding to content pane, not to Frame
repaint();
printAll(getGraphics());
}
else {
JOptionPane.showMessageDialog(null, "Invalid password. Try again.", "Error Message",
JOptionPane.ERROR_MESSAGE);
}
答案 2 :(得分:0)
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class FileReading {
public static void main(String args[]) throws FileNotFoundException{
Scanner fileScan = new Scanner (new File("test.txt"));
boolean found = false; // added this variable
while (fileScan.hasNextLine()) {
String input = fileScan.nextLine();
String Username = input.substring(0,input.indexOf(' '));
String Password = input.substring(input.indexOf(' ')+1,input.length());
if ((Username.equals("pawan")) && (Password.equals("kalyan"))) {
found = true; // added this to set found
System.out.println("Success");
}
}
if(!found) {
System.out.println("failed");
}
}
}
答案 3 :(得分:0)
这是您问题的解决方案
import java.io.*;
import java.util.*;
public class Credentials {
public static void main(String[] args) throws Exception {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
System.out.println("Enter user name");
String inputUser = br.readLine();
System.out.println("Enter Password");
String inputPassword = br.readLine();
Credentials c =new Credentials();
c.readPassword(inputUser,inputPassword );
}
void readPassword(String inputUser,String inputPassword) throws Exception {
Scanner scr = new Scanner(new File("password.txt"));
boolean flag=false;
while(scr.hasNextLine()){
String line = scr.nextLine();
String username= line.substring(0,line.indexOf(' '));
String password =line.substring(line.indexOf(' ')+1,line.length());
System.out.println(username+","+password);
if(inputUser.endsWith(username) && inputPassword.equals(password) ){
System.out.println("Valid user");
flag=true;
break;
}
}
if(!flag){
System.out.println("invalid user");
}
}
}