I am currently using java and still haven't got the hang of everything so bear with me. I have been trying to get my names to appear on the output menu but I must be missing something in my code.
Here is my code it has a driver class and client class:
import java.util.Scanner;
//This is TestBaby.java class
public class TestBaby {
// This is main() method, program execution start from here
public static void main(String[] args) {
// This is Scanner class object, it will help to take value from end user
Scanner scan = new Scanner(System.in);
//This is array object of Baby type
Baby[] babies = new Baby[4];
//This is for loop, it iterates 4 times for holding 4 Babies data
for(int i=0; i<4; i++) {
//a. Enter details for each baby (name and age) and thus populate the Baby array
System.out.println("Enter details of " + "Baby " +(i+1));
if(i!=0)
scan.nextLine();
System.out.println("Enter name: ");
String name = scan.nextLine();
System.out.println("Enter age: ");
int age = scan.nextInt();
//This is Baby class object*/
Baby baby = new Baby();
//Set name in Baby class object whatever user entered
baby.setName(name);
//Set age in Baby class object whatever user entered
baby.setAge(age);
//Store current Baby class object into babies array
babies[i] = baby;
}//for end
//b. Output the details of each baby from the array (name and age)
int i=0;
for(Baby baby : babies) {
System.out.println("Baby name: " + baby.getName());
System.out.println("Baby age: " + baby.getAge());
i++;
}
//c. Calculate and display the average age of all babies in the array
double averageAge = averageAge(babies);
System.out.println("Average age of all babies: "+averageAge);
//d. Determine whether any two babies in the array are the same
for(i=0; i<babies.length; i++) {
for(int j=i+1; j<babies.length; j++) {
System.out.println(babies[i].getName()+ " and " + babies[j].getName() + " are equal? " + babies[i].equals(babies[j]));
}//inner for loop
}//outer for loop
}//main() end
//This is averageAge() method, it will calculate average age of Babies
public static double averageAge(Baby[] babies) {
//These are local variables of averageAge() method
int size = babies.length;
double averageAge = 0.0;
//This is for loop, it will calculate total age of babies
for(int i=0; i<size; i++) {
averageAge = averageAge + babies[i].getAge();
}//for end
//return average age to caller of this method
return averageAge/size;
}//averageAge() end
}// TestBaby end
//This is Baby.java class
public class Baby {
//These are instance variable of Baby.java class
private String name;
private int age;
//This is default constructor
public Baby() {
name = "Baby";
age = 1;
}
//This is parameterized constructor will take two parameters, a string to set the name and an integer to set the age
public Baby(String name, int age) {
this.name = name;
this.age = age;
}
//Supply methods for setting the name, setting the age, getting the name and getting the age.
public String getName() {
return name;
}
public void setName(String name) {
//The set method for the name instance variable should ensure that the input is not empty or contain whitespaces (otherwise set a default value)
if (name.trim().isEmpty()){
this.name = "Baby";
this.name = name;
}
}
public int getAge() {
return age;
}
public void setAge(int age) {
//The set method for the age instance variable should validate the input to be between 1 and 4 inclusive (otherwise set a default value).
if (age >= 1 && age <= 4) {
this.age = age;
} //if end
else {
this.age = 1;
}//else end
}// setAge() end
//Give Java code for an equals method for the Baby class.
#Override
public boolean equals(Object obj) {
//This is type casting
Baby baby = (Baby)obj;
//Compare name and age of Baby, if both are same then return true otherwise return false
if(this.age == baby.age && this.name.equalsIgnoreCase(baby.name))
return true;
return false;
}//equals() end
}// Baby end
Please try the code first so you understand what I mean incase its not clear
Baby.setName(String name) only sets the name if name is empty. Your method:
public void setName(String name) {
if (name.trim().isEmpty()){
this.name = "Baby";
this.name = name;
}
}
Try this:
public void setName(String name)
{
if (name == null)
{
// Use default from constructor
return;
}
String n = name.trim();
if (!n.isEmpty())
{
this.name = n;
}
}
I guess your setName() is badly written. You only set the new name if it is empty. I guess there is a not (!) missing.
Related
I have to find the highest scoring student and the lowest scoring student from the given user input.but i only get the highest scoring student and can't get the lowest scoring student from it.
public class Student{
public String name;
public String id;
public int score;
public static int n;
public Student(String initName,String initID,int initScore){
initName=name;
initID=id;
initScore=score;
}
public Student (){
}
public static void main(String[] args) {
System.out.println("Enter the number of students:");
Scanner s1=new Scanner(System.in);
Student.n=Integer.parseInt(s1.nextLine().trim());
System.out.println("Enter the student name,id and score.");
Scanner s2=new Scanner(System.in);
Student st1=new Student();
Student min=new Student(" "," ",100);
Student max=new Student(" "," ",0);
for(int i=0;i<Student.n;i++){
st1.name=s2.next();
st1.id=s2.next();
st1.score=s2.nextInt();
if(max.score<st1.score){
max.score=st1.score;
max.name=st1.name;
max.id=st1.id;
}
if(min.score>st1.score){
min.name=st1.name;
min.score=st1.score;
min.id=st1.id;
}
}
System.out.println("the highest scoring student: "+max.name);
System.out.println("the lowest scoring student: "+min.name);
}
}
initScore=score;
Please fix the above line in constructor.
Actually all the assignments are incorrect:
public Student(String initName,String initID,int initScore){
initName=name;
initID=id;
initScore=score;
}
It should be changed to
public Student(String initName, String initId, int initScore) {
name = initName;
id = initId;
score = initScore;
}
Even better
public Student(String name, String id, int score) {
this.name = name;
this.id = id;
this.score = score;
}
Since score is not assigned in the initial construction, it is assigned a default value of 0 and hence impacts the min computation. (as its already at min assuming the scores are positive)
It works for max computation as the scores obtained will be greater than or equal to 0 and will eventually update the score directly(assuming only positive scores are allowed).
I see plenty of problems in your code and it seems you don't really know how to code, which makes me wanna recommend going through some Java tutorial first.
Now to your code:
Student class implementation is just full of flaws, no encapsulation, mistakes in constructor, no idea what n is used for
public class Student {
private String name;
private String id;
private int score;
public Student(String initName, String initID, int initScore){
this.name = initName;
this.id = initID;
this.score = initScore;
}
// declare getters such as these two
public String getName() {
return this.name;
}
public int getScore() {
return this.score;
}
}
your main method has so many mistakes too: you have 2 Scanners, you don't initialize enough students and your iteration is plain wrong
public static void main(String[] args) {
Scanner s1 = new Scanner(System.in);
// get number of students
System.out.println("Enter the number of students:");
int numberOfStudents = s1.nextInt();
// initialize array of students
Student[] array = new Student[numberOfStudents];
for(int i = 0; i < numberOfStudents; i++) {
// get name of the student
System.out.println("Enter the student name:");
String name = s1.nextLine();
// get id of student
System.out.println("Enter the student id:");
String id = s1.nextLine();
// get score of student
System.out.println("Enter the student score:");
int score = s1.nextInt();
array[i] = new Student(name, id, score);
}
// now you have students input
// it's time to find your student
// set the lowest and highest student to first student
Student min = array[0];
Student max = array[0];
for(int i = 1; i < numberOfStudents; i++) {
if(min.getScore() > array[i]) {
min = array[i];
} else if(max.getScore() < array[i]) {
max = array[i];
}
}
System.out.println("the highest scoring student: "+max.getName());
System.out.println("the lowest scoring student: "+min.getName());
}
Carefully check the code again :
public Student(String initName,String initID,int initScore){
initName=name;
initID=id;
initScore=score;
}
Did you get it...? Buggy constructor.
The code should be
public Student(String initName,String initID,int initScore){
name = initName;
id = initID;
score = initScore;
}
I need to fill in the blanks on a phone book entry and phone book demo class. I've filled them all in except for two of them in the demo class, the parts that I need to fill in are represented by question marks.
PhoneBookEntry:
public class PhoneBookEntry
{
private String name; // Person's name
private String phoneNumber; // Person's phone number
/**
* The constructor initializes the person's name
* and phone number.
*/
public PhoneBookEntry(String n, String pn)
{
name = n;
phoneNumber = pn;
}
/**
* The setName method sets the person's name.
*/
public void setName(String n)
{
name = n;
}
/**
* setPhoneNumber method sets the person's
* phone number.
*/
public void setPhoneNumber(String pn)
{
phoneNumber = pn;
}
/**
* The getName method returns the person's
* name.
*/
public String getName()
{
return name;
}
/**
* The getPhoneNumber method returns the
* person's phone number.
*/
public String getPhoneNumber()
{
return phoneNumber;
}
}
I filled in the blanks on that one, in the PhoneBookDemo I don't have a clue on what to put in the spaces with the question marks:
public class PhoneBookDemo
{
public static void main(String args[])
{
// Constant for the numer of entries.
final int NUM_ENTRIES = 5;
// Create an ArrayList to hold PhoneBookEntry objects.
ArrayList<PhoneBookEntry> list =
new ArrayList<PhoneBookEntry>();
// Tell the user what's about to happen.
System.out.println("I'm going to ask you to enter " +
NUM_ENTRIES + " names and phone numbers.");
System.out.println();
// Create and store PhoneBookEntry objects in the ArrayList.
for (int i = 0; i < NUM_ENTRIES; i++)
{
???????
System.out.println();
}
System.out.println("Here's the data you entered:");
// Display the data stored in the ArrayList.
for (int i = 0; i < list.size(); i++)
{
????????????
}
}
/**
* The getEntry method creates a PhoneBookEntry object
* populated with data entered by the user and returns
* a reference to the object.
*/
public static PhoneBookEntry createEntry()
{
// Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);
// Variables to hold a person's name and
// phone number.
String name;
String phoneNumber;
// Get the data.
System.out.print("Enter a person's name: ");
name = keyboard.nextLine();
System.out.print("Enter that person's phone number: ");
phoneNumber = keyboard.nextLine();
// Create a PhoneBookEntry object.
PhoneBookEntry entry = new PhoneBookEntry(name, phoneNumber);
// Return a reference to the object.
return entry;
}
/**
* The displayEntry method displays the data stored
* in a PhoneBookEntry object.
*/
public static void displayEntry(PhoneBookEntry entry)
{
System.out.println("------------------------------");
System.out.println("Name: " + entry.getName());
System.out.println("Phone number: " + entry.getPhoneNumber());
}
}
You can use the two methods in your code which are not used
// Create and store PhoneBookEntry objects in the ArrayList.
for (int i = 0; i < NUM_ENTRIES; i++)
{
list.add (createEntry());
}
and
for (int i = 0; i < list.size(); i++)
{
displayEntry(list.get(i));
}
Create and store : call the method that create an instance, and add it to the list
for (int i = 0; i < NUM_ENTRIES; i++){
PhoneBookEntry create = createEntry();
list.add(create);
System.out.println();
}
Display : call the displayEntry method on each element, but the best way is to implement the toString() method in the PhoneBookEntry class and use a System.out.println(list.get(i));
for (int i = 0; i < list.size(); i++){
displayEntry(list.get(i));
}
Take a look at following code:
public class PhoneBookDemo {
public static void main(String args[]) {
// Constant for the numer of entries.
final int NUM_ENTRIES = 5;
// Create an ArrayList to hold PhoneBookEntry objects.
ArrayList<PhoneBookEntry> list
= new ArrayList<PhoneBookEntry>();
// Tell the user what's about to happen.
System.out.println("I'm going to ask you to enter "
+ NUM_ENTRIES + " names and phone numbers.");
System.out.println();
// Create and store PhoneBookEntry objects in the ArrayList.
for (int i = 0; i < NUM_ENTRIES; i++) {
list.add(createEntry());
}
System.out.println("Here's the data you entered:");
// Display the data stored in the ArrayList.
for (int i = 0; i < list.size(); i++) {
displayEntry(list.get(i));
}
}
/**
* The getEntry method creates a PhoneBookEntry object populated with data
* entered by the user and returns a reference to the object.
*/
public static PhoneBookEntry createEntry() {
// Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);
// Variables to hold a person's name and
// phone number.
String name;
String phoneNumber;
// Get the data.
System.out.print("Enter a person's name: ");
name = keyboard.nextLine();
System.out.print("Enter that person's phone number: ");
phoneNumber = keyboard.nextLine();
// Create a PhoneBookEntry object.
PhoneBookEntry entry = new PhoneBookEntry(name, phoneNumber);
// Return a reference to the object.
return entry;
}
/**
* The displayEntry method displays the data stored in a PhoneBookEntry
* object.
*/
public static void displayEntry(PhoneBookEntry entry) {
System.out.println("------------------------------");
System.out.println("Name: " + entry.getName());
System.out.println("Phone number: " + entry.getPhoneNumber());
}
}
public class PhoneBookEntry {
private String name;
private String phoneNumber;
public PhoneBookEntry() {
}
public PhoneBookEntry(String name, String phoneNumber) {
this.name = name;
this.phoneNumber = phoneNumber;
}
/**
* #return the name
*/
public String getName() {
return name;
}
/**
* #param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* #return the phoneNumber
*/
public String getPhoneNumber() {
return phoneNumber;
}
/**
* #param phoneNumber the phoneNumber to set
*/
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
}
I think you should not hold phone book's entires apart from phone bool. Do incapsulate this logic into the phone book:
public final class PhoneBook {
private final Map<String, Entry> entries = new TreeMap<>();
public void add(String name, String phoneNumber) {
entries.put(name, new Entry(name, phoneNumber));
}
public Iterator<Entry> iterator() {
return entries.values().iterator();
}
public static final class Entry {
private final String name;
private final String phoneNumber;
public Entry(String name, String phoneNumber) {
this.name = name;
this.phoneNumber = phoneNumber;
}
public String getName() {
return name;
}
public String getPhoneNumber() {
return phoneNumber;
}
}
}
Then you can define method, that ask user for phone book entries and retrieves the whole phone book instead of list of phone book's entreis:
public static PhoneBook getPhoneBook(int totalEntries) {
try (Scanner scan = new Scanner(System.in)) {
PhoneBook phoneBook = new PhoneBook();
System.out.println("I'm going to ask you to enter " + totalEntries + " names and phone numbers.");
System.out.println();
for (int i = 1; i <= totalEntries; i++) {
System.out.println("Entry " + i + ':');
System.out.print("Enter a person's name: ");
String name = scan.nextLine();
System.out.print("Enter that person's phone number: ");
String phoneNumber = scan.nextLine();
System.out.println();
phoneBook.add(name, phoneNumber);
}
return phoneBook;
}
}
Finally you can use this method to get a phone book and print all entires:
final int totalEntries = 5;
getPhoneBook(totalEntries).iterator().forEachRemaining(entry -> {
System.out.println("------------------------------");
System.out.println("Name: " + entry.getName());
System.out.println("Phone number: " + entry.getPhoneNumber());
});
//iterate over number of entries
for (int i = 0; i < NUM_ENTRIES; i++)
{
//add to the phonebooklist
list.add (createEntry());
System.out.println();
}
System.out.println("Here's the data you entered:");
// Display the data stored in the ArrayList.
for (int i = 0; i < list.size(); i++)
{
//display entry from list
displayEntry(list.get(i));
//use display entry method for display
}
how to print Junior, Intermediate,Seniour as string as per age,and must generate roll number and these input must b generated from the user. Ex: junior name:Rahul age:22, roll:1000 intermediate name:prem age:40, roll:1001 senior name: Vamsi age:60, rollno:1002
String name;
static int age;
public static void main(String[] args) {
int size = 3;
DB[] studs = new DB[size];
for (int i = 0; i < size; i++) {
studs[i] = readStudent(i);
}
for (int i = 0; i <studs.length; i++) {
if(age <=30) {
System.out.println( studs[i]);
}else if(age <=40) {
System.out.println("Intermidiate" +" "+studs[i]);
}else {
System.out.println("Seniour" +" "+studs[i]);
}
}
}
static DB readStudent(int i) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter your name:");
String name = sc.nextLine();
System.out.print("Enter your age:");
int age = sc.nextInt();
DB studs = new DB(name, age);
return studs;
}
}
class juniourStudent extends Student {
String rollno = "juniour";
static int juniourcount = 1000;
juniourStudent(String name, int age) {
rollno = "juniour" + juniourcount++;
}
}
class intermidiateStudent extends Student {
String rollno = "intermidiate";
static int intermidiatecount = 1000;
intermidiateStudent(String name, int age) {
rollno = "intermidiate" + intermidiatecount++;
}
}
class seniourStudent extends Student {
String rollno = " seniour";
static int seniourcount = 1000;
seniourStudent(String name, int age) {
rollno = "seniour" + seniourcount++;
}
}
There are quite a few issues with your code.
Your class structure is counter-intuitive to what you are trying to do. Based on the example output, it seems that you want the rollNo to increase every time a student is made. It also seems to be redundant to create separate classes when the only thing that differs between them is the prefix (like junior, senior, intermediate, etc.). Instead of making multiple classes, why not add an instance variable to your student class. (Also import Scanner so we can use it later for user input, and ArrayList to store the student data) For Example:
import java.util.Scanner;
import java.util.ArrayList;
public class Student {
String name;
String studentType;
int age;
int rollno;
static int schoolRollno = 1000;
Next you should make a proper constructor for your student class consisting of a String name and an int age, and assigning them to the instance variables we declared earlier. We will assign the studentType variable based upon their age, and we will obtain the students rollno from the static variable. We must increment the static variable so the next student will have a different roll number. (You can change the age comparisons to whatever number you need, but these numbers are what I pieced together from your code)
public Student(String name, int age) {
this.name = name;
if(age <= 30) {
this.studentType = "Junior";
}
else if(age <= 40) {
this.studentType = "Intermediate";
}
else {
this.studentType = "Senior";
}
this.age = age;
this.rollno = schoolRollno;
schoolRollno++;
}
The last thing we need to do before we work on our main function is to write a toString() method for our student class. Based upon the output, you might want a method like this:
#Override
public String toString() {
return studentType + " name: " + name + " age: " + age + ", roll: " + rollno;
}
Now we can take a look at the public static void main(String args[]) function. We have a few things we need to do here. Since it appears we do not know the number of students we are going to be adding, we should use an ArrayList. We should also instantiate our scanner here as well:
ArrayList<Student> students = new ArrayList<Student>();
Scanner scan = new Scanner(System.in);
Now we should create a while loop that allows the user to continue inputting students and wish to stop. Personally, I'm a fan of the do/while loop, so I'll do it that way. We'll need to declare our variable outside of the loop so that we can use it as a conditional for the loop.
boolean done = false;
do {
//(inner loop code here)
} while(!done);
//end loop code
Now for the inner loop code. We need to obtain a String (name) from the user and an int (age). Once we obtain the inputs, we want to construct a Student object and add it to our ArrayList. Then, after all that, we want to see if the user wants to input another student (by changing the value of our boolean we used when declaring the first do/while loop according to the user's input). We can do all this using scanner like so:
System.out.println("Enter name:");
String name = scan.nextLine();
System.out.println("Enter age:");
int age = scan.nextInt();
//we use scan.nextLine() here because scan.nextInt() does not read to the end of the line
scan.nextLine();
Student s = new Student(name, age);
students.add(s);
boolean validInput = true;
do {
validInput = true;
System.out.println("Continue? (y/n)");
String input = scan.nextLine();
if(input.equalsIgnoreCase("y")) {
done = false;
}
else if(input.equalsIgnoreCase("n")) {
done = true;
}
else {
validInput = false;
}
} while(!validInput);
Finally, we want to print the students once we are done. We can print all the students in our ArrayList easily using a for-each loop. (This goes at the end of our main(String args[]) method)
for(Student s: students) {
System.out.println(s.toString());
}
TL;DR:
Combine your classes into one student class
Fix the constructor
Add a toString() method
Use an ArrayList to store the students b/c we don't know the number. (DB isn't a class in java, and an array is counter-intuitive when you don't know what size you'll need)
Create a loop that the user can break out of when they wish to
Obtain user input for the name and age, and use this to construct a student object and add it to the ArrayList
Print out all the students in the ArrayList after the loop
The Cullerton Part District holds a mini-Olympics each summer. Create a class named Participant with fields for a name, age, and street address. Include a constructor that assigns parameter values to each field and a toString() method that returns a String containing all the values. Also include an equals() method that determines two Participants are equal if they have the same values in all three fields. Create an application with two arrays of at least 5 Participants each--one holds the Participants in the mini-marathon and the other holds Participants in the diving competition. Prompt the user for Participants who are in both events save the files as BC.java and ABC.java.
import javax.swing.JOptionPane;
import java.util.*;
public class ABC {
private static Participant mini[] = new Participant[2];
public static void main(String[] args) {
setParticipant();
displayDetail();
}
// BC p=new BC(name,age,add);
//displayDetails();
// System.out.println( p.toString());
public static void displayDetail() {
String name=null;
String add = null;
int age=0;
System.out.println("Name\tAdress\tAge");
BC p=new BC(name,age,add);
for (int x = 0; x < mini.length; x++) {
//Participant p1=mini[x];
System.out.println(p.toString());
}
}
public static String getName() {
Scanner sc = new Scanner(System.in);
String name;
System.out.print(" Participant name: ");
return name = sc.next();
}
// System.out.print(" Participant name: ");
// name = sc.next();
public static int getAge() {
int age;
System.out.print(" Enter age ");
Scanner sc=new Scanner(System.in);;
return age= sc.nextInt();
}
public static String getAdd() {
String add;
Scanner sc=new Scanner(System.in);;
System.out.print("Enter Address: ");
return add=sc.next();
}
public static void setParticipant(){
for (int x = 0; x < mini.length; x++) {
System.out.println("Enter loan details for customer " + (x + 1) + "...");
//Character loanType=getLoanType();
//String loanType=getLoanType();
String name=getName();
String add=getAdd();
int age=getAge();
System.out.println();
}
}
}
//another class
public class BC {
private String name;
private int age;
private String address;
public BC(String strName, int intAge, String strAddress) {
name = strName;
age = intAge;
address = strAddress;
}
#Override
public String toString() {
return "Participant [name=" + name + ", age=" + age + ", address=" + address + "]";
}
public boolean equals(Participant value){
boolean result;
if (name.equals(name) && age==value.age && address.equals(address))
result=true;
else
result=false;
return result;
}
}
outPut:
Enter loan details for customer 1...
Participant name: hddgg
Enter Address: 122
Enter age 12
Enter loan details for customer 2...
Participant name: ddjkjde
Enter Address: hdhhd23
Enter age 12
//Why I'm not getting right output
Name Adress Age
Participant [name=null, age=0, address=null]
Participant [name=null, age=0, address=null]
You are getting that output because of this method:
public static void displayDetail() {
String name=null;
String add = null;
int age=0;
System.out.println("Name\tAdress\tAge");
BC p=new BC(name,age,add);
for (int x = 0; x < mini.length; x++) {
//Participant p1=mini[x];
System.out.println(p.toString());
}
}
You are creating a BC with null for name and add and 0 for age. You are then printing it twice.
Q. Create a class named Participant with fields for a name, age, and street address. Include a constructor that assigns parameter values to each field and a toString() method that returns a String containing all the values.
Also include an equals() method that determines two Participants are equal if they have the same values in all three fields.
Create an application with two arrays of at least 5 Participants each--one holds the Participants in the mini-marathon and the other holds Participants in the diving competition. Prompt the user for Participants who are in both events save the files as Participant.java and TwoEventParticipants.java.*/
Here is my code so far. How do I display the value of Participants who are in both events ?
import javax.swing.JOptionPane;
import java.util.*;
public class TwoEventParticipants {
private static Participant mini[] = new Participant[2];
private static Participant diving[] = new Participant[2];
public static void main(String[] args) {
String name="";;
String add="";
int age=0;
Participant p=new Participant(name, age, add);
Participant p1=new Participant(name, age, add);
setParticipant();
setParticipant1();
displayDetail();
displayDetail1();
//Arrays.sort(p1);
if (p.equals(p1)){
System.out.println(p);
}else{
System.out.println(p1);
}
}
public static void setParticipant(){
for (int x = 0; x < mini.length; x++) {
System.out.println("Enter loan details for customer " + (x + 1) + "...");
//Character loanType=getLoanType();
//String loanType=getLoanType();
String name=getName();
String add=getAdd();
int age=getAge();
System.out.println();
mini[x] = new Participant(name, age, add); //<--- Create the object with the data you collected and put it into your array.
}
}
public static void setParticipant1(){
for (int y = 0; y < diving.length; y++) {
System.out.println("Enter loan details for customer " + (y + 1) + "...");
String name=getName();
String add=getAdd();
int age=getAge();
System.out.println();
diving[y] = new Participant(name, age, add);
}
}
// Participant p=new Participant(name,age,add);
//displayDetails();
// System.out.println( p.toString());
public static void displayDetail() {
// for (int y = 0; y < diving.length; y++) {
System.out.println("Name \tAge \tAddress");
//Participant p=new Participant(name,age,add);
for (int x = 0; x < mini.length; x++) {
System.out.println(mini[x].toString());
// System.out.println(diving[y].toString());
}
}
public static void displayDetail1() {
System.out.println("Name \tAge \tAddress");
for (int y = 0; y < diving.length; y++) {
System.out.println(diving[y].toString());
}
}
public static String getName() {
Scanner sc = new Scanner(System.in);
String name;
System.out.print(" Participant name: ");
return name = sc.next();
}
// System.out.print(" Participant name: ");
// name = sc.next();
public static int getAge() {
int age;
System.out.print(" Enter age ");
Scanner sc=new Scanner(System.in);;
return age= sc.nextInt();
}
public static String getAdd() {
String add;
Scanner sc=new Scanner(System.in);;
System.out.print("Enter Address: ");
return add=sc.next();
}
}
Participant with fields for a name, age, and street address
//
public class Participant {
private String name;
private int age;
private String address;
public Participant(String name, int age, String address) {
this.name = name;
this.age = age;
this.address = address;
}
#Override
public String toString() {
return name + " " + age + " " + address ;
}
// include an equals() method that determines two Participants are equal
public boolean equals(Participant[] name,Participant[] age,Participant[] add) {
if (this.name.equals(name) && this.address.equals(address)&& age == age){
return true;
}
else{
return false;
}
}
}
This will work for you:
for(Participant p : mini){
if(diving.contain(p)){
System.out.pringtln(p.toString()) ;
}
}