Results 1 to 10 of 10
  1. #1
    Reiyn is offline Member
    Join Date
    Sep 2008
    Posts
    29
    Rep Power
    0

    Default arraylist trouble

    Heres my code, I am writing a login emulator for class. I either get "array expected, arraylist found" or the darn thing can't find my variables and wants another While....thanks for the help!
    Java Code:
    import javax.swing.*;
    import java.util.ArrayList;
    public class UserLogin extends Main
    {
        int i = 0;
        boolean found = false;
        private String sInputName;
        private String sPassword;
        private String sTempName;
        private String sTempPassword;
        java.util.ArrayList UserNameList = new java.util.ArrayList();
        {
            UserNameList.add("Kevin Schuth");
        }
        java.util.ArrayList UserPasswordList = new java.util.ArrayList();
        {
            UserPasswordList.add("didit");
        }
        public void CreateLogin()
        {
            
            sInputName = JOptionPane.showInputDialog("Please enter a user name");
            UserNameList.add(sInputName);
                    
            sPassword = JOptionPane.showInputDialog("Please enter a password");
            UserNameList.add(sPassword);
            CheckLogin();
        }
        public void CheckLogin()
        {       
            //for (int iCount = 0; iCount < 3; iCount++)
            
            do
            {        
                sTempName = JOptionPane.showInputDialog("Please enter your user name");   
            
                while (!found && i < UserNameList.size())
                {
                    if (UserNameList[i].equals (sTempName))
                    {
                        found = true;
                        CheckPassword();
                    }
                                 
                }
                JOptionPane.showMesageDialog(null, "Please retry");
            }while (iCount <3 && !found);
            }    
            
        
    
        public void CheckPassword()
        {
            //for (int iCount = 0; iCount < 3; iCount++)
            
            do
            {
                sTempPassword = JOptionPane.showInputDialog("Please enter your password");
            
                while (!found && i < UserPasswordList.size())
                
                    if (UserPasswordList[i].equals (sTempPassword))
                    {
                    found = true;
                    JOptionPane.showMessageDialog(null, "Thank you");
                }
                else 
                {
                JOptionPane.showMessageDialog(null, "Incorrect password, please retry", JOptionpane.INFORMATION_MESSAGE);
            }while (iCount <3 && !found);
           
        }
    }
    Yes the methods do look different, i was trying different things in each to see what may work best..don't ask why I dun know either, i am frustrated lol!!!
    Last edited by Reiyn; 05-19-2009 at 12:18 AM. Reason: comment

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    One problem: an ArrayList is not an array and cannot be used like an array. Thus if you have this ArrayList:
    Java Code:
    ArrayList<String> UserNameList = new ArrayList<String>();
    This will cause the compiler to complain:
    Java Code:
    if (UserNameList[i].equals("Foo"))
    {
      ...
    Instead, you must use the arraylist method "get" to get the items out such:
    Java Code:
    if (UserNameList.get(i).equals("Foo"))
    {
      ...
    2. You appear to be mixing for loops and while loops in strange ways. If you want to use a loop that uses an int index, go with a for loop and avoid using a global int as your index.

    3. Spelling and capitalization matters. For instance
    Java Code:
    JOptionPane.showMesageDialog(null, "Please retry");
    won't work and never well. It's showMessageDialog.

    Good luck and HTH.

  3. #3
    Reiyn is offline Member
    Join Date
    Sep 2008
    Posts
    29
    Rep Power
    0

    Default

    Thank you, I see I haven't researched array lists far enough, I saw them as a way to implement an array without a set size and mistakingly thought they would be much the same otherwise...the project doesn't require them, i want to use it to push myself further, I since redid it to conform to the requirements (just using two arrays, and then some further for a higher percentage of the available grade, just having it run using two arrays is 80%...I feel a need to go further though - ya, I'm wierd like that)
    Yes, I caught that misspelling in message lol

  4. #4
    Reiyn is offline Member
    Join Date
    Sep 2008
    Posts
    29
    Rep Power
    0

    Default

    So, I reworked this as an array for the assignment... can anyone help with the loop only going through the array one time? If you put in an incorrect name, it asks for name again, but doesnt loop through the array. We haven't been able to see what is causing that to happen (or not happen as it is lol)

    We have moved things around, the for loop is not really needed with the way we did the while that encases the methods...thought I'd try it and see if it would reset i to 0 so it would loop through the array again..but nooo
    thanks

    Java Code:
    import javax.swing.*;
    
    public class Login extends Main
    {
        
        boolean found = false;
        int i = 0;
        int iCount = 0;
        private String sUserNameResult = "UserName not found";
        private String sPasswordResult = "Password not found";
        private String[] UserName = {"Kevin Schuth", "Christina Abler", "Reiyn", "Jim Hatlak"};
        private String[] Password = {"didit", "sunshine", "mage", "theman"};
        
        public void Login() 
        {
            for (iCount = 0; iCount  < 3 ; iCount++)
            { 
            do
            {
                String sTempName = JOptionPane.showInputDialog("Please enter your name:");
            
                while (!found && i < UserName.length) 
                { 
                    System.out.println(UserName[i]);
                    if (sTempName.equals(UserName[i]))
                        {
                            found = true; 
                            sUserNameResult = "User name is correct";
                            Password();
                        }
                    else 
                    {
                        
                        continue;
                    }
                    i = i+1;
                }            
            //iCount = iCount + 1;
            } while (iCount < 3 && !found);
             
            JOptionPane.showMessageDialog(null, sUserNameResult,  "User Name Results", JOptionPane.INFORMATION_MESSAGE);
            }
        }
        public void Password()
        {
            found = false;
            i = 0;
            for (iCount = 0; iCount <3 && i < Password.length; iCount++)
            {
            do
            {
                String sTempPassword = JOptionPane.showInputDialog("Please enter your password:");
            
                while (!found && i < Password.length) 
                { 
                    System.out.println(Password[i]);
                    if (sTempPassword.equals(Password[i]))
                        {
                            found = true; 
                            iCount = 3;
                            sPasswordResult = "User name and Password is correct, thank you";
                        }
                        
                    i = i +1;
                }            
              //iCount = iCount + 1;
            } while (iCount != Password.length && !found);
             
            JOptionPane.showMessageDialog(null, sPasswordResult,  "Password Results", JOptionPane.INFORMATION_MESSAGE);
        }
        }
    }
    Thanks!

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Why do you have continue; in your else block? Do you know what it does and what its purpose is? As an aside, I've rarely seen it used, and for good reason.

  6. #6
    Reiyn is offline Member
    Join Date
    Sep 2008
    Posts
    29
    Rep Power
    0

    Default

    My understanding is that it will go through the loop from that point again...it didn't have the intended effect, and was removed shortly after posting.

    Fubarable, I must be able to check an array more than once, my text book does not show this, however, the for loop doesn't work, I think because i reaches UserName.length, how would I go about resetting i to 0 without getting it stuck there? Either i stays at 0 or it loops continuously at Please enter your username! I am stumped.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Can you load your most recent code? I'd like to see what you are currently working with.

  8. #8
    Reiyn is offline Member
    Join Date
    Sep 2008
    Posts
    29
    Rep Power
    0

    Default

    Sure, I have the Password method working properly now, it checks the (i) address to match password with UserName, and if incorrect it asks and checks again. The UserName still will only go through the array once however. Here is the latest:
    Java Code:
    import javax.swing.*;
    
    public class Login extends Main
    {
        
        boolean found = false;
        int i = 0;
        int iCount = 0;
        private String sPasswordResult = "Password not found";
        private String[] UserName = {"Kevin Schuth", "Christina Abler", "Reiyn", "Jim Hatlak"};
        private String[] Password = {"didit", "sunshine", "mage", "theman"};
    
        public void Login() 
        {
            do
            {
                String sTempName = JOptionPane.showInputDialog("Please enter your name:");
            
                while (!found && i < UserName.length) 
                { 
                    System.out.println(UserName[i]);
                    if (sTempName.equals(UserName[i]))
                        {
                            found = true; 
                            
                            Password();
                        }
                    else
                    {
                        i = i+1;
                        iCount = iCount + 1;
                    }
                        
                }            
            } while (iCount < 3);
        }
        public void Password()
        {
            found = false;
          
            do
            {
                String sTempPassword = JOptionPane.showInputDialog("Please enter your password:");
            
                while (!found) 
                { 
                    System.out.println(Password[i]);
                    if (sTempPassword.equals(Password[i]))
                        {
                            found = true; 
                            iCount = 3;
                            sPasswordResult = "User name and Password is correct, thank you";
                        }
                    else 
                    {
                        break;
                    }
                }            
              iCount = iCount + 1;
            } while (iCount <3);
             
            JOptionPane.showMessageDialog(null, sPasswordResult,  "Password Results", JOptionPane.INFORMATION_MESSAGE);
        }
    }
    Thanks

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    You need to pretend that you are the computer and you've been given the job to run your code, then walk through your code step by step analyzing the results of each line of code and how the input will effect this.

    If you do this diligently and correctly, you'll see that you may want to change the location of where you increment iCount and why, and what happens to int i each time the loop loops again. Many will advise you to declare your variables in the most local scope possible, and your variable "i" is a good reason for this.

    In short, your problems I think stem mainly from "i" and iCount and how you handle them. Walk through your code with a careful eye on these two variables and you'll solve your problem.

  10. #10
    Reiyn is offline Member
    Join Date
    Sep 2008
    Posts
    29
    Rep Power
    0

    Default

    hmmm ok I will look at them, thank you.
    I also am doing this in a scanner class, with similar issues, so I will look at those as well before posting it here.

Similar Threads

  1. Here comes trouble... :-)
    By sargehendricks in forum Introductions
    Replies: 1
    Last Post: 04-23-2009, 04:18 PM
  2. trouble in removing a value
    By jacline in forum New To Java
    Replies: 5
    Last Post: 03-20-2009, 06:56 PM
  3. Trouble with ArrayList
    By John D. in forum New To Java
    Replies: 4
    Last Post: 03-18-2009, 08:43 PM
  4. having some trouble
    By Unknown1369 in forum New To Java
    Replies: 13
    Last Post: 07-22-2008, 12:52 AM
  5. Replies: 2
    Last Post: 04-21-2008, 12:43 PM

Posting Permissions

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