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 6 of 6

Thread: Problems with Code?

  1. #1
    Junior Member Sean137's Avatar
    Join Date
    Dec 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problems with Code?

    import java.util.*;
    import java.lang.*;
    /**
     * To output whether a students chosen password is valid or not.
     * 
     * @author (Sean McGlone
     * @version (December 13, 2010)
     */
    class SemesterProject
    {
        public static void main(String[]args)throws Exception
        {
            Scanner scanReader = new Scanner(System.in);
            String password = "";
            boolean firstLoop = true;
            int length = 0;
            int passwordLength = 0;
            int countCaps = 0;
                System.out.println("This was made by Sean McGlone");
                System.out.print("Please enter a password consisting of 12 characters to be checked for validity:  ");
                    password = scanReader.nextLine ();
                    passwordLength = password.length ();
            do
            {
                try
                {
                    if (passwordLength == 12)
                    { 
                        System.out.println("Your pasword meets the length requirements");
                    }
                        else
                        {
                            throw new Exception ();
                        }
                }
                    catch (Exception ex)
                {
                    System.out.println("Please enter a valid password consisting of 12 characters:  ");
                    password = scanReader.nextLine ();
                }
            }
            while (firstLoop = true);
        }
    }

    When I run this code the loop keeps reiterating even if a 12 character word is input. The loop keeps saying to please reenter a valid password. Can someone please help?

    Thanks
    Sean


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Problems with Code?

    Hint: == != =

    And even when you fix that, when do you set firstLoop to false?

  3. The Following User Says Thank You to KevinWorkman For This Useful Post:

    javapenguin (December 14th, 2010)

  4. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Problems with Code?

    It looks like firstloop is never set to false.

    Great way for an infinite loop.

  5. #4
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Problems with Code?

    Also, it's

    while (firstLoop == true)

    anyway.

  6. #5
    Member DavidFongs's Avatar
    Join Date
    Oct 2010
    Location
    Minneapolis, MN
    Posts
    107
    Thanks
    1
    Thanked 45 Times in 41 Posts

    Default Re: Problems with Code?

    it should be while(firstLoop){} because that helps avoid the assignment error that is present.

  7. The Following User Says Thank You to DavidFongs For This Useful Post:

    javapenguin (December 14th, 2010)

  8. #6
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Problems with Code?

    How about this:
    boolean firstLoop = false;
               while (firstLoop != true)
    {
                try
                {
    password = scanReader.nextLine ();
                    passwordLength = password.length ();
                    if (passwordLength == 12)
                    {
                        System.out.println("Your pasword meets the length requirements");
    firstLoop = true;
                    }
                        else
                        {
                            throw new Exception ();
    firstLoop = false;
                        }
                }
                    catch (Exception ex)
                {
                    System.out.println("Please enter a valid password consisting of 12 characters:  ");
     
                }
            }

Similar Threads

  1. 2 problems...
    By Day2Day in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 22nd, 2010, 02:51 PM
  2. [SOLVED] Have a few odd problems.
    By javapenguin in forum What's Wrong With My Code?
    Replies: 18
    Last Post: October 19th, 2010, 06:08 PM
  3. rmi problems
    By deepthought in forum Java SE APIs
    Replies: 7
    Last Post: September 7th, 2010, 07:25 PM
  4. How to compile and run java program?
    By ebalari56 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 27th, 2009, 09:33 PM
  5. If you have any .NET problems
    By antony_t in forum The Cafe
    Replies: 1
    Last Post: August 26th, 2009, 10:49 AM