How do you compare an Arraylist w/ a scanner

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sandyw
    New Member
    • Mar 2007
    • 122

    How do you compare an Arraylist w/ a scanner

    Hi Everyone.
    Need some help. I have no ideal how to start this.

    my arlist is as followed
    static ArrayList arlist;
    arlist = new ArrayList<ticke t>()

    I do not know where to go after this how do I compare a value with a class method??? ie empid it does not work, it tell me I can not because its static

    Here is what I have so far
    Code:
    String ans;// used temp to hold a value
                System.out.print("Enter name==> ");
                ans = input_flag.next();
    
                // loop through the array
                int pos = 0;
    
                for(String idx :  name) {  
                        if( ans.equalsIgnoreCase(idx)) {
                                break;
    Thanks
    sandy
  • RedSon
    Recognized Expert Expert
    • Jan 2007
    • 4980

    #2
    I'm not sure what you are asking here but anytime that you create your own class and if you want to be able to compare two objects of that class you will need to implement your own comparator. I forget exactly what the syntax is. I think you have to implements Comparator or something. Hopefully someone else will be able to explain it better.

    Comment

    • RedSon
      Recognized Expert Expert
      • Jan 2007
      • 4980

      #3
      Here ya go: http://www.javapractic es.com/Topic10.cjp

      Comment

      • sandyw
        New Member
        • Mar 2007
        • 122

        #4
        Still a little lose.
        What I need to find and learn

        is the use insert a name from a scanner.
        the scanner then makes a new value say "ans".
        in which it goes thou an if else statement.
        ans is compared to a value (ie name) which is not static.
        if it finds the name in the arraylist it will print it
        if not it will tell you to go back to the main menu.

        Code:
        String ans;// used temp to hold a value
                    System.out.print("Enter name==> ");
                    ans = input_flag.next();
        
                    // loop through the array
                    int pos = 0;
        
                    for(String idx :  name) {  
                            if( ans.equalsIgnoreCase(idx)) {
                                    break;

        sandy...

        ps Redson for responding...

        Comment

        • RedSon
          Recognized Expert Expert
          • Jan 2007
          • 4980

          #5
          I'm sorry I don't know what you are asking. Your words above do not correspond to the code that you have outlined below. I won't be able to help you further unless you have more information to give me or unless someone can decipher what you want to know.

          Comment

          • sandyw
            New Member
            • Mar 2007
            • 122

            #6
            OK...
            What I'm trying to do is when a client enter information from a scanner
            ie a name John...
            John is then compared to a value which is in the arraylist.
            if john is in the arraylist then it prints out john information
            if john is not in the arraylist then the client is put back to the menu.

            note I need the same compareto but in a different method to
            if john is not in the arraylist then the client add the rest of the data.
            if john is in the arraylist then it prints out john id number.


            sandy

            Comment

            • RedSon
              Recognized Expert Expert
              • Jan 2007
              • 4980

              #7
              Originally posted by sandyw
              OK...
              What I'm trying to do is when a client enter information from a scanner
              ie a name John...
              John is then compared to a value which is in the arraylist.
              if john is in the arraylist then it prints out john information
              if john is not in the arraylist then the client is put back to the menu.

              note I need the same compareto but in a different method to
              if john is not in the arraylist then the client add the rest of the data.
              if john is in the arraylist then it prints out john id number.


              sandy
              To accomplish this you can checkout ArrayList.conta ins(Object o) and ArrayList.add(E e). Or alternatively you can look at ArrayList.itera tor() and iterate over all the items in the list comparing the string literal "John" with you object's client name for example Client.getFirst Name() if you do not want to implement comparable interface.

              Comment

              • sandyw
                New Member
                • Mar 2007
                • 122

                #8
                Originally posted by RedSon
                To accomplish this you can checkout ArrayList.conta ins(Object o) and ArrayList.add(E e). Or alternatively you can look at ArrayList.itera tor() and iterate over all the items in the list comparing the string literal "John" with you object's client name for example Client.getFirst Name() if you do not want to implement comparable interface.
                I had something like this in the begin...
                by the way is this right???
                I change it but I'm still having troubles...

                Can you PLEASE give me a clue on who to fix this part..
                Two flags come up
                1. one at the
                for (Employee e : Employees) {

                2. the other one at the
                for (Employee entry : arlist)

                Code:
                	public static Employee findEmployee(){
                		Employee myEmployee;
                
                		String id_flag = "";
                		Scanner input_flag = new Scanner(System.in);
                		System.out.println("Enter Employee Id Number AB1234==>");
                		id_flag = input_flag.next();
                		boolean notfound = true;
                		for (Employee e : Employee) {
                			String emp = e.getEmpId();
                			if (emp.equals(id_flag)) {
                				System.out.println("Hello" + ("e.lname"));
                				notfound = false;
                			}
                		}
                		if (notfound == true) {
                			System.out.println("Error - Employee is in the db");
                // back to menu...
                }//close if not found...
                    	System.out.println("The contacts list contains:");
                    	for (Employee entry : arlist)
                    	System.out.println (entry);
                    }// close the else loop
                Thanks again RedSon...
                sandy

                Comment

                • RedSon
                  Recognized Expert Expert
                  • Jan 2007
                  • 4980

                  #9
                  Code:
                  for (Employee e : Employee) {
                  			String emp = e.getEmpId();
                  			if (emp.equals(id_flag)) {
                  				System.out.println("Hello" + ("e.lname"));
                  				notfound = false;
                  			}
                  		}
                  I've never see a for loop like this before. Maybe you are more advanced then me. I would do something like:

                  Code:
                  Iterator iter = ArrayList.iterator();
                  while (iter.hasNext())
                  {
                    Employee e = iter.next();
                    String emp = e.getEmpId();
                    //compare emp to the id that your scanner read in
                    //then do the rest of your code
                  }
                  Remember to replace ArrayList with your class's array list object.

                  Comment

                  • sandyw
                    New Member
                    • Mar 2007
                    • 122

                    #10
                    Originally posted by RedSon
                    Code:
                    for (Employee e : Employee) {
                    			String emp = e.getEmpId();
                    			if (emp.equals(id_flag)) {
                    				System.out.println("Hello" + ("e.lname"));
                    				notfound = false;
                    			}
                    		}
                    I've never see a for loop like this before. Maybe you are more advanced then me. I would do something like:

                    Code:
                    Iterator iter = ArrayList.iterator();
                    while (iter.hasNext())
                    {
                      Employee e = iter.next();
                      String emp = e.getEmpId();
                      //compare emp to the id that your scanner read in
                      //then do the rest of your code
                    }
                    Remember to replace ArrayList with your class's array list object.
                    Ok RedSon
                    I'm getting closer, but I still have some flags
                    two places:
                    1. The method iterator() is undefined for the type Employee
                    Employee.iterat or();)

                    2. Type mismatch: cannot convert from Object to Employee
                    Employee e = iter.next();



                    Code:
                    Iterator iter = Employee.iterator();
                    		while (iter.hasNext())
                    		{
                    		  Employee e = iter.next();
                    Thanks
                    sandy

                    Comment

                    • RedSon
                      Recognized Expert Expert
                      • Jan 2007
                      • 4980

                      #11
                      Iterators only make sense for collections. Is your Employee a collection of employees or just one employee?

                      Comment

                      • sandyw
                        New Member
                        • Mar 2007
                        • 122

                        #12
                        for a collection of employees

                        Comment

                        • RedSon
                          Recognized Expert Expert
                          • Jan 2007
                          • 4980

                          #13
                          So what you really want to do is something like Employees.getCo llectionOfEmplo yees().Iterator ();

                          Comment

                          • sandyw
                            New Member
                            • Mar 2007
                            • 122

                            #14
                            Originally posted by RedSon
                            So what you really want to do is something like Employees.getCo llectionOfEmplo yees().Iterator ();

                            I need to know what you are using this line of code.
                            not sure about what
                            Employees does.
                            I think I know what getCollectionOf Employees() //String value
                            It looks up the arraylist for a value (name)
                            Iterator what does the do... All I know it removes unwanted collections.

                            I have all my object collected by
                            Employee otherEmployee = (Employee) otherObject;

                            I'm just not getting all this Java.
                            I really need to set down with someone and get a one on one hands experience

                            sandy
                            PS do you think this project is for a beginner...
                            Last edited by sandyw; Apr 6 '07, 09:20 PM. Reason: adding info

                            Comment

                            • RedSon
                              Recognized Expert Expert
                              • Jan 2007
                              • 4980

                              #15
                              Its a moderate project not one for a brand new beginner. Tell me what the member data in your Employee class is and I will be able to help you further.

                              Comment

                              Working...