Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Simple beginner's password guessing program

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Simple beginner's password guessing program

    Hello all, I'm new to java and new to forum. It's great to see all the activity here and active posts. Now on to business.
    I'm teaching myself programming using an online cs tutorial. I can work through most of the programming, but occasionally I get stuck and I can't find the bug.

    Here is a program I'm trying to write:
    Write a program that asks the user for a password. The user enters a password (which must be short). Now pretend that this password is a secret from the rest of the program, which must try to guess it. The program then uses the password generator repeatedly until a randomly generated password matches the password the user entered. Assume that the user's password is five characters or less (otherwise this program will never end). Write out the number attempts it took to find the password. Here are some example runs:

    K:\cai\>java PasswordCracker
    Enter a "secret" password-->ant
    Here is your password: ant
    It took 2181892 tries to guess it


    I've learned up to if, else, while loops, and random.

    Here is the code I've written:

    import java.util.*;
    import java.io.*;
    class pwcracker
    {
    	public static void main (String[] args)
    	{
    		Scanner scan = new Scanner( System.in );
    		Random rand = new Random();
     
    		String pw, choices, guess;
    		long tries;
    		int j, length;
     
    		System.out.println("Enter a password that is less than 5 chars and contains no numbers: ");
    		pw = "" + scan.nextLine();
    		length = pw.length();
     
    		choices = "abcdefghijklmnopqrstuvwxyz";
    		tries = 0;
    		guess = "";
     
    		System.out.println("Your pw is: " + pw);
    		System.out.println("The length of your pw is: " + length);
     
    		while ( guess != pw )
    		{
    			j = 0;
    			guess = "";
     
    			while ( j < length )
    			{
    				guess = guess + choices.charAt( rand.nextInt ( choices.length() ) );
    				j = j + 1;
    			}
    			System.out.println("Guess: " + guess);		//this is for me to view the actual guesses
    			tries = tries + 1;						
    		}
    		System.out.println("Here is your password: " + guess);
    		System.out.println("It took " + tries + " tries to guess it.");
    	}
    }

    I've run my program many times, but no matter what I do, the program ceases to see that at some point "guess == pw". What I mean is that I've tried just typing in one letter, say the letter "a". The inner while loop guesses the string "a", but it skips past it as if nothing has happened.

    Edit: My program just continues forever, it fails to recognize when the while loop ends.
    Last edited by edishuman; July 11th, 2011 at 06:05 PM. Reason: Solved.


  2. #2
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Simple beginner's password guessing program

    You're not comparing the guess and the password correctly. To compare object contents you should use the equals(..) method, not the == or != operator. The operators compare object identity, i.e. whether it's the same object, not the object contents. You can use the '!' operator in front of the equals(..) method to negate it.

  3. #3
    Junior Member
    Join Date
    Jul 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Simple beginner's password guessing program

    Thanks for the help, I got it to work. I'll read more on equals().

  4. #4
    Junior Member
    Join Date
    Jul 2011
    Posts
    4
    My Mood
    Lurking
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Simple beginner's password guessing program

    pw = "" + scan.nextLine();

    You could have skipped the redundent part:

    "" +

    Scanner (Java 2 Platform SE 5.0)

Similar Threads

  1. Password Program
    By computercoder in forum What's Wrong With My Code?
    Replies: 10
    Last Post: October 20th, 2010, 07:03 AM
  2. Help with a beginner's java assignment: Survey results
    By lavloki in forum Java Theory & Questions
    Replies: 17
    Last Post: October 14th, 2010, 09:08 AM
  3. beginner's question
    By bardd in forum Java Theory & Questions
    Replies: 5
    Last Post: September 14th, 2010, 04:02 PM
  4. urgent simple help for a very simple program
    By albukhari87 in forum Java Applets
    Replies: 4
    Last Post: June 5th, 2010, 03:43 PM
  5. simple program
    By Memb in forum Paid Java Projects
    Replies: 0
    Last Post: March 17th, 2010, 01:47 PM