Results 1 to 10 of 10
Thread: Understand Code
- 03-07-2011, 04:24 PM #1
Member
- Join Date
- Feb 2011
- Posts
- 34
- Rep Power
- 0
Understand Code
My HW assignment is to read and understand the code below. I pretty much understand the whole thing except for a few things.
1-How is the author able to use decimal formater with out it being declaired?
2-What do the %d and %s mean? When %d first appears I see it is for the number 1,2,3,....etc to show after "Enter Password #1,2,3...etc" but how is that declaired and why and what is %s and how it is declaired and why?
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("\n1Enter the number of passwords to check: "); count = input.nextInt(); reportString = "\nHere are your results!\n"; reportString += String.format("Passwords checked:\n"); } public void inputPassword() { data++; System.out.printf("Enter password #%d: ", data); password = input.next(); reportString += String.format(" [COLOR="lime"]%d. %s[/COLOR]", 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; break; } } } public void checkUppercase() { uppercaseOK = false; for (int i = 0; i < password.length(); i++) { if (Character.isUpperCase(password.charAt(i))) { uppercaseOK = true; break; } } } public void checkDigit() { digitOK = false; for (int i = 0; i < password.length(); i++) { if (Character.isDigit(password.charAt(i))) { digitOK = true; break; } } } 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: [COLOR="Lime"]%d[/COLOR]\n", count); reportString += String.format(" Number of valid passwords: [COLOR="lime"]%d[/COLOR](%.1f%%)\n", valid, (double)(valid*100)/count); reportString += String.format(" Number of invalid passwords: [COLOR="lime"]%d[/COLOR](%.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(); } }[LIST=1][/LIST]
Thanks
- 03-07-2011, 04:31 PM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
I don't see where he is using a decimal formatter without declaring it, paste just the part where you see this problem.
Check out:
Formatter (Java 2 Platform SE 5.0)
You can also look into String.format.
- 03-07-2011, 05:28 PM #3
Member
- Join Date
- Feb 2011
- Posts
- 34
- Rep Power
- 0
%d(%.1f%%)
When this prints out it looks something like this:
Percentage of Vaild Password:1 (50%)
Percentage of Invaild Password:1 (50%)
How does he do that?
- 03-07-2011, 05:44 PM #4
Member
- Join Date
- Feb 2011
- Posts
- 34
- Rep Power
- 0
Ok so I see he used the String.format that makes sense. Is that what the %d is as well?
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);
- 03-07-2011, 06:39 PM #5
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
yup, the link I used explains it pretty in depth, but there are special characters that can be used in String.format and System.out.printf().
for example if you see %d, it needs an int argument and it puts that int argument in the string. The use of String.format and printf can be very challenging to learn but they can offer a lot of power in how your strings look.
here are some examples of using printf in different ways
System.out.printf*«*java.lang*«*Java by API
but the general format is
where it says %d you swap the 20 for the %d, so it would sayJava Code:System.out.printf("Hello there, there is %d days left", 20);
You could get more complex tooJava Code:Hello there, there is 20 days left
This isn't very complex but it should print outJava Code:System.out.printf("hello there, there are %d days in this %d day month", 20, 21);
there is an appropriate item for each type in java(bool, float, double, etc)Java Code:hello there, there are 20 days left in this 21 day month
If you have a textbook or a decent java book check the index and see if you can find anything on this. It may also be in the Strings chapter. Thinking in Java has a pretty good bit on the use of this.
- 03-07-2011, 07:58 PM #6
Member
- Join Date
- Feb 2011
- Posts
- 34
- Rep Power
- 0
Ok. Can you give me an easy way to do this? My assignment is to re-write the code. I will speak with my teacher about using the methods above we just have not learned them yet so I don't want to turn in something we have not went over.
- 03-07-2011, 08:43 PM #7
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
What's the assignment? I thought you just needed to understand the code.
- 03-07-2011, 09:40 PM #8
Member
- Join Date
- Feb 2011
- Posts
- 34
- Rep Power
- 0
The assignment to read and understand the code and to re-write what you didn't understand in another way.
- 03-07-2011, 09:57 PM #9
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Oh alright, since you didn't understand it that way you would have to hardcode
BecomesJava Code:System.out.printf("hi %d", someVal);
Java Code:System.out.println("hi " + someVal);
- 03-07-2011, 10:07 PM #10
Member
- Join Date
- Feb 2011
- Posts
- 34
- Rep Power
- 0
Similar Threads
-
need to understand code
By Masken2 in forum New To JavaReplies: 2Last Post: 02-17-2011, 04:21 PM -
understand the code
By prof.deedee in forum New To JavaReplies: 8Last Post: 11-11-2009, 02:43 AM -
help to understand the ? mark in this code
By carolain79@hotmail.com in forum New To JavaReplies: 2Last Post: 10-13-2009, 06:57 AM -
Trying to understand this code
By new2java2009 in forum New To JavaReplies: 2Last Post: 09-09-2009, 07:18 PM -
I don´t understand
By Manikyr in forum New To JavaReplies: 6Last Post: 02-22-2009, 11:22 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks