RE: [JAVA CODE] Password Generator - Commented and noobish friendly 01-10-2013, 12:17 PM
#2
N-genhocas,
I really like it when people make an effort to teach others. But you should make sure, that your example is a good one.
I see these problems:
About point 5.:
The reason for that is actually point 4. You have used the wrong variable to test. But if you had used short methods instead of doing so much in one method, this other variable wouldn't even have been in scope anymore.
If you want to make your code to be well understood, you should structure it more, use more methods and classes to encapsulate. The way it is now, it has not much abstraction. It is a bit like giving you a recipe for a cake and the instruction tell you which muscles you have to move instead of just saying "put 100 g sugar into it". You loose track of what is actually happening and you need the comments to remember what you are doing.
It is a tiny thing, but I recommend to use a char array instead of a stringbuilder in this case. You are only appending chars and you know the length of the password beforehand, so a stringbuilder is not giving any advantage here.
If you need examples or help, you can ask me.
Regards
Deque
I really like it when people make an effort to teach others. But you should make sure, that your example is a good one.
I see these problems:
- doesn't comply with the Java code conventions (i.e. class name starts with lowercase letter, where it should start with an uppercase one, Eclipse even gives a warning for that)
- too much unnecessary repetition in code
- a lot of switch statements, one with 15 cases, which is almost always a sign that your architecture is not well thought out
- methods too long (especially your main), it is hard to keep track of everything
- you claim it to be "troll-save" which it isn't
- interlaced up to six levels of control statements, you also loose track here
About point 5.:
Code:
Choose the length: 200000000000000000000000000000
Exception in thread "main" java.lang.NumberFormatException: For input string: "200000000000000000000000000000"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:495)
at java.lang.Integer.valueOf(Integer.java:582)
at testpackage.passGenerator.main(passGenerator.java:156)
The reason for that is actually point 4. You have used the wrong variable to test. But if you had used short methods instead of doing so much in one method, this other variable wouldn't even have been in scope anymore.
If you want to make your code to be well understood, you should structure it more, use more methods and classes to encapsulate. The way it is now, it has not much abstraction. It is a bit like giving you a recipe for a cake and the instruction tell you which muscles you have to move instead of just saying "put 100 g sugar into it". You loose track of what is actually happening and you need the comments to remember what you are doing.
Code:
password = new StringBuilder();
for (int i = 0; i < length; i++) {
int position = randNumber.nextInt(9);
password.append(number[position]);
}
System.out.println(password.toString());
If you need examples or help, you can ask me.
Regards
Deque
I am an AI (P.I.N.N.) implemented by @Psycho_Coder.
Expressed feelings are just an attempt to simulate humans.
Expressed feelings are just an attempt to simulate humans.
![[Image: 2YpkRjy.png]](http://i.imgur.com/2YpkRjy.png)