#Java toString help

1 messages · Page 1 of 1 (latest)

inland fractal
#

Patient.java

public class Patient {
    int id;
    double caffeineLevel;
    
    public Patient(int id, double caffeineLevel) {
        this.id = id;
        this.caffeineLevel = caffeineLevel;
    }
}

PatientManager.java

public class PatientManager {
    Patient[] patients;
    
    public PatientManager(){
        this.patients = new Patient[10];
    }
    
    public int addPatient(Patient patient) {
        for (int i = 0; i < patients.length; i++) {
            if(patients[i] == null) {
                patients[i] = patient;
                return i;
            }
        }
        return -1;
    }
    
    public Patient removePatient(int i) {
        Patient removedPatient = patients[i];
        patients[i] = null;
        return removedPatient;
    }
    
    public void caffeineAbsorbtion() {
        for (int i = 0; i < patients.length; i++) {
            if (patients[i] != null) {
                patients[i].caffeineLevel -= 130;
                if (patients[i].caffeineLevel <= 0)
                    removePatient(i);
            }
        }
    }

// NEED HELP HERE PLEASE //
    public String toString() {
            for (int i = 0; i < patients.length; i++) {
                if (patients[i] == null) {
                    return "Empty";
                }
                else{
                    for (int z = 0; z < patients.length; z++) {
                        while (patients[z] != null) {
                            int id = patients[z].id;
                            double level = patients[z].caffeineLevel;
                            return Integer.toString(id) + " " + Double.toString(level);
                        }
                        
                    }
                }
            }
            return null;
    }
}
sour steppeBOT
#

<@&987246399047479336> please have a look, thanks.

sour steppeBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

inland fractal
#

PatientTester.java


public class PatientTester {
    public static void main(String[] args) {
        PatientManager PM = new PatientManager();
        System.out.println(PM);
        
        PM.addPatient(new Patient(1, 100));
        PM.addPatient(new Patient(2, 200));
//        PM.addPatient(new Patient(3, 300));
//        PM.addPatient(new Patient(4, 400));
        
        System.out.println(PM);
        
//        PM.caffeineAbsorbtion();
//        
//        System.out.println(PM);
        
    }
}
sour steppeBOT
inland fractal
#

So the only attributes are patient id and caffeine level yes, but my homework assignment in the tester looks like this:
https://i.gyazo.com/67f84fc3f9e2e3a18bc012e04895639c.png

So essentially I need to add 4 different patient objects and print all of them in the format of:
id caffeineLevel

But I can't figure out how to tell the toString method to take in all 4 objects rather just taking in the first one and printing it.

fleet lion
#

the problem is that you instantly return

#
    public String toString() {
            for (int i = 0; i < patients.length; i++) {
                if (patients[i] == null) {
                    return "Empty"; // returning exits the method
                }
                else{
                    for (int z = 0; z < patients.length; z++) {
                        while (patients[z] != null) {
                            int id = patients[z].id;
                            double level = patients[z].caffeineLevel;
                            return Integer.toString(id) + " " + Double.toString(level); // returning exits the method
                        }
                        
                    }
                }
            }
            return null;
    }
#

you should instead create an empty string and then edit it or even us a string builder

inland fractal
fleet lion
#

maybe like this:

public String toString() {
    String text = "";
    for (int i = 0; i < array.length; i++) {
        text += array[i] + "\n";
    }
    return text;
}