0

For a school project I have to build a password generator. This must consist of 3 different methods, including 1 main method and then 2 other methods.

The following is what I have so far. Does anyone have an idea how I can add a third method here?

public class main {

    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        System.out.println("Wil je je wachtwoord resetten? Ja of nee");
        String pass=input.next();
        input.close();

        if("Ja".equalsIgnoreCase(pass)) {
            System.out.println(generatePassword(28));
        } else{
            System.out.println("Okee, volgende keer beter!");
        }
    }
    static char[] generatePassword(int length) {
        System.out.println("Nieuw wachtwoord: ");
        String letters="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvw";
        String nummers="0123456789";
        String raarTeken="!@#$%^&*)(}{[]<>?:;";

        String pwd=letters+nummers+raarTeken;
        Random r=new Random();
        char[] newPass=new char[length];

        for(int i=0; i<length; i++) {
            newPass[i]=pwd.charAt(r.nextInt(pwd.length()));
        }
        return newPass; 
    }
}
Ivar
  • 6,138
  • 12
  • 49
  • 61
iDaily
  • 3
  • 2
  • do you need a third method? – Stultuske Oct 07 '19 at 09:25
  • You don't need one. You could move `pwd.charAt(r.nextInt(pwd.length()))` to it's own method. For no reason. – f1sh Oct 07 '19 at 09:25
  • Yes, it is one of the requirements of the assignment – iDaily Oct 07 '19 at 09:26
  • what will that look like? – iDaily Oct 07 '19 at 09:30
  • You can put the part where you ask if the user wants to reset their password in a separate method. Then let it return a `boolean` that you can use in the `if` in your main. As a side note, you should never close a `Scanner` that uses `System.in`. [It can break things if you want to use it at a later point](https://stackoverflow.com/q/13042008). – Ivar Oct 07 '19 at 09:31
  • Sorry, I am not completely familiar with a bolean. that's only false or true right? How would you convert it to code? – iDaily Oct 07 '19 at 09:36
  • `"Ja".equalsIgnoreCase(pass)` returns a boolean. That is why you can put it inside an if-statement. So in the final line of your new method you can return that statement. Then instead of `if("Ja".equalsIgnoreCase(pass))` you can simply change it to `if(newMethodName())`. – Ivar Oct 07 '19 at 09:41
  • @Ivar I solved it in the following way. Could you check my code based on the requirements of the project? 1. The application compiles and runs flawlessly 2. At least two primitive types are used correctly in the application. 3. At least 1 String is used correctly in the application. 4. In addition to the main method, the application has at least two other methods that work correctly. 5. At least 1 selection structure is applied correctly in the application. 6. Correct use is made of at least 1 collection that loops through. The code in the answers is how I did it! – iDaily Oct 07 '19 at 11:21
  • @iDaily I'm not what is meant by a "_selection structure_", so I can't answer that one. For point 2, you _are_ using two primitives (`int` and `char`), but `char` in your case is also an array. I'd say that that counts, but I'm not your lecturer. Other than that, it matches all points. – Ivar Oct 07 '19 at 11:37
  • @iDaily Yes, I'm dutch, but since comments here are public and the required language is English, I'll stick with that. :) But yes, I agree that it is very likely that they mean the if-else branch by "keuzestructuur". – Ivar Oct 07 '19 at 12:35

1 Answers1

0

You can send it to a checker method and it will direct it to final method!

public class main {

public static void main(String[] args) {
    Scanner input=new Scanner(System.in);
    System.out.println("Wil je je wachtwoord resetten? Ja of nee");
    String pass=input.next();
    input.close();
    checkPassword(pass);
}

public static void checkPassword(String pass){
    if("Ja".equalsIgnoreCase(pass)) {
        System.out.println(generatePassword(28));
    } else{
        System.out.println("Okee, volgende keer beter!");
    }
}

static char[] generatePassword(int length) {
    System.out.println("Nieuw wachtwoord: ");
    String letters="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvw";
    String nummers="0123456789";
    String raarTeken="!@#$%^&*)(}{[]<>?:;";

    String pwd=letters+nummers+raarTeken;
    Random r=new Random();
    char[] newPass=new char[length];

    for(int i=0; i<length; i++) {
        newPass[i]=pwd.charAt(r.nextInt(pwd.length()));
    }
    return newPass; 
}
}
TheCoder
  • 128
  • 12