PDA

View Full Version : need help with arrays


mia_tech
02-12-2009, 07:02 AM
guys I need help with this project of entering name on the arrayList, but I'm not able to get them print to screen... here's the main Class, and the Patients Class

import java.util.Scanner;
import java.util.ArrayList;
public class Main {

/**
* @param args the command line arguments
*/
public static void main(String [] args){
int num = 2;

ArrayList<Patients> HospPatients = new ArrayList<Patients>();

Scanner myscan = new Scanner(System.in);

for( int i = 0; i <= num; i++){
System.out.println("please enter you name: ");
String name = myscan.nextLine();
Patients patientOne = new Patients(name);
HospPatients.add(patientOne);
}
for ( int i = 0; i <= num; i++ ){
System.out.println(HospPatients.get(i));
}
}

}

Patients Class
package hospital;

/**
*
* @author jorge.vazquez001
*/
public class Patients {
//attributes
private String name = " ";
private int age = 0;
private String symptoms = " ";
private double balance = 0;
//constructor
public Patients(String tempName){
name = tempName;

}
//getters
public String getName(){
return name;
}
public int getAge(){
return age;
}
public String getSymptoms(){
return symptoms;
}
//balance
public double getBalance(){
return balance;
}

//setters
public void PatientsAge( int tempAge){
age = tempAge;
}

public void setBalance ( double tempbalance ){
balance = tempbalance;
}
public void setSymptoms( String tempsymptoms){
symptoms = tempsymptoms;
}


}

servlet
02-12-2009, 07:31 AM
Patients class needs to override toString() method.

mia_tech
02-12-2009, 07:23 PM
I got this from an example of the book, I'm using in class, but I understand everything in this peace of code except the @Override toString() method, could you explain to me what actually does that part so I can implement it on the above program

package student;

/**
*
* @author dfreer
*/
import java.util.ArrayList;
public class Student {

private String studentName = "";
ArrayList <StudentClass> studentClasses = new ArrayList<StudentClass>();

public Student(String n, StudentClass a)
{
studentName = n;
studentClasses.add(a);
}

public Student(String n, ArrayList<StudentClass> b)
{
studentName = n;
studentClasses = b;
}

public String getStudentName()
{
return studentName;
}

public void addClass(StudentClass a)
{
studentClasses.add(a);
}

@Override
public String toString()
{
String listOfClasses = "";
for(StudentClass a: studentClasses)
{
listOfClasses = listOfClasses + a.getName() + " for " +
a.getCredits() + " credits" + "\n";
}
return studentName + " is taking \n" + listOfClasses;
}

public static void main (String [] args)
{
StudentClass first = new StudentClass("Physics", 3);
StudentClass second = new StudentClass("Chemistry", 4);
Student david = new Student("David", first);
ArrayList<StudentClass> jonClasses = new ArrayList<StudentClass>();
jonClasses.add(first);
jonClasses.add(second);
System.out.println(david);
Student jon = new Student("Jonathan", jonClasses);
System.out.println(jon);
}
}

Gox
02-12-2009, 07:39 PM
When you are calling System.out.println(HospPatients.get(i)) you're asking java to print out a "Patient" object. But it doesn't know how to do that in a meaningful manner because you haven't defined one, thus it prints out the objects reference instead.

There's a couple ways you can get the results you wish. First let's look at what Servlet has suggested and override toString(). With this solution you need to change the Patient class but can leave Main the same.

In the patient class (like your student example) you can override the toString() method so that java has a definition for how you'd like your class object to be printed. So in your Patient class add a toString method

@Override
public String toString()
{
//This is where you have to decide how you want a Patients info to be printed
//I'll just have it print the Name and Age
return this.getName() + " " + this.getAge();
}

The above method is nice and clean since it's a single method that defines how a Patient object should be printed, but you can also accomplish this by changing your Main and leaving your Patient class untouched. We can do this by just calling the methods already defined in Patient and just "stringing"
them together to print out a patients info. anyway we wish. Take a look at the highlighted line of code below.

import java.util.Scanner;
import java.util.ArrayList;
public class Main {

/**
* @param args the command line arguments
*/
public static void main(String [] args){
int num = 2;

ArrayList<Patients> HospPatients = new ArrayList<Patients>();

Scanner myscan = new Scanner(System.in);

for( int i = 0; i <= num; i++){
System.out.println("please enter you name: ");
String name = myscan.nextLine();
Patients patientOne = new Patients(name);
HospPatients.add(patientOne);
}
for ( int i = 0; i <= num; i++ ){
System.out.println(HospPatients.get(i).getName() + " " + HospPatients.get(i).getAge());
}
}

}

Hope that helps.

mia_tech
02-12-2009, 08:02 PM
thanks a lot... it works either way, I guess that in my code I was just printing the position of the arrayList using hospPatients.get(i), and not its content using hospPatients.get(i).getName() was that the case?, and also I think that what the @Override toSring() method does it actually overrides the toString() method that is used when you use the System.out.println(...) am I right?