这是GameModule.java的代码:
#!/bin/sh
v1="-E"
v2="s/./x/g"
sed "$v1" "$v2" <<<"abc" # result = "xxx"
#!/bin/sh
v1="-E"
v2="s/./x/g"
sed "$v1 $v2" <<<"abc" # result = "sed: illegal option..."
是另一个java文件/类。 public void run()
{
System.out.println("FISHING PAIR GAME LOGIN");
Utility.printLine(50);
System.out.println();
ArrayList<Player> players = new ArrayList<>();
Scanner myScanner=null;
try {
myScanner = new Scanner (new File("players.dat"));
while (myScanner.hasNextLine())
{
String data = myScanner.nextLine();
String [] dataSplit = data.split("\\|");
Player existingPlayer = new Player (dataSplit[0], dataSplit[1],dataSplit[2], Integer.parseInt(dataSplit[3]));
players.add(existingPlayer);
}
} catch(FileNotFoundException ex) {
System.out.println("Error! File - players.dat not found!");
}
myScanner.close();
//End of read file into array
Scanner input = new Scanner (System.in);
boolean exist = false;
System.out.print("Enter Login name > ");
String userName = input.nextLine();
System.out.print("Enter Password > ");
String userPW = input.nextLine();
System.out.println();
for (int i=0; i < players.size(); i++) {
//Verifies user login name and password
if (players.get(i).getLoginName().equalsIgnoreCase(userName)&&(players.get(i).checkPassword(userPW)))
{
char continueGame='y';
//Loop for allowing player to continue the game
while(continueGame=='y'||continueGame=='Y') {
//Ensure player have sufficient chips to play the game
if (players.get(i).getScores()>0) {
//Game function which returns the chips of the player after the game
players.get(i).setScores(startGame(players.get(i).getLoginName(), players.get(i).getScores()));
System.out.print("Next Game? (Y/N) > ");
continueGame = input.next().charAt(0);
} else {
continueGame = 'n';
System.out.println("Sorry you do not have enough chips.");
}
}
System.out.println("Thank you for playing, see you again!");
exist = true;
}
}
//If user id or password does not match or incorrect
if (exist == false) {
System.out.println("Error! incorrect username or password!");
}
//Write array into file
try {
PrintWriter fileOut = new PrintWriter("players.dat");
//BufferedWriter fileOut = new BufferedWriter(new FileWriter("players.dat", false));
for (int i = 0; i < players.size(); i++) {
fileOut.println(players.get(i).getLoginName() + "|" + players.get(i).getHashPass() + "|" + players.get(i).getDate() + "|"
+ players.get(i).getScores());
//fileOut.printLn();
}
fileOut.close();
//System.out.println("Data saved");
} catch (IOException e) {
System.out.println("Failed to open file");
}
}
是GameModule中的一种方法。
正如你所看到的那样......首先检查player.dat是否存在。然后转到登录部分。在登录期间,它将检查用户名和/或密码是否在文件中。
这是输出:
Player
在player.dat文件中-
IcePeak | P1 | - | 10
BlueKnight | P2 | - | 10
BlackRanger | P3 | - | 10
GoldDigger | P4 | - | 10
' - '表示登录日期。由于球员是新的,没有约会。
好吧,你可以看到IcePeak和密码p1已经存储在那个文件中,但是我收到了错误信息。我已逐行重读所有代码,甚至重新输入。我忘了我试过的另一件事。我甚至删除了文件并使用相同的信息重新创建它。但我仍然收到相同的错误消息。我只是不知道并且不明白出了什么问题:(
所以请帮助我,谢谢:)
编辑 - 添加了Player.java
startGame()
第二次编辑 - User.java
FISHING PAIR GAME LOGIN
--------------------------------------------------
Enter Login name> IcePeak
Enter Password> p1
Error! incorrect username or password!
答案 0 :(得分:0)
我已经弄清楚问题是什么
在我的players.dat文件中,它应该是哈希密码而不是普通密码。
由于我的文件链接在一起(多态),所有文件共享players.dat文件。此外,这些文件还包含散列密码的代码。
谢谢你帮我解决了^^
答案 1 :(得分:-1)
我怀疑你的输入可能包含不可显示的字符。你使用String.equalsIgnoreCase(),如果输入包含&#34;空格或控制&#34;字符,Java完全不匹配。首先尝试修剪()。您应该能够在控制台中打印出用户名和密码,并附上引号,以便仔细检查。
我知道这不是答案,但我没有足够的声誉来添加评论:(