*
The moose likes Beginning Java and the fly likes help looping through objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply locked New topic
Author

help looping through objects

vuthlarhi donald
Ranch Hand

Joined: Jul 31, 2006
Posts: 76
I hav created 10 instances...and I want to loop through all of them while comparing if the username or password is right!
for an example I hava!

Person object1 = new Person("username","password")
...object2
...object3
if((k==object1.password) && (kc.equals(object1.username))
I don't know how to loop through the objects, from object1 til 10 while comparing the username & password then print out the result...this is the program that I hav written..below



import java.util.Scanner;
/**
* Details of a Person.
* @version 1.00 10 Jan 2008
* @author valoyi
*
*/

public class Person {
private String name;
private String surname;
private String address;
private String username;
private String email;
private int password;

/**
* default contractor that initializes all Strings to null
* and Integers to zero.
*/
public Person(){
}
/**
* A contractor to initialize all the parameters.
* @param name
* @param surname
* @param address
* @param username
* @param email
* @param password
*/
public Person(String name,String surname,String address,String username,
String email,int password){
this.name = name;
this.surname = surname;
this.address = address;
this.username = username;
this.email = email;
this.password = password;
}
/**
* get the name.
* @return name.
*/
public String getName(){
return name;
}
/**
* get the name.
* @param name
*/
public void setName(String name){
this.name = name;
}
/**
* get the surname.
* @return surname.
*/
public String getSurname(){
return surname;
}
/**
* set the surname.
* @param surname
*/
public void setSurname(String surname){
this.surname = surname;
}
/**
* get the address.
* @return address.
*/

public String getAddress(){
return address;
}
/**
* set address.
* @param address
*/
public void setAddress(String address){
this.address = address;
}
/**
* get the username.
* @return username
*/
public String getUsername(){
return username;
}
/**
*
* @param username
*/
public void setUsername(String username){
this.username=username;
}
/**
* get the email.
* @return email
*/
public String getEmail(){
return email;
}
/**
* set email.
* @param email
*/
public void setEmail(String email){
this.email = email;
}
/**
* get the password.
* @return password
*/
public int getPassword(){
return password;
}
/**
* set pasword.
* @param password
*/
public void setPassword(int password){
this.password = password;
}

/**
* Overrides the toString method.
* @return all the instances
*/
@Override
public String toString(){
return "Name=" + getName() + "Surname="+ getSurname()
+ "Address=" + getAddress()+ "Username="+ getUsername()
+ "email="+ getEmail() + "password="+getPassword();
}

/**
* main method
* @param args
*/
public static void main(String[] args) {

// Create ten instances of the person class.

Person object1 = new Person("Donald","Funy","Sandton","valoyivd","hello",100);
Person object2 = new Person("Funny","Va","","","",1234);
Person object3 = new Person("hey","vutivi","rhandu","sho","hi",145);
Person object4 = new Person("rgh","hte","tu","then","sweat",1456);
Person object5 = new Person("duke","benni","sweat","folks","",200);
Person object6 = new Person("","","","","",100);
Person object7 = new Person("","","","","",123);
Person object8 = new Person("","folks","funny","hey","",4321);
Person object9 = new Person("","","","sweat","happy",876);
Person object10 = new Person("holla","nike","adidas","yayay","sweat",123);


Scanner kbInteger = new Scanner(System.in);
Scanner kdString = new Scanner(System.in);

//give the user three attempts to enter the right password and username.

for(int j=1 ; j<=3 ; j++)
{
// prompt for password of integers.

System.out.println("Enter password :");
int k = kbInteger.nextInt();

// prompt for username which must be a String.

System.out.println("Enter username");
String kc = kdString.nextLine();

// compare the username and password to see if it is valid.

if((k==object1.password) && (kc.equals(object1.username)) ){

System.out.println("Welcome");
System.out.println(object1); // print all the parameter values in object1.
break; // break out the for loop.
}
// for invalid username or password.

else{
System.out.println("Incorrect password or username");
}}}}
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41572
    
  54
You can make it much easier for us to read and understand the code if you UseCodeTags.

Instead of

you can store the objects in an array:

Then you can iterate through the array in a loop. By the way, you seem to be comparing string using the "==" operator - that will give incorrect results. You should use the equals method instead.


Ping & DNS - my free Android networking tools app
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11240
    
  16

follow-ups here:


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: help looping through objects