Jack Input Jack Input - 1 year ago 58
Java Question

Searching an array with multiple columns and display the results JAVA

So I have an array that looks like this


I made a search function to search the 2nd column which is the student ID and if the ID is found then I display the whole string, for example if I type 1142 I want "AF2006 1142 F 52 B" to be displayed, here's my code:

public static void searchData(String studentCIs [][]) {

String idToSearch;

System.out.println("Type the student's ID to search:");
idToSearch = userInput.nextLine();

for (int row=0; row<studentCIs.length; row++) {
for (int column=0; column<studentCIs[0].length; column++) {
if (studentCIs[row][1].equalsIgnoreCase(idToSearch))
System.out.print(studentCIs[row][column] + "\t");


When I type 1142 this is what's outputted:

AF2006 1142 F 52 B AF3313 1142 M 40 C AF3319 1142 M 12 F

any help?

Answer Source
 String[][] studentCIs = new String[][]{{"AF2006","1142","F","52","B"},
                {"AF2006","1167","M","48","C+"}}; //is this your array?
        Scanner scanner = new Scanner(System.in); //scanner is reading from System.in

//this code:
        String idToSearch = scanner.nextLine();
        for (String[] row : studentCIs)
            if (row[1].equalsIgnoreCase(idToSearch))
                System.out.println("[StudnetCIs]: "+row[0]+" "+row[1]+" "+row[2]+" "+row[3]+" "+row[4]);

Output: [StudnetCIs]: AF2006 1142 F 52 B

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download