Results 1 to 6 of 6
- 03-10-2011, 01:53 AM #1
Member
- Join Date
- Feb 2011
- Posts
- 34
- Rep Power
- 0
Help I need to make this code simpler ASAP!!
Hello,
So I had this assignment to understand and re-write the code below but when I try to change from using the String.Format, reportString and the %d, %s it breaks. I really just want it to be normal scanner and print the same info required. Can anyone help? I know what it needs to look like and all the info is there I just can't figure out how to reformat it. :(
Java Code:import java.util.Scanner; public class Password { private final int MIN_LENGTH = 6; private Scanner input; private String password; private int count; private int data; private int valid; private String reportString; private boolean lengthOK; private boolean lowercaseOK; private boolean uppercaseOK; private boolean digitOK; public Password() { input = new Scanner(System.in); count = 0; data = 0; valid = 0; } public int getNumberOfPasswords() { return count; } public void startPasswordChecker() { System.out.print("Ready to check your passwords? "); System.out.print("\nEnter the number of passwords to check: "); count = input.nextInt(); reportString = "\nHere are your results!\n"; reportString = reportString + String.format("Passwords checked:\n"); } public void inputPassword() { data++; System.out.printf("Enter password #%d: ", data); password = input.next(); reportString += String.format(" %d. %s", data, password); } public void checkLength() { lengthOK = (password.length() >= MIN_LENGTH); } public void checkLowercase() { lowercaseOK = false; for (int i = 0; i < password.length(); i++) { if (Character.isLowerCase(password.charAt(i))) { lowercaseOK = true; } } } public void checkUppercase() { uppercaseOK = false; for (int i = 0; i < password.length(); i++) { if (Character.isUpperCase(password.charAt(i))) { uppercaseOK = true; } } } public void checkDigit() { digitOK = false; for (int i = 0; i < password.length(); i++) { if (Character.isDigit(password.charAt(i))) { digitOK = true; } } } public void reportStat() { if (lengthOK && lowercaseOK && uppercaseOK && digitOK) { reportString += " ** valid password"; valid++; } if (!lengthOK) reportString += " ** too short"; if (!lowercaseOK) reportString += " ** doesn't contain a lowercase letter"; if (!uppercaseOK) reportString += " ** doesn't contain a uppercase letter"; if (!digitOK) reportString += " ** doesn't contain a digit"; reportString += "\n"; } public void reportStatistics() { reportString += "Statistics:\n"; reportString += String.format(" Number of passwords checked: %d\n", count); reportString += String.format(" Number of valid passwords: %d(%.1f%%)\n", valid, (double)(valid*100)/count); reportString += String.format(" Number of invalid passwords: %d(%.1f%%)\n", count - valid, (double)((count - valid)*100)/count); } public void printReport() { System.out.println(reportString); System.out.println("End of report"); } public static void main(String[] args) { // instantiate a Password object Password password = new Password(); // call startPasswordChecker password.startPasswordChecker(); // for loop (all passwords) for (int i = 0; i < password.getNumberOfPasswords(); i++) { // call inputPassword method password.inputPassword(); // call checkLength method password.checkLength(); // call checkLowercase method password.checkLowercase(); // call checkUppercase method password.checkUppercase(); // call checkDigit method password.checkDigit(); // call reportStat method password.reportStat(); } // call reportStatistics method password.reportStatistics(); // call printReport method password.printReport(); } }
- 03-10-2011, 01:58 AM #2
ASAP, sir!
Yes, sir!
Right away, sir!
- 03-10-2011, 02:05 AM #3
Member
- Join Date
- Feb 2011
- Posts
- 34
- Rep Power
- 0
Lol sorry I'm just under a deadline and trying to get everything done.
- 03-10-2011, 02:06 AM #4
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,619
- Rep Power
- 5
Recommend you boil your code down to an Short, Self Contained, Correct Example , as well as in the future refraining from writing things like ASAP in your post title (doing so will not get your question answered faster, and may actually dissuade some from attempting to answer your question as it implies your time is more valuable than theirs - see How To Ask Questions The Smart Way )
- 03-10-2011, 02:09 AM #5
- 03-10-2011, 02:10 AM #6
Member
- Join Date
- Feb 2011
- Posts
- 34
- Rep Power
- 0
Similar Threads
-
How to make EXE from code?
By heyitsmebenjamin in forum New To JavaReplies: 1Last Post: 01-09-2011, 06:37 PM -
Need some help with code. Make it more efficient.
By Meta in forum New To JavaReplies: 1Last Post: 03-22-2010, 08:21 AM -
how to make my code better
By vendetta in forum New To JavaReplies: 4Last Post: 02-10-2010, 09:14 PM -
Code to make an Applet that downloads?
By Leeky in forum Java AppletsReplies: 0Last Post: 09-05-2009, 10:56 PM -
problems with java code! (very new - need help asap!)
By sumkindafreek in forum New To JavaReplies: 1Last Post: 01-07-2009, 05:00 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks