パスワードとユーザー名を2つの別々のファイル(それに応じて)に設定して保存できるログインプログラムをコーディングしています。私の問題は、ユーザー名をパスワードに設定する方法を理解できないことです。例:ユーザ名はBob、パスワードはcat、ユーザ名はBob、パスワードはcatと入力する必要があります。そうしないとエラーが発生します(ユーザがbillとしてユーザ名を入力し、パスワードをcatとして入力すると、ユーザー名はそれぞれのパスワードではありません)。どんな助けもいいだろう。 *私はまだパスワード部分と同じ構造であるため、ユーザー名部分を追加していません。パスワードに対応するユーザー名が必要です。JAVAユーザー名とログイン用のパスワードを一致させる
ここは私です私は参考のために、これまで持っているコード:
import java.io.File;
import java.io.PrintStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.io.FileReader;
import java.io.*;
import java.util.*;
import javax.swing.JOptionPane;
public class PassCode
{
static String adminPassword = "Change Password";
public static void fileMaker() throws IOException
{
PrintStream standard = System.out;
File f = new File("Password.txt");
FileOutputStream fs= new FileOutputStream(f);
PrintStream ps = new PrintStream(fs);
System.setOut(ps);
String theMessage = "Set New Password";
String userInput = JOptionPane.showInputDialog(theMessage);
System.out.println(userInput);
ps.close();
System.setOut(standard);
}
public static void Checker() throws IOException
{
Scanner inputStream = new Scanner(new FileReader("Password.txt")); //Scans declared file for text on the first line
String fileChecker = inputStream.nextLine(); //Sets scanned line into a string variable
if(fileChecker.isEmpty())
{
fileMaker();
}
else
{
int reply = JOptionPane.showConfirmDialog(null, "Would you like to change the current password", "Warning!", JOptionPane.YES_NO_OPTION);
if (reply == JOptionPane.YES_OPTION)
{
String inquire = "Type Administrator Password to Proceed";
boolean flag = true;
while(flag == true)
{
String confirm = JOptionPane.showInputDialog(inquire);
if(confirm.equals(adminPassword))
{
fileMaker();
flag = false;
}
else
{
inquire = "Incorrect!" + "\n" + "Retry";
}
}
}
}
}
public static void main(String[] args) throws IOException
{
Checker();
Scanner inputStreamThree = new Scanner(new FileReader("Password.txt"));
String line = inputStreamThree.nextLine();
String question = "Password Please";
Boolean right = true;
while(right == true)
{
String ask = JOptionPane.showInputDialog(question); //Asks for user to input password
if(ask.equals(adminPassword)) //Checks if user input the admin password
{
fileMaker();
Scanner inputStreamAdmin = new Scanner(new FileReader("Password.txt")); //Scans for admin password
String adminChecker = inputStreamAdmin.nextLine(); //Sets scanned line as a new string variable
line = adminChecker;
}
else if(line.equals(ask)) //Checks if user password is correct
{
System.out.println("Welcome Fellow Programmer to the Now Functioning Password Checker!" +
"\n" + "Date Today: 10/31/2017" +
"\n\n\n\n\n\n\n" + "Did you figure out the Admin password yet?");
right = false;
}
else if(ask != line) //Checks if user password is incorrect
{
question = "Incorrect Password!";
}
}
}
}
は、2つの異なるファイルの要求をしていますか? –
それが好ましいですが、それのまわりに別の方法があるなら、それはうまくいくでしょう。また、2つの異なるファイルを使用して、パスワードとユーザー名の両方を別々に保管したいと考えました。 –