简体   繁体   中英

How do I print data stored in if statement multiple time?

So how do I make the data stored in a variable to store each time before displaying it in the end.

I have a for loop statement that set data into array.

Then I create an if statement in the for loop statement to check if it matches then, store the data entered in a variable and display it in the end. I'll give you an example code

public static void main (String [] args)
{
    TestClass [] emp = new testclass [3];

    String name, id, nickname;
    int sameName = 0;

    for (int i = 0; i < 3; i++)
    {
        emp[i] = new testclass();

        name = JOptionPane.showInputDialog ("Your name");
        id = JOptionPane.showInputDialog ("Your ID");
        nickname = JOptionPane.showInputDialog ("Your Nickname");

        emp[i].setTestClass (name, id, nickname);

        System.out.println (emp[i]);

        if ("David".equalsIgnoreCase(emp[i].getName()) && "Dave".equalsIgnoreCase(emp[i].getNickname()))
        {
            sameName = i;
        }
    }

    System.out.println ("People with the same name and nickname : \n\n" + emp[sameName]) //Is this the correct way?
}

So as you notice the program will first display out all 3 input,

then display out output "people with the same name & nickname".

So assume, there are two persons with same name and nickname.

How do you make it possible to print the data stored at the end after all 3 input displayed out?

You cannot do that with single variable for index, as there could be many users matching your criteria. There are many ways to handle that. The simplest would be to:

before for loop create any container, eg

List<TestClass> matching = new ArrayList<TestClass>();

inside for loop, if particular item is matching your criterias, just add it to the collection:

matching.add(emp[i])

after for loop you have references to all items matching you criterias, so you can do whatever you want with them. Just printing for example:

for(final TestClass item : matching) {
   System.out.println ("Another person matching was: " + item.getName());
}

best regards,

Darek

You will have to store the indizes or something similar, that have the same name. Making it simple just with arrays it would something like this:

boolean[] sameName = new boolean[emp.length];
...

// in the loop the if statement:
if ("David".equalsIgnoreCase(emp[i].getName()) && "Dave".equalsIgnoreCase(emp[i].getNickname()))
{
    sameName[i] = true;
}
else
{
    sameName[i] = false;
}    
...

// the output part:
System.out.println ("People with the same name and nickname : \n");
for (int i = 0; i < emp.length; i++) {
    if(sameName[i])
        System.out.println(emp[i]);
}

You could store the Dave/David duplicate's indexes and that print them.

A quick and dirty way could be this

String indexes = "";
for (int i = 0; i < 3; i++)
    {
        emp[i] = new testclass();

        name = JOptionPane.showInputDialog ("Your name");
        id = JOptionPane.showInputDialog ("Your ID");
        nickname = JOptionPane.showInputDialog ("Your Nickname");

        emp[i].setTestClass (name, id, nickname);

        System.out.println (emp[i]);

        if ("David".equalsIgnoreCase(emp[i].getName() && "Dave".equalsIgnoreCase(emp[i].getNickname()))
        {
            indexes += i.toString();
        }
    }
System.out.println ("People with the same name and nickname : \n");
for (int j = 0; j < indexes.length(); j+=1) {
    int index = Integer.parseInt( indexes.charAt(j) );
    System.out.println(emp[j]);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM