简体   繁体   中英

Get separate record for each value

I have a list, List<String> myList=new ArrayList<String>(); This list contains the list of countries that I am dealing with.

I am dealing with several records. I need to calculate in such a way that a separate entry for records as per country is sorted.I am using the following logic

for(int zTmp = 0; zTmp<myList.size(); zTmp++)
{
    System.out.println("COUNTRY IS"+myList.get(zTmp));
    if((record).contains(myList.get(zTmp)))
    {  
        // my next step
    }
}

How ever I find that each and every record is entering after the if condition. The records are alphabetically sorted as per countries, and records of every country are together. Please correct me.

This is my String

RECORD 1@India$
RECORD 2@India$
RECORD 3@United Arab Emirates$
RECORD 4@United Arab Emirates$
RECORD 5@United Kingdom$

Sorted as per country name. I need to give a condition such that it enters in the loop for every country ie say RECORD 1,RECORD 2 calculation must be done break; record 3 ,4 break; record 5 like this. Hope I am more clear now.

Maybe you mean this?

String currentCountry = "";
for (String record : myList) {
    // Regex for entire string "^....$"
    // Country between '@' and '$' (the latter escaped)
    String country = record.replaceFirst("^.*@(.*)\\$$", "$1");
    if (!country.equals(currentCountry)) {
        currentCountry = country;
        ... // Deal with next country
    }
}
for(int zTmp = 0; zTmp<myList.size(); zTmp++)
{
    System.out.println("COUNTRY IS"+myList.get(zTmp));
    if((record).contains(myList.get(zTmp)))
    {  
        // my next step
    }
}

Your if condition will result in false only if record doesn't contain a country which is not present in complete myList , otherwise It will be true atleast in one iteration.

In comments you wrote:

You want to calculate 3 records

instead of using myList , create a separate list (say myChoosenCountriesList ) of having those countries only when you want if condition to become true.

Then replace your code with following: (Note other improvments also)

int countryCount = myChoosenCountriesList.size();
for(int zTmp = 0; zTmp<countryCount; zTmp++)
{
    String countryName = myChoosenCountriesList.get(zTmp);
    System.out.println("COUNTRY IS"+countryName);
    if(record.contains(countryName))
    {  
        // my next step
    }
}

The desired output has been achieved by using do while loop here is the snippet

                 int zTmp=0;
                 do  
            {
                String country=myList.get(zTmp);
                if(inputCountry.equals(country))
                {

                    CalcDays(tmpTokens[iTmp]);
                    myDateList.clear();
                }zTmp++;
            }while(zTmp<myList.size());

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