How do I pick an array position when pressing a key for example a number from 1 to 5?

Jihjin Kairo Hunter

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.

OptimusCrime

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How do I pick randomly from an array?

From Dev

Unity3D: How do i change force when pressing 1 key then pressing another at the same time?

From Dev

How do I check if the user is pressing a key?

From Dev

How do I automate pressing the F5 key using vbscript?

From Dev

How do I automate pressing the F5 key using vbscript?

From Dev

How do I find the position number of an array that matches a textbox value?

From Dev

How do I call a button click by pressing the Enter key in Powershell

From Dev

In java how do I check if a user is pressing a certain key?

From Dev

How do I call a button click by pressing the Enter key in Powershell

From Dev

How to block fields from being available when pressing the tab key

From Dev

How to block fields from being available when pressing the tab key

From Dev

how do i shuffle an array of numbers using python. A number is only allowed to move a step from its origin position

From Dev

How do I pick a string from a larger string within an array and group them with name?

From Dev

How do I pick a string from a larger string within an array and group them with name?

From Dev

How do I pick a word that's next to a number in a file in Python?

From Dev

How do I fill an n-dimensional array in HDF5 from a 1D source?

From Dev

How do I go from an array of key => "array of values" to an array of an array of "key => values" in php

From Java

How can I pick highest 3 string values from array

From Dev

How do I print the number of negative subarrays from a given array?

From Dev

How do I cherry pick packages from a PPA?

From Dev

How do I cherry pick packages from a PPA?

From Dev

Pressing the tilde key (~) waits for a second key stroke, how do I disable this?

From Dev

Pressing the tilde key (~) waits for a second key stroke, how do I disable this? (2020 version)

From Dev

Pick from array which is equal to some number

From Dev

How do I rebind the "Show position of pointer when the Control key is pressed" to left click?

From Dev

Timestamp: Pressing F5 notepad gives time 1st then date. How do I do date 1st then time?

From Dev

How can I retain the scroll position of a scrollable area when pressing back button?

From Dev

How do I traverse to the next position of an array

From Dev

How do I calculate the new position (latitude and longitude) after increment x meters from position1 to position2?

Related Related

  1. 1

    How do I pick randomly from an array?

  2. 2

    Unity3D: How do i change force when pressing 1 key then pressing another at the same time?

  3. 3

    How do I check if the user is pressing a key?

  4. 4

    How do I automate pressing the F5 key using vbscript?

  5. 5

    How do I automate pressing the F5 key using vbscript?

  6. 6

    How do I find the position number of an array that matches a textbox value?

  7. 7

    How do I call a button click by pressing the Enter key in Powershell

  8. 8

    In java how do I check if a user is pressing a certain key?

  9. 9

    How do I call a button click by pressing the Enter key in Powershell

  10. 10

    How to block fields from being available when pressing the tab key

  11. 11

    How to block fields from being available when pressing the tab key

  12. 12

    how do i shuffle an array of numbers using python. A number is only allowed to move a step from its origin position

  13. 13

    How do I pick a string from a larger string within an array and group them with name?

  14. 14

    How do I pick a string from a larger string within an array and group them with name?

  15. 15

    How do I pick a word that's next to a number in a file in Python?

  16. 16

    How do I fill an n-dimensional array in HDF5 from a 1D source?

  17. 17

    How do I go from an array of key => "array of values" to an array of an array of "key => values" in php

  18. 18

    How can I pick highest 3 string values from array

  19. 19

    How do I print the number of negative subarrays from a given array?

  20. 20

    How do I cherry pick packages from a PPA?

  21. 21

    How do I cherry pick packages from a PPA?

  22. 22

    Pressing the tilde key (~) waits for a second key stroke, how do I disable this?

  23. 23

    Pressing the tilde key (~) waits for a second key stroke, how do I disable this? (2020 version)

  24. 24

    Pick from array which is equal to some number

  25. 25

    How do I rebind the "Show position of pointer when the Control key is pressed" to left click?

  26. 26

    Timestamp: Pressing F5 notepad gives time 1st then date. How do I do date 1st then time?

  27. 27

    How can I retain the scroll position of a scrollable area when pressing back button?

  28. 28

    How do I traverse to the next position of an array

  29. 29

    How do I calculate the new position (latitude and longitude) after increment x meters from position1 to position2?

HotTag

Archive