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

Thread: Make it stop looping! please. :)

  1. #1
    Member
    Join Date
    Jan 2011
    Posts
    78
    My Mood
    Confused
    Thanks
    23
    Thanked 1 Time in 1 Post

    Default Make it stop looping! please. :)

    So I'm basically trying to make a part that checks to see if someone is registering a new account is trying to register with a name that's in an arraylist. Which currently has nothing in it. But my problem is, it keeps asking to enter a login over and over again. I thought I made it pretty clear to my computer that as long as its not used it should move on but apparently it doesn't think so o_O?

    // start attempting new login
                            boolean loginClear = false;
                            boolean loginUsed = false;
                            do
                            {
     
                                System.out.print("Login:");
     
                                newLogin = keyBd.next();
     
                                for (int i = 0; i < AAL.accArray.size(); i++)
                                {
     
                                    if (AAL.accArray.get(i).getLogin().equals(
                                            newLogin))
                                    {
                                        loginUsed = true;
                                        System.out.println("Bad login.");
                                    }
                                    else
                                    {
                                        loginClear = true;
                                    }
                                }
                            } while (loginClear == false);


    This is what it prints:

    Please select a menu option.

    1. Login
    2. Register
    2
    Please fill out the folling information:
    Login:John
    Login:Why are you asking me again fool?
    Login:Login:Login:Login:Login:Login:Login:Wtf?
    Login:


  2. #2
    Junior Member TopdeK's Avatar
    Join Date
    May 2011
    Location
    Ireland
    Posts
    21
    My Mood
    Cheerful
    Thanks
    3
    Thanked 4 Times in 4 Posts

    Default Re: Make it stop looping! please. :)

    Correct me if I'm wrong but is it possible that since there is nothing in the ArrayList then the for loop is not executing, hence leaving loginClear to be false??

    i.e if ArrayList.size() returns 0, then the for loop condition would be 0 < 0...which would return false and skip the loop.

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

    Hallowed (May 6th, 2011)

  4. #3
    Member
    Join Date
    Jan 2011
    Posts
    78
    My Mood
    Confused
    Thanks
    23
    Thanked 1 Time in 1 Post

    Default Re: Make it stop looping! please. :)

    =O I think you are right. But I'm not sure because I can't test it. now its giving me more problems. After its suppose to add the person I tell it to print the list and it just says null. =(
    and I try to register another person and it has an error. =/ so I did something wrong, maybe that's why it wasn't working in the first place. maybe you want to look at my code for me and tell me whats wrong? haha.

    import javax.swing.JOptionPane;
    import java.util.*;
     
    public class Main
    {
    	public static void main(String[] args)
    	{
    		Login Login = new Login();
     
    		Login.Login();
    	}
     
    }

    import javax.swing.*;
    import java.util.*;
     
    public class Login
    {
    	public void Login()
    	{
    		// Account Creation Variables.
    		String newLogin = null;
    		String newPassword = null;
    		int newidNumber = 0;
    		String newName = null;
    		String newType = null;
    		String reEnterPassword = null;
     
    		// Login variables
    		boolean running = true;
    		String LoginMenu = "";
    		int response1 = 0;
    		Scanner keyBd = new Scanner(System.in); // scanner
     
    		AAL AAL = new AAL(); // Initialize AAL class.
     
    		// Initialize Account class.
    		Account Account = new Account(newLogin, newPassword, newidNumber,
    				newName, newType);
     
    		String errorMsg = "Bad command.";
     
    		while (running == true)
    		{
    			response1 = 0; // Resets response.
     
    			LoginMenu = "Please select a menu option.\n\n1. Login \n2. Register";
     
    			System.out.println(LoginMenu);
    			try
    			{
    				response1 = keyBd.nextInt();
    			} // end try
    			catch (Exception inputMismatchException)
    			{
    				System.out.println(errorMsg);
    				Login();
    			}
    			// respond to menu options
    			switch (response1)
    			{
     
    			// Login
    			case 1:
    			{
     
    				break;
    			}
     
    				// Register
    			case 2:
    			{
    				// reset info to default.
    				newLogin = "";
    				newPassword = "";
    				newidNumber = 0;
    				newName = "";
    				newType = "";
     
    				System.out.println("Please fill out the folling information:");
     
    				boolean newAccountLoop = true;
    				do
    				{
     
    					try
    					{
    						// start attempting new login
    						boolean loginClear = false;
    						boolean loginUsed = false;
    						do
    						{
     
    							loginClear = false;
    							loginUsed = false;
    							System.out.print("Login:");
     
    							newLogin = keyBd.next();
     
    							if (AAL.accArray.size() == 0)
    							{
    								loginClear = true;
    							}
    							else
    							{
    								for (int i = 0; i < AAL.accArray.size(); i++)
    								{
     
    									if (AAL.accArray.get(i).getLogin().equals(
    											newLogin))
    									{
    										loginUsed = true;
    										System.out.println("Bad login.");
    									}
    									if (loginUsed != true)
    									{
    										loginClear = true;
    									}
    								}
     
    							}
    						} while (loginClear == false);
     
    						// start attempting new password
    						boolean passwordClear = false;
    						do
    						{
    							System.out.print("\nPassword:");
    							newPassword = keyBd.next();
    							System.out.print("\nReEnter Password:");
    							reEnterPassword = keyBd.next();
    							// check to make sure password is the same.
    							if (newPassword.equals(reEnterPassword))
    							{
    								passwordClear = true;
    							}
    						} while (passwordClear == false);
     
    						// start attempting new id number.
    						boolean idNumberUsed = false;
    						boolean idNumberClear = false;
    						do
    						{
    							idNumberUsed = false;
    							idNumberClear = false;
    							System.out
    									.print("\nPlease choose and account number.");
    							System.out.print("\nidNumber:");
    							newidNumber = keyBd.nextInt();
    							if (AAL.accArray.size() == 0)
    							{
    								idNumberClear = true;
    							}
    							else
    							{
    								for (int i = 0; i < AAL.accArray.size(); i++)
    								{
    									// Make sure idNumber is not used already.
    									if (AAL.accArray.get(i).getidNumber() == newidNumber)
    									{
    										idNumberUsed = true;
    										System.out.println("Bad ID Number");
    									}
    									if (idNumberUsed != true)
    									{
    										idNumberClear = true;
    									}
     
    								}
     
    							}
     
    						} while (idNumberClear == false);
     
    						newAccountLoop = false;
    					}// end try
    					catch (Exception inputMismatchException)
    					{
    						System.out.println(errorMsg);
    						Login();
    					}// end catch
    				} while (newAccountLoop == true);
     
    				// Create account object with new values
    				Account newAccount = new Account(newLogin, newPassword,
    						newidNumber, newName, newType);
     
    				// Add the account to accArray
    				AAL.accArray.add(newAccount);
     
    				System.out.printf("Account " + newName + " created successfully.\n");
     
    				break;
    			}
    			case 433:
    			{
    				for (int i = 0; i < AAL.accArray.size(); i++)
    				{
    					System.out.println(AAL.accArray.get(i).getName());
    					System.out.print("" + AAL.accArray.get(i).getName());
     
    				}
    				break;
    			}
     
    			default:
    			{
    				System.out.println(errorMsg);
    				System.out.println("Switch Default.");
    				/*
    				 * String Msg =
    				 * String.format("Sorry that is an invalid option.");
    				 * JOptionPane.showMessageDialog(null, Msg);
    				 */
    				break;
    			}
     
    			}// end switch
     
    		}// end while
    	}
    }

    public class Account
    {
     
    	public String Login;
    	public String Password;
    	public String Name;
    	public int idNumber;
    	public String Type;
     
    	public Account(String newLogin, String newPassword, int newidNumber,
    			String newName, String newType)
    	{
     
    		Login = newLogin;
    		Password = newPassword;
    		idNumber = newidNumber;
    		Name = newName;
    		Type = newType;
    	} // end Account
     
    	public String toString()
    	{
    		return Name;
    	} // end method toString
     
    	public void setName(String changedName)
    	{
    		Name = changedName;
    	}// end setName
     
    	public void setPassword(String changedPassword)
    	{
    		Password = changedPassword;
    	}// end setPassword
     
    	public void setType(String changedType)
    	{
    		Type = changedType;
    	}// end setType
     
    	public int getidNumber()
    	{
    		return idNumber;
    	}// end getidNumber
     
    	public String getType()
    	{
    		return Type;
    	}// end getType
     
    	public String getName()
    	{
    		return Name;
    	}// end getName
     
    	public String getLogin()
    	{
    		return Login;
    	}
    }// end class

    import java.util.ArrayList;
     
     
    public class AAL
    {
    	public ArrayList<Account> accArray = new ArrayList();
    }

    That's a fair amount of code to go through I know but if you do thanks. I've never tried putting an arraylist in its own class like that before though. There should be nothing wrong with it right?
    Last edited by Hallowed; May 7th, 2011 at 02:47 AM.

  5. #4
    Junior Member TopdeK's Avatar
    Join Date
    May 2011
    Location
    Ireland
    Posts
    21
    My Mood
    Cheerful
    Thanks
    3
    Thanked 4 Times in 4 Posts

    Default Re: Make it stop looping! please. :)

    I'll have a look but can you do something for me please. Its much easier to read your code with the highlighter on. Edit your [CODE] brackets to be [highlight]. In the first one say "highlight=java" Thanks!

  6. #5
    Member
    Join Date
    Jan 2011
    Posts
    78
    My Mood
    Confused
    Thanks
    23
    Thanked 1 Time in 1 Post

    Default Re: Make it stop looping! please. :)

    There we go.

  7. #6
    Junior Member TopdeK's Avatar
    Join Date
    May 2011
    Location
    Ireland
    Posts
    21
    My Mood
    Cheerful
    Thanks
    3
    Thanked 4 Times in 4 Posts

    Default Re: Make it stop looping! please. :)

    At a glance, I think the reason your list is producing null is because you ARE adding a null! The "Account" is constructed outside the main while loop and no other "Account" is created for the new login, nor are the input values assigned the the previously created "Account". So when you go to add to the list, you are adding the same null account over and over. Before you add to the list and after receiving all the users details:
    //Create account object with new values
    Account temp = new Account(newLogin, newPassword, newidNumber,
    			newName,  newType);
     
    //Add to list
    AAL.accArray.add(temp);

  8. The Following User Says Thank You to TopdeK For This Useful Post:

    Hallowed (May 7th, 2011)

  9. #7
    Member
    Join Date
    Jan 2011
    Posts
    78
    My Mood
    Confused
    Thanks
    23
    Thanked 1 Time in 1 Post

    Default Re: Make it stop looping! please. :)

    Wow. Can't believe I left that out. Definitely fixed it though. Thanks so much for your help.
    Although this still wont print anything o_O

    case 433:
    			{
    				for (int i = 0; i < AAL.accArray.size(); i++)
    				{
    					System.out.println(AAL.accArray.get(i));
    				}
    				break;
    			}

    it should print the to string which returns the name.. right?
    It does check the names correctly when I try to add 2 of the same name.

  10. #8
    Junior Member TopdeK's Avatar
    Join Date
    May 2011
    Location
    Ireland
    Posts
    21
    My Mood
    Cheerful
    Thanks
    3
    Thanked 4 Times in 4 Posts

    Default Re: Make it stop looping! please. :)

    When you type in 433 as your response, what happens?

    EDIT: Actually, if I remember right toString() is only auto-called by the compiler during string concatenation (i.e object + " "). So try this:
    case 433:
    			{
    				for (int i = 0; i < AAL.accArray.size(); i++)
    				{
    					System.out.println(AAL.accArray.get(i).toString());
    				}
    				break;
    			}
    If that doesn't work (it should...but I'm exhausted and should be asleep), try creating a temp account object with no args. I noticed you didn't have a default no-args constructor in your account class. Perhaps add one. Then just assign the temp the account at index i and call temp.toString();
    Last edited by TopdeK; May 6th, 2011 at 08:11 PM.

  11. #9
    Member
    Join Date
    Jan 2011
    Posts
    78
    My Mood
    Confused
    Thanks
    23
    Thanked 1 Time in 1 Post

    Default Re: Make it stop looping! please. :)

    Hmm it still doesn't print oddly. I even tried using the .getName() method I have and it refuses to print out the information. Its weird cause I got it to work on another program I made before. =/
    Even more weird because I can get it to check using .equals() but it wont print it.


    oh and as for what it does, it just prints blank spaces instead.

  12. #10
    Junior Member TopdeK's Avatar
    Join Date
    May 2011
    Location
    Ireland
    Posts
    21
    My Mood
    Cheerful
    Thanks
    3
    Thanked 4 Times in 4 Posts

    Default Re: Make it stop looping! please. :)

    Your code keeps failing to print because you are asking it to print blanks. Nowhere in the code does it ask the user for his/her actual name. Just the login name. So I added this piece of code just at the very start of the try block and it works fine:
    						//Get users registration name
    						System.out.print("Please enter your name: ");
    						newName = keyBd.next();
                                                    System.out.println("");

    Now when you go to call the getName() method it has something to print out.

    Or if you'd prefer to print out the login credentials instead of the users actual name, in your for-loop, change to the following:
    case 433:
    			{
    				for (int i = 0; i < AAL.accArray.size(); i++)
    				{
    					System.out.println(AAL.accArray.get(i).getLogin());
    				}
    				break;
    Last edited by TopdeK; May 7th, 2011 at 04:22 AM. Reason: Mistake in provided code...and added additional solution.

  13. The Following User Says Thank You to TopdeK For This Useful Post:

    Hallowed (May 7th, 2011)

  14. #11
    Member
    Join Date
    Jan 2011
    Posts
    78
    My Mood
    Confused
    Thanks
    23
    Thanked 1 Time in 1 Post

    Default Re: Make it stop looping! please. :)

    Wow, I am having serious problems lately o.o that should have been obvious. I forgot that I was going to get that information after.
    Thanks so much for your help again.

  15. #12
    Junior Member TopdeK's Avatar
    Join Date
    May 2011
    Location
    Ireland
    Posts
    21
    My Mood
    Cheerful
    Thanks
    3
    Thanked 4 Times in 4 Posts

    Default Re: Make it stop looping! please. :)

    No problem at all. I'm always making simple mistakes and I know its always something small that I've overlooked. Outside perspective is a life saver! Besides, I'm still only a beginner really when it comes to Java but I enjoy the challenge of fixing problematic code

    Glad to have been of some help !
    while(true)
    {
            codeInJava();
    }

Similar Threads

  1. Need Help Getting A Simple Stop-Watch Working?
    By logmein in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 2nd, 2011, 07:57 AM
  2. Button Won't Stop Animation.
    By SyntheticD in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 24th, 2011, 02:27 PM
  3. Cant get my code to stop jumping
    By Mob31 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 2nd, 2011, 07:03 AM
  4. paying off dept ( can't make it stop at 0 )
    By bossmeister in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 16th, 2010, 07:33 AM
  5. Code Stop working after converting to jar?
    By Ron6566 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 16th, 2010, 12:17 PM