When I run the program it shows me on the first values on the list which is the one with the [0] here is my array code:
KeyStroke[] clientsDetails = new KeyStroke[5];
clientsDetails[0] = new KeyStroke(9,"OX5BJM","Peter",2039489);
clientsDetails[1] = new KeyStroke(12,"OX1BOL","Kim",2434587);
clientsDetails[2] = new KeyStroke(67,"OX2VBN","Patrick",2233842);
clientsDetails[3] = new KeyStroke(34,"OX2XHB","Liam",2432340);
clientsDetails[4] = new KeyStroke(54,"OX3BUN","Bob",2234098);
And here is the code to support the array:
if(input.matches("S")){
System.out.println("Enter an array postion from 1 to 4 to show paitient's details");
number1 = enterNumber0.nextInt();
System.out.println("Name: " +clientsDetails[0].nameLable);
System.out.println("Age: " +clientsDetails[0].howOld);
System.out.println("Postcode: " +clientsDetails[0].postcode);
System.out.println("Phone Number: " + clientsDetails[0].cellPhoneNumber);
number2 = enterNumber1.nextInt();
System.out.println("Name: " +clientsDetails[1].nameLable);
System.out.println("Age: " +clientsDetails[1].howOld);
System.out.println("Postcode: " +clientsDetails[1].postcode);
System.out.println("Phone Number: " + clientsDetails[1].cellPhoneNumber);
number3 = enterNumber2.nextInt();
System.out.println("Name: " +clientsDetails[2].nameLable);
System.out.println("Age: " +clientsDetails[2].howOld);
System.out.println("Postcode: " +clientsDetails[2].postcode);
System.out.println("Phone Number: " + clientsDetails[2].cellPhoneNumber);
number4 = enterNumber1.nextInt();
System.out.println("Name: " +clientsDetails[3].nameLable);
System.out.println("Age: " +clientsDetails[3].howOld);
System.out.println("Postcode: " +clientsDetails[3].postcode);
System.out.println("Phone Number: " + clientsDetails[3].cellPhoneNumber);
}
Help will be greatly appreciated this is the last bit of my work that I'm stuck on. I have no teacher to advice this for me as to why I added a question on this forum I hope nobody gets offended.
Assuming enterNumber0
is a Scanner that is already created, we can do something like this:
// Populate the array
KeyStroke[] clientsDetails = new KeyStroke[]{
new KeyStroke(9,"OX5BJM","Peter",2039489),
new KeyStroke(12,"OX1BOL","Kim",2434587),
new KeyStroke(67,"OX2VBN","Patrick",2233842),
new KeyStroke(34,"OX2XHB","Liam",2432340),
new KeyStroke(54,"OX3BUN","Bob",2234098)
}
Not 100% sure what input
does here, so I've kept this part. Also note that we fetch the ID from the user non-zero-indexed, so we have to subtract 1 to all the values. You also seem to have misunderstood how to use this value to return the correct client details. You use this value as the index in the array.
if (input.matches("S")) {
System.out.println("Enter an array postion from 1 to 5 to show paitient's details");
int number = enterNumber0.nextInt();
if (number >= 1 && number <= 5) {
System.out.println("Name: " +clientsDetails[number - 1].nameLable);
System.out.println("Age: " +clientsDetails[number - 1].howOld);
System.out.println("Postcode: " +clientsDetails[number - 1].postcode);
System.out.println("Phone Number: " + clientsDetails[number - 1].cellPhoneNumber);
}
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments