尝试将随机生成的密码存储为字符串,以便我可以将其保存到文件中(Java)

huangapple go评论73阅读模式
英文:

Trying to store randomly generated password as a string so then i can save it to a file (Java)

问题

以下是翻译好的内容:

我正在尝试创建一个随机密码生成器,我已经成功让系统根据用户输入的字符长度生成随机字符串。接下来,我想要的是询问用户是否想要保存文件。在文件中,我想要包含网站/应用的名称和密码。

我的问题是,我无法将密码的随机字符串存储为变量,有谁知道如何解决这个问题?

代码如下所示:

        System.out.println("请输入您需要的密码长度:");
        int passwordLength = sc.nextInt();
        sc.nextLine();
        Random r = new Random();
        String alphabet = "abcde5678fghijklmn123opqrs?!£$%tuvwxyzABCDEFGHIJKLMN490@#/*(OPQRSTUVWXYZ)";
        for (int i = 0; i < passwordLength; i++) {
            System.out.print(alphabet.charAt(r.nextInt(alphabet.length())));
        }
英文:

So what I am trying to do is create a random password generator, I have managed to make the system produce a random string of characters based on the char length that the user inputs. What I then want to do is ask the user if they want to save the file. On the file I want to include the name of the website/application and the password.

My problem is I cannot get the random string for the password to be stored as a variable, does anyone know how to fix this?

Code can be seen below:

    System.out.println(&quot;Please enter the length of password that you require: &quot;);
    int passwordLength = sc.nextInt();
    sc.nextLine();
    Random r = new Random();
    String alphabet = &quot;abcde5678fghijklmn123opqrs?!&#163;$%tuvwxyzABCDEFGHIJKLMN490@#/*(OPQRSTUVWXYZ)&quot;;
    for (int i = 0; i &lt; passwordLength; i++) {
        System.out.print(alphabet.charAt(r.nextInt(alphabet.length())));

    }

答案1

得分: 1

我相信您的意思是希望将密码收集到一个字符串变量中,而不是打印每个字符。以下是一个完整工作示例:

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println("请输入您需要的密码长度:");
    int passwordLength = sc.nextInt();
    sc.nextLine();
    Random r = new Random();
    String alphabet = "abcde5678fghijklmn123opqrs?!&#163;$%tuvwxyzABCDEFGHIJKLMN490@#/*(OPQRSTUVWXYZ)";
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < passwordLength; i++)
        sb.append(alphabet.charAt(r.nextInt(alphabet.length())));
    String password = sb.toString();
    System.out.print(password);
}

结果:

请输入您需要的密码长度:
12
j$miDCadnJMd
英文:

I believe you mean that you want to collect the password into a String variable instead of printing each character. Here's how to do that in a completely working example:

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println(&quot;Please enter the length of password that you require: &quot;);
    int passwordLength = sc.nextInt();
    sc.nextLine();
    Random r = new Random();
    String alphabet = &quot;abcde5678fghijklmn123opqrs?!&#163;$%tuvwxyzABCDEFGHIJKLMN490@#/*(OPQRSTUVWXYZ)&quot;;
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i &lt; passwordLength; i++)
        sb.append(alphabet.charAt(r.nextInt(alphabet.length())));
    String password = sb.toString();
    System.out.print(password);
}

Result:

Please enter the length of password that you require: 
12
j$miDCadnJMd

答案2

得分: 0

如果您不想使用字符串生成器,可以这样做:

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println("请输入所需的密码长度:");
    int passwordLength = sc.nextInt();
    sc.nextLine();
    Random r = new Random();
    String alphabet = "abcde5678fghijklmn123opqrs?!&#163;$%tuvwxyzABCDEFGHIJKLMN490@#/*(OPQRSTUVWXYZ)";
    String password = "";
    for (int i = 0; i < passwordLength; i++)
        password += (alphabet.charAt(r.nextInt(alphabet.length()))) + "";
    System.out.print(password);
}

请注意,我只翻译了代码部分,不包括注释和其他内容。

英文:

If you dont want to use a String Builder, you could do this

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println(&quot;Please enter the length of password that you require: &quot;);
    int passwordLength = sc.nextInt();
    sc.nextLine();
    Random r = new Random();
    String alphabet = &quot;abcde5678fghijklmn123opqrs?!&#163;$%tuvwxyzABCDEFGHIJKLMN490@#/*(OPQRSTUVWXYZ)&quot;;
    String password = &quot;&quot;;
    for (int i = 0; i &lt; passwordLength; i++)
        password += (alphabet.charAt(r.nextInt(alphabet.length()))) + &quot;&quot;;
    System.out.print(password);
}

huangapple
  • 本文由 发表于 2020年10月22日 04:47:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/64471513.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定