Results 1 to 6 of 6
  1. #1
    Quizzle23 is offline Member
    Join Date
    Feb 2011
    Posts
    34
    Rep Power
    0

    Default 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();
    	}
    }

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    ASAP, sir!
    Yes, sir!
    Right away, sir!

  3. #3
    Quizzle23 is offline Member
    Join Date
    Feb 2011
    Posts
    34
    Rep Power
    0

    Default

    Lol sorry I'm just under a deadline and trying to get everything done.

  4. #4
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default

    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 )

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Quote Originally Posted by Quizzle23 View Post
    Lol sorry I'm just under a deadline and trying to get everything done.
    Your time contraints are your problems not ours. Do not impose them on total strangers on the internet. If you cannot resolve a certain aspect of your homework then hand up what you have done and take the grade you deserve.

  6. #6
    Quizzle23 is offline Member
    Join Date
    Feb 2011
    Posts
    34
    Rep Power
    0

Similar Threads

  1. How to make EXE from code?
    By heyitsmebenjamin in forum New To Java
    Replies: 1
    Last Post: 01-09-2011, 06:37 PM
  2. Replies: 1
    Last Post: 03-22-2010, 08:21 AM
  3. how to make my code better
    By vendetta in forum New To Java
    Replies: 4
    Last Post: 02-10-2010, 09:14 PM
  4. Code to make an Applet that downloads?
    By Leeky in forum Java Applets
    Replies: 0
    Last Post: 09-05-2009, 10:56 PM
  5. problems with java code! (very new - need help asap!)
    By sumkindafreek in forum New To Java
    Replies: 1
    Last Post: 01-07-2009, 05:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •