Win a copy of Rust Web Development this week in the Other Languages forum!
    Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

help looping through objects

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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");
}}}}
 
Rancher
Posts: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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.
 
lowercase baba
Posts: 13019
66
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
follow-ups here:
 
It sure was nice of your sister to lend us her car. Let's show our appreciation by sharing this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic